Contents    Page-10    Prev    Next    Page+10    Index   

Design Pattern: Binary Tree Recursion

If we are using the form of binary tree that has contents, left and right, we can alter the design pattern slightly:

(defun myfun (tree)
         (if (not (null tree))
         (combine (contents tree)
                         (myfun (left tree))
                         (myfun (right tree)))
                 (if (test tree) ; leaf
                         (baseanswer tree)
                         safeanswer ) ) )

safeanswer depends on the combine:

combine safeanswer = identity
+ 0
* 1
and, every true
or, some false
min Integer.MAX_VALUE
max Integer.MIN_VALUE
union null