next up previous contents
Next: Printing the nodes of Up: Binary search trees Previous: Checking whether a given   Contents

Induction over binary trees

We have already seen an example of an inductive definition over trees. Here are another two, corresponding to notions that we have already discussed:

  size(EmptyTree) = 0
  size(MkTree(v,l,r)) = 1 + size(l) + size(r)

  height(EmptyTree) = -1
  height(MkTree(v,l,r)) = 1 + max(height(l),height(r))
You may wish to pause to think why we have decided to define the height of an empty tree to be -1 rather than 0. Sometimes it is necessary to establish some properties of trees such as

$\displaystyle {\tt height}(t) < {\tt size}(t) \le 2^{{\tt height}(t)+1}-1.
$

This is intended to hold for all binary trees. Since there are infinitely many trees, how could we possibly prove this? The answer lies in the fact that, although there are infinitely many trees, there are only two rules for creating trees, as we have seen:
  1. (base case) EmptyTree is a tree.
  2. (induction step) If v is a value and l and r are trees then MkTree(v,l,r) is a tree.
Thus, each tree is obtained by starting from the base case and then applying the induction step finitely many times. This gives rise to the following proof rule, which plays a key rôle in correctness proofs of algorithms that manipulate trees:
Induction principle: In order to prove that a property holds for all binary trees, show that
  1. (base case) it holds for the empty tree EmptyTree, and
  2. (induction step) it holds for the tree MkTree(v,l,r) whenever it holds for given trees l and r.
It can be proved by induction that

$\displaystyle {\tt size}(t) \le 2^{{\tt height}(t)+1}-1.
$

This is left as an exercise. We now prove by induction that

$\displaystyle {\tt height}(t) < {\tt size}(t).
$

The base case is easy:

$\displaystyle {\tt height}({\tt EmptyTree}) = -1 < 0 = {\tt size}({\tt EmptyTree}).
$

The induction step is not hard either:
  1. Assume that we are given trees l and r such that
    $\displaystyle {\tt height}({\tt l})$ $\displaystyle <$ $\displaystyle {\tt size}({\tt l}),$ (2.1)
    $\displaystyle {\tt height}({\tt r})$ $\displaystyle <$ $\displaystyle {\tt size}({\tt r}).$ (2.2)

  2. We have to conclude that, no matter what v is,

    $\displaystyle {\tt height}({\tt MkTree(v,l,r)}) < {\tt size}({\tt MkTree(v,l,r)}).$

    We do this as follows.

  3. Combining the two equations (1) and (2), we get

    $\displaystyle {\tt height}({\tt l})+ {\tt height}({\tt r}) < {\tt size}({\tt l})+{\tt size}({\tt r}).
$

  4. Using this and the fact that

    $\displaystyle \max({\tt height}({\tt l}), {\tt height}({\tt r})) \le {\tt height}({\tt l})+ {\tt height}({\tt r}),
$

    we conclude that

    $\displaystyle \max({\tt height}({\tt l}),{\tt height}({\tt r})) < {\tt size}({\tt l})+{\tt size}({\tt r}).
$

  5. Adding one to each side of the inequality, we get

    $\displaystyle 1+\max({\tt height}({\tt l}),{\tt height}({\tt r})) < 1+{\tt size}({\tt l})+{\tt size}({\tt r}).
$

  6. By the above definitions of height and size, we conclude that

    $\displaystyle {\tt height}({\tt MkTree(v,l,r)}) < {\tt size}({\tt MkTree(v,l,r)}), $

    as we wished to conclude.


next up previous contents
Next: Printing the nodes of Up: Binary search trees Previous: Checking whether a given   Contents
Martin Escardo 2005-01-11