previous | index | next

Exercise 8.8, p. 222

Write a procedure called height that behaves like:
     (height (make-leaf 3)) ⇒ 0

     (height bushy-tree) ⇒ 2

     (height branchless-tree) ⇒ 6
Facts about binary trees:
  • If a tree is composed of a single leaf, its height is 0.

  • If a tree's left sub-tree is empty, its height is one plus the height of it's right sub-tree.
(define height
  (lambda (tree)
    (cond ((leaf? tree) 0)
          ((empty-tree? (left-subtree tree))
           (+ 1 (height (right-subtree tree)))))))

previous | index | next