Task

Implement a binary tree where each node carries an integer,   and implement:

   pre-order,
   in-order, 
   post-order,     and 
   level-order   traversal. 


Use those traversals to output the following tree:

         1
        / \
       /   \
      /     \
     2       3
    / \     /
   4   5   6
  /       / \
 7       8   9

The correct output should look like this:

preorder:    1 2 4 7 5 3 6 8 9
inorder:     7 4 2 5 1 8 6 9 3
postorder:   7 4 5 2 8 9 6 3 1
level-order: 1 2 3 4 5 6 7 8 9



See also

   Wikipedia article:   Tree traversal.


constant VALUE = 1, LEFT = 2, RIGHT = 3

constant tree = {1, {2, {4, {7, 0, 0}, 0},
                        {5, 0, 0}},
                    {3, {6, {8, 0, 0}, 
                            {9, 0, 0}},
                        0}}
 
procedure preorder(object tree)
    if sequence(tree) then
        printf(1,"%d ",{tree[VALUE]})
        preorder(tree[LEFT])
        preorder(tree[RIGHT])
    end if
end procedure
 
procedure inorder(object tree)
    if sequence(tree) then
        inorder(tree[LEFT])
        printf(1,"%d ",{tree[VALUE]})
        inorder(tree[RIGHT])
    end if
end procedure
 
procedure postorder(object tree)
    if sequence(tree) then
        postorder(tree[LEFT])
        postorder(tree[RIGHT])
        printf(1,"%d ",{tree[VALUE]})
    end if
end procedure
 
procedure level_order(object tree, sequence more = {})
    if sequence(tree) then
        more &= {tree[LEFT],tree[RIGHT]}
        printf(1,"%d ",{tree[VALUE]})
    end if
    if length(more) > 0 then
        level_order(more[1],more[2..$])
    end if
end procedure
 
puts(1,"\n preorder:    ")  preorder(tree)
puts(1,"\n inorder:     ")  inorder(tree)
puts(1,"\n postorder:   ")  postorder(tree)
puts(1,"\n level-order: ")  level_order(tree)
