Bin_Tree_Node(T type : property.orderable,
val T,
left, right bin_tree T) ref
is
add (v T) bin_tree T =>
l := if (v < val) (left .add v) else left
r := if (v > val) (right.add v) else right
Bin_Tree_Node val l r
bin_tree(T type : property.orderable) : choice (Bin_Tree_Node T) nil is
add (v T) bin_tree T =>
match bin_tree.this
nil => Bin_Tree_Node v nil nil
n Bin_Tree_Node => n.add v
traverse (f T -> unit) unit =>
match bin_tree.this
nil =>
n Bin_Tree_Node =>
n.left.traverse f
f n.val
n.right.traverse f
for
tree bin_tree String := nil, tree.add x
x in ["dog", "elephant", "cat", "cat", "cat", "cat", "lion", "ant", "bee", "albatros", "hyena", "horse", "frog"]
else
tree.traverse (s -> say s)