Contents    Page-10    Prev    Next    Page+10    Index   

Inorder Printing of Binary Tree

An ordered binary tree can be printed in sorted order by an inorder traversal. It is clear that inorder is the right algorithm since the parent is between the two children in the sort ordering.


(defun printbt (tree)
  (if (consp tree)
      (progn (printbt (lhs tree))    ; 1. L child
             (print (op tree))       ; 2. parent
             (printbt (rhs tree)))   ; 3. R child
      (if tree (print tree)) ) )


>(printbt '(cat (bat ape
                     bee)
                (elf dog
                     fox)))
APE 
BAT 
BEE 
CAT 
DOG 
ELF 
FOX