Sub topic 7
Why AVL trees have logarithmic height
This chapter assumes you know the AVL invariant (the heights of a node's two subtrees differ by at most 1) and the recurrence notation N(h) = N(h-1) + N(h-2) + 1. It uses the convention from the earlier code: a single node has height 1 and the empty tree has height 0.
The question
The invariant is local, because each node only compares its two children. Insertion and deletion keep it true everywhere, but that doesn't yet tell us how tall the whole tree can get. Search, insert and delete each cost O(height), so we need to show that height is O(log n).
The approach is to flip the question. Instead of asking how tall a tree with n nodes can be, ask how few nodes a tree of height h can have. If even the sparsest AVL tree of height h has many nodes, then h must be small compared to n.
The sparsest AVL tree
Let N(h) be the fewest nodes in any AVL tree of height h. Such a tree has a root and two subtrees. To reach height h, one subtree must have height h-1. To use as few nodes as possible, the other subtree should be as short as the invariant allows, which is h-2. Both subtrees must themselves be sparsest, so:
- N(0) = 0 and N(1) = 1.
- N(h) = N(h-1) + N(h-2) + 1 for h ≥ 2.
This is the Fibonacci recurrence plus one. The code computes it iteratively:
def min_nodes(h):
"""Fewest nodes an AVL tree of height h can have: N(h) = N(h-1) + N(h-2) + 1."""
if h == 0:
return 0
a, b = 0, 1 # N(0), N(1)
for _ in range(h - 1):
a, b = b, a + b + 1
return b
print([min_nodes(h) for h in range(1, 6)]) # [1, 2, 4, 7, 12]Connecting to Fibonacci numbers
Add 1 to each value: 2, 3, 5, 8, 13 for h = 1 to 5. These are Fibonacci numbers. With F(1) = F(2) = 1, the identity is
N(h) + 1 = F(h+2)
It holds because M(h) = N(h) + 1 satisfies M(h) = M(h-1) + M(h-2), the Fibonacci recurrence, with M(0) = 1 = F(2) and M(1) = 2 = F(3).
Fibonacci numbers grow exponentially. With φ = (1 + √5) / 2 ≈ 1.618, we have F(k) ≥ φ^(k-2). Therefore:
n + 1 ≥ N(h) + 1 = F(h+2) ≥ φ^h
Here n is the node count of any AVL tree of height h. Taking logarithms:
h ≤ log_φ(n + 1) ≈ 1.44 · log₂(n + 1)
What this means in practice
| Nodes n | Perfect tree height | Worst AVL height |
|---|---|---|
| 1,000 | 10 | 14 |
| 1,000,000 | 20 | 28 |
At a million keys, a search follows at most about 28 links in the worst case. A degenerate BST could need a million.
The bound also explains why rebalancing is cheap. Rebalancing follows one root-to-leaf path, which has at most about 1.44 log₂ n nodes. Each node on that path needs only constant work: an update and at most two rotations. So insert and delete are O(log n).
Common mistakes
- Using the wrong base case. If you treat a single node as height 0 (counting edges), the recurrence still works but the offsets change. Keep one convention throughout.
- Assuming the bound is tight for every tree. It only says the height cannot exceed this limit. Most AVL trees are shorter.
- Forgetting that both subtrees of the sparsest tree must be sparsest. If one is not, you could remove nodes and still keep the same height, which contradicts minimality.
Exercise
Using N(h) = N(h-1) + N(h-2) + 1 with the values from the code output, what is the fewest nodes an AVL tree of height 6 can have? Check it against a Fibonacci number.
Show answer
N(6) = N(5) + N(4) + 1 = 12 + 7 + 1 = 20. Check: N(6) + 1 = 21 = F(8), since the Fibonacci numbers run 1, 1, 2, 3, 5, 8, 13, 21.
Exercise
A tree stores 1,000,000 keys. Use h ≤ 1.44 · log₂(n + 1) to give an upper bound on its height if it is an AVL tree.
Show answer
log₂(1,000,001) ≈ 19.93, and 1.44 × 19.93 ≈ 28.7, so the height is at most 28. A perfectly balanced tree needs 20 levels.