Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml: Tree functions

Tags:

module

tree

ocaml

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.

like image 615
Nick Heiner Avatar asked Sep 25 '09 02:09

Nick Heiner


People also ask

How do you define a binary tree in Ocaml?

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.

What are binary trees used for?

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.

What are binary trees in data structure?

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.


1 Answers

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)

like image 61
Pascal Cuoq Avatar answered Sep 22 '22 15:09

Pascal Cuoq