Contents    Page-10    Prev    Next    Page+10    Index   

Binary Tree Recursion

Add up all numbers in a Cons tree:


(defun addnums (tree)
  (if (consp tree)               ; interior node
      (+ (addnums (first tree))
         (addnums (rest tree)) )
      (if (numberp tree)         ; leaf node
          tree
          0) ) )

Set of all symbols in a Cons tree:


(defun symbolset (tree)
  (if (consp tree)
      (union (symbolset (first tree))
             (symbolset (rest tree)) )
      (if (and (not (null tree))
               (symbolp tree))
          (list tree)
          '()) ) )