Turn this:
Name | Report 1 | Report 2 |
---|---|---|
Tim | Jony | Phil |
Phil | Craig | Eddie |
Jony | Dan | Katie |
Katie | Peter | Andrea |
Into a nice Binary Tree.
Trees can be created using the Tree.new(value,left,right)
syntax. Assigning the left and right parameters is optional and will default to nil if not assigned. You can simply use tree_name.left = another_tree
if you want to assign left or right values later.
There are three ways to depth-first traverse a binary tree. In order to traverse the tree , use tree_obj.traverse(root_tree, "traveral method")
, where "traversal method"
is either in_order_traversal
, preorder_traversal
or in_order_traversal
input as strings.
------- Pre Order --------
Tim,Jony,Dan,Katie,Peter,Andrea,Phil,Craig,Eddie,
------- In Order --------
Dan,Jony,Peter,Katie,Andrea,Tim,Craig,Phil,Eddie,
------- Post Order --------
Dan,Peter,Andrea,Katie,Jony,Craig,Eddie,Phil,Tim,