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.
- If a tree's right sub-tree is empty, its height is one plus the height of
it's left sub-tree.
- Otherwise, a tree's height is one plus the maximum height of its left
and right sub-trees.
|
(define height
(lambda (tree)
(cond ((leaf? tree) 0)
((empty-tree? (left-subtree tree))
(+ 1 (height (right-subtree tree))))
((empty-tree? (right-subtree tree))
(+ 1 (height (left-subtree tree))))
(else
(+ 1 (max (height (left-subtree tree))
(height (right-subtree tree))))))))
|