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.


declare
  Tree = n(1
           n(2
             n(4 n(7 e e) e)
             n(5 e e))
           n(3
             n(6 n(8 e e) n(9 e e))
             e))
 
  fun {Concat Xs}
     {FoldR Xs Append nil}
  end
 
  fun {Preorder T}
     case T of e then nil
     [] n(V L R) then
        {Concat [[V]
                 {Preorder L}
                 {Preorder R}]}
     end
  end
 
  fun {Inorder T}
     case T of e then nil
     [] n(V L R) then
        {Concat [{Inorder L}
                 [V]
                 {Inorder R}]}
     end
  end
 
  fun {Postorder T}
     case T of e then nil
     [] n(V L R) then
        {Concat [{Postorder L}
                 {Postorder R}
                 [V]]}
     end
  end
 
  local
     fun {Collect Queue}
        case Queue of nil then nil
        [] e|Xr then {Collect Xr}
        [] n(V L R)|Xr then
           V|{Collect {Append Xr [L R]}}
        end
     end
  in
     fun {Levelorder T}
        {Collect [T]}
     end
  end
in
  {Show {Preorder Tree}}
  {Show {Inorder Tree}}
  {Show {Postorder Tree}}
  {Show {Levelorder Tree}}