Why Balance Keeps the Tree Short

Since the start of the chapter we have relied on one claim: keeping every node balanced keeps the tree short, so the height stays about whatever order the values arrive in.

The balance property says a node’s two subtrees differ in height by at most one. So a node cannot have one tall subtree and one almost-empty one: if one subtree has height , the other has height at least . Every extra level of height needs nodes on both sides, so the node count grows much faster than the height.

To make this precise, we ask how tall an AVL tree of nodes can get. We answer it by asking the reverse question: for a given height , what is the fewest nodes an AVL tree that tall could possibly have? An AVL tree that tall with only that many nodes is as lean as the balance property allows. It is the most lopsided shape the property still permits. This is the minimal AVL tree of height . In the search tree chapter we saw that a perfect binary tree of height has nodes. That is the most nodes a height- tree can have. The minimal AVL tree is the other extreme.

The minimal AVL tree of height is a single leaf: node. The minimal AVL tree of height is a root with one child: nodes.

At height , the root has to reach down two levels on one side, so it needs a subtree of height beneath it. The balance property then forbids the other side from being empty; it has to be at least height . The way to satisfy both with the fewest nodes is a height- subtree on one side and a single node on the other:

       ╭───╮
       │ 7 │
       ╰─┬─╯
    ┌────┴─────┐
  ╭─┴─╮      ╭─┴─╮
  │ 5 │      │ 9 │
  ╰─┬─╯      ╰───╯
  ┌─┘
╭─┴─╮
│ 3 │
╰───╯

The total node count is , which is one more than the sum of the node counts of the two subtrees: .

The same pattern works at every height: the minimal AVL tree of height is a root on top of the minimal AVL trees of heights and . For height , the count is . The counts form the sequence

This is a Fibonacci-like sequence, and it grows exponentially. We do not need the exact formula here. We only need the growth rate: the node count more than doubles every two levels.

The minimal AVL tree of height has the fewest nodes of any AVL tree of that height, so every AVL tree of height has at least that many. So with only nodes, the height cannot exceed a constant times . (The exact constant works out to about .) Whatever order the values arrive in, an AVL tree of nodes stays about tall.