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.


(defun flatten-preorder (tree)
   (if (endp tree)
       nil
       (append (list (first tree))
               (flatten-preorder (second tree))
               (flatten-preorder (third tree)))))
 
(defun flatten-inorder (tree)
   (if (endp tree)
       nil
       (append (flatten-inorder (second tree))
               (list (first tree))
               (flatten-inorder (third tree)))))
 
(defun flatten-postorder (tree)
   (if (endp tree)
       nil
       (append (flatten-postorder (second tree))
               (flatten-postorder (third tree))
               (list (first tree)))))
 
(defun flatten-level-r1 (tree level levels)
   (if (endp tree)
       levels
       (let ((curr (cdr (assoc level levels))))
            (flatten-level-r1
             (second tree)
             (1+ level)
             (flatten-level-r1
              (third tree)
              (1+ level)
              (put-assoc level
                         (append curr (list (first tree)))
                         levels))))))
 
(defun flatten-level-r2 (levels max-level)
   (declare (xargs :measure (nfix (1+ max-level))))
   (if (zp (1+ max-level))
       nil
       (append (flatten-level-r2 levels
                                 (1- max-level))
               (reverse (cdr (assoc max-level levels))))))
 
 
(defun flatten-level (tree)
   (let ((levels (flatten-level-r1 tree 0 nil)))
      (flatten-level-r2 levels (len levels))))