Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
anggao committed Dec 28, 2011
0 parents commit 76f18d6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Some interesting data structures and algorithms implemented in Ocaml.
43 changes: 43 additions & 0 deletions rbtree.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(* Ocaml implementations of red-black trees *)
(* ref: Purely Functional Data Structures by by C Okasaki *)

(* Data type definition *)
type color = Black | Red

type 'a rbtree = Leaf | Node of color * 'a * 'a rbtree * 'a rbtree

(* check membership in red-black trees *)
let rec mem (x: 'a) (tree: 'a rbtree): bool = match tree with
| Leaf -> false
| Node (_, y, left, right) ->
x = y || (x < y && mem x left) || (x > y && mem x right)

(* Consider all the cases where a Red node has a Red child and
* rearranges the tree *)
let balance (tree: 'a rbtree) : 'a rbtree = match tree with
| Node (Black, z, Node (Red, y, Node (Red, x, a, b), c), d)
| Node (Black, z, Node (Red, x, a, Node (Red, y, b, c)), d)
| Node (Black, x, a, Node (Red, z, Node (Red, y, b, c), d))
| Node (Black, x, a, Node (Red, y, b, Node (Red, z, c, d)))
-> Node (Red, y, Node (Black, x, a, b), Node (Black, z, c, d))
| _ -> tree

(* insert new element to a red-black tree *)
let rec insert (x: 'a) (tree: 'a rbtree) : 'a rbtree =
let rec ins subtree = match subtree with
| Leaf -> Node (Red, x, Leaf, Leaf)
| Node (color, y, left, right) ->
if x < y then balance (Node (color, y, ins left, right))
else if x > y then balance (Node (color, y, left, ins right))
else subtree in
match ins tree with
| Node (_, y, a, b) -> Node (Black, y, a, b)
| Leaf -> (* guaranteed to be nonempty *)
failwith "rbtree insert failed with ins returning leaf"

(* convert list of elements to a red-black tree *)
let rec tree_of_list (lst: 'a list) : 'a rbtree = match lst with
| [] -> Leaf
| x::xs -> insert x (tree_of_list xs)


0 comments on commit 76f18d6

Please sign in to comment.