← again tap: answer good → ↑ swipe up at the end: next
Question
What problem does an AVL tree solve, and what rule does it enforce?
Answer
A plain binary search tree can degenerate into a chain of height $n$; an AVL tree forbids that with one rule: at every internal node, the heights of the two child subtrees differ by at most 1. That keeps the height in $O(\log n)$.
* Every node is checked, not just the root: one place where the children differ by 2 is enough to break the rule. *
Every operation on a binary search tree walks one path from the root, so it costs $O(h)$, where $h$ is the height. If the keys arrive in sorted order, each new key becomes the right child of the previous one and the tree is a linked list in disguise: $h = n - 1$, and search is no better than a linear scan: with a million keys, up to a million comparisons instead of about 20.
Adel'son-Vel'skii and Landis (1962) added a height-balance property to the search-tree rules:
for every internal node, the heights of its left and right subtree differ by at most 1.
A missing child counts as height $-1$ (a leaf has height 0), so a node with one leaf child and no other child is still fine. The rule compares heights, not node counts: the two subtrees of a node may hold very different numbers of nodes, as long as neither reaches more than one level deeper. The rule does not ask for a perfect tree. It tolerates some slack everywhere, which is what makes it cheap to maintain: the tree repairs itself with a few local rotations after an update, a self-organising tree.
How to check it by hand: compute the height of every node bottom-up (leaf 0, otherwise 1 + the larger child height), then compare the two child heights at each node.
Go deeper:
AVL tree — the balance property, the four rotation cases and the height bound.
AVL tree visualisation (USF) — insert and delete keys and watch the rotations happen.
Question
Why does the AVL rule guarantee a height of $O(\log n)$?
Answer
Because even the sparsest AVL tree of height $h$ needs $N(h) = 1 + N(h-1) + N(h-2)$ nodes, which grows like the Fibonacci numbers, i.e. exponentially in $h$. Turned around, $n$ nodes allow a height of at most about $1.44 \log_2 n$.
* Each sparsest tree is a root over the two previous ones, which is the Fibonacci recurrence drawn as trees. *
Ask the opposite question: what is the fewest nodes an AVL tree of height $h$ can have? To be as sparse as possible, the root has one subtree of height $h-1$ (it needs one, to reach height $h$) and the other as low as the rule allows, $h-2$. Both subtrees are themselves sparsest trees, so
$$N(h) = 1 + N(h-1) + N(h-2), \qquad N(0) = 1,\ N(1) = 2$$
giving 1, 2, 4, 7, 12, 20, 33, … , each one Fibonacci number minus 1. Fibonacci numbers grow by a factor of about 1.618 per step, so $N(h)$ is exponential in $h$. Taking the logarithm turns that into the bound: a tree with $n$ nodes has height below roughly $1.44 \log_2 n$.
So an AVL tree with a million keys is at most about 28 levels deep (a perfect tree would be 20). That is the whole point: search, insert and remove all walk one root-to-leaf path, and that path is guaranteed short.
Go deeper:
AVL tree — properties — the height bound with the golden ratio worked out.