LOGBOOK

HELP

← again tap: answer good → ↑ swipe up at the end: next

1 / 10 Part 1 of 3
Other keys: show • Space: good • 1-4: rate • 0: skip • 5: flag

Question

What problem does an AVL tree solve, and what rule does it enforce?

Answer

1 2 3 4 5 6 height 5 = n − 1 each new key: right of every key before it −1 | 1 AVL rule at every node: height(left) and height(right) differ by ≤ 1 a chain breaks it at every node but the last two 1732444850627888 0 0 0 0 1 1 2 3 heights, counted from the bottom up 1 | 2 −1 | 0 1 | 0 0 | 0 left height | right height, at every node 2 nodes height 1 5 nodes · height 2 counts differ a lot; heights by one at every node: heights differ by at most 1keeps h in O(log n) · rotations repair it

Explanation in 6 steps

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)$.

An AVL tree with its heights next to a tree that breaks the rule at the root

* 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:

or press any other key

Question

Why does the AVL rule guarantee a height of $O(\log n)$?

Answer

h = 01 h = 12 the sparsest tree of each height height h h − 1 h − 2 one side must reach h − 1; the other may stop at h − 2 N(h) = 1 + N(h−1) + N(h−2) the root, plus the two sparsest subtrees h = 24 h = 37 1, 2, 4, 7, 12, 20, 33, 54, 88, ... 54 88 143 232 N(h), h = 0 to 10 each count ≈ 1.6 × the one before h ≤ 1.44 · log₂ na million keys: at most 28 levels · a perfect tree: 20

Explanation in 6 steps

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$.

The sparsest AVL trees of heights 1 to 4, with 2, 4, 7 and 12 nodes

* 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:

or press any other key