This is a module for using red-black trees in OCaml. It can be used to store simple elements that can be compared with (<), (=), and (>), or for more complicated types in real-world applications. Aside from an exception used in the remove functions to indicate failure, all of the functions are purely functional. User's guide type 'a t: the type of red-black trees empty: an empty tree insert e t: add an element to a tree and return the new tree insert_new e t: don't insert if the element is already in the tree remove e t: remove one node containing the element and return the new tree remove_all e t: remove all nodes containing the element size t: the number of nodes in a tree (takes O(N) time) black_height t: the black height of the tree is_member e t: whether an element is in a tree of_list l: create a tree from a list to_list t: create a list from a tree merge t1 t2: all the nodes in the two trees, together in one tree union t1 t2: merge with no duplication (uses insert_new instead of insert) Duplicates are eliminated from the smaller of the two trees (based on black height), but not from the larger one. fold_left f x0 t: in-order fold fold_right f x0 t: reverse in-order fold map f t: map for trees mapi f t: map with in-order numbering of nodes iter f t: in-order iteration iteri f t: iter with in-order numbering Make (E): a functor to create modules for complex elements In modules generated by Make, is_member and the removal functions take a key instead of an element. Those modules have concrete types, along with two additional functions: type element = E.t type t = element RedBlackTree.t find k t: returns an optional element from a tree with a specified key value k t: calls find and extracts the value from the element User-defined modules don't have map or mapi, but you can use the ones in RedBlackTree. To create a module with Make, you'll need to write your own module describing the elements in the tree: Typeof_Element: the type of element modules that can be passed to Make Your module must contain these items: t: the type of the elements in the tree tkey: the type of element keys tval: the type of element values key e: returns an element's key value e: returns an element's value compare k1 k2: the comparison function. Returns zero for equal keys, a negative integer when k1 belongs before k2, and a positive integer when k1 belongs after k2. Once you've defined an element module, you can create your own tree module: module MyRedBlackTree = RedBlackTree.Make (MyElement)