/* * P1b Tree representation * Kevin Angstadt * St. Lawrence University * * recall you can do: open P1btree; */ type tree('a) = { data: 'a, left: option(tree ('a)), right: option(tree('a)) }; /* accessor functions */ let root_data = (t: tree('a)) : int => t.data; let left_child = (t: tree('a)) : option(tree('a)) => t.left; let right_child = (t: tree('a)) : option(tree('a)) => t.right; /* * draw a tree on standard out. * t : the tree * repr : function for converting tree data to string */ let draw = ((t: tree('a)), (repr: 'a => string)) : unit => { let rec indent = ((t: tree('a)), (prefix: string)) => { switch (right_child(t)) { | None => () | Some(subt) => indent(subt, (prefix ++ " ")) }; Printf.printf("%s--%s\n", prefix, repr(root_data(t))); switch (left_child(t)) { | None => () | Some(subt) => indent(subt, (prefix ++ " ")); } }; indent(t, ""); }; /* let's define an int_tree */ type int_tree = tree(int); /* draw an int_tree */ let draw_int_tree = (t: int_tree) : unit => { draw(t, (i => Printf.sprintf("%i", i))) }