Binary Tree Recursion

The recursions we have seen so far have involved linear structures, that is, lists in which only the top level was involved. A second class of recursive programs operates on both halves of a pair; in general, such functions call themselves twice.

; Copy a tree structure
(define (copy-tree tree)
  (if (pair? tree)
      (cons (copy-tree (car tree))
            (copy-tree (cdr tree)))

; Test equality of structure (define (equal? x y) (if (pair? x) (and (pair? y) (equal? (car x) (car y)) (equal? (cdr x) (cdr y))) (eqv? x y)) )

Contents    Page-10    Prev    Next    Page+10    Index