Skip to content

Instantly share code, notes, and snippets.

@kunishi
Created July 2, 2012 03:00
Show Gist options
  • Save kunishi/3030736 to your computer and use it in GitHub Desktop.
Save kunishi/3030736 to your computer and use it in GitHub Desktop.
Binary Tree, a simplified version of the codes in 'Elements of ML Programming (ML97 Edition)'
datatype 'label btree =
Empty |
Node of 'label * 'label btree * 'label btree;
fun lower(nil) = nil
| lower(c::cs) = (Char.toLower c)::lower(cs);
fun lt(x, y) =
implode(lower(explode(x))) < implode(lower(explode(y)));
fun insert(x, Empty) = Node(x, Empty, Empty)
| insert(x, T as Node(y, left, right)) =
if x=y then T
else if lt(x, y) then Node(y, insert(x, left), right)
else (* lt(y, x) *) Node(y, left, insert(x, right));
fun list_to_bintree(nil) = Empty
| list_to_bintree(x::xs) = insert(x, list_to_bintree(xs));
fun inOrder(Empty) = nil
| inOrder(Node(a, left, right)) =
inOrder(left) @ [a] @ inOrder(right);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment