Are there any modules or functions for dealing with trees? I have a type that looks like this:
type t =
Leaf of string (* todo: replace with 'a *)
| Node of string * t list
I'm struggling to do insertion, removal of subtrees, etc.
I've used Google but can't find anything.
A binary tree, as you'll recall from CS 2110, is a node containing a value and two children that are trees. A binary tree can also be an empty tree, which we also use to represent the absence of a child node.
In computing, binary trees are mainly used for searching and sorting as they provide a means to store data hierarchically. Some common operations that can be conducted on binary trees include insertion, deletion, and traversal.
In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.
Read the implementation of module Set in the sources of the OCaml standard library. They are implemented with binary trees, only a little more sophisticated than yours.
(I would recommend you start with binary trees instead of having a list of children as you seem to have defined)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With