previous | index | next

Preorder Traversal

Another reason for doing a preorder traversal would be to create a (flat) list of the nodes:
     (preorder bushy-tree) ⇒ (4 2 1 3 6 5 7)
To create this list, the built-in procedure append is useful. Example:
     (append '(a b c) '(1 2 3 4)) ⇒ (a b c 1 2 3 4)

     (define preorder
       (lambda (tree)
         (if (empty-tree? tree)
             '()
             (cons (root tree) (append 
                                (preorder (left-subtree tree))
                                (preorder (right-subtree tree)))))))

previous | index | next