The invariant and seven inserts
Lesson 11's node gains one new field — its own height — and the whole guarantee grows out of that. There is no code in this step: three ideas first, then seven insertions in a table.
The balance factor
balance(n) = height(n.left) − height(n.right)
The AVL invariant: for every node, balance(n) is −1, 0 or +1.
Not "the tree looks reasonable" — a condition you can check at every node. That is exactly why it can be guaranteed rather than hoped for.
Why that is enough
Why does a local condition bound the height of the whole tree? Turn it around:
what is the smallest number of nodes that can still build an AVL tree of
height h?
The root has at least one subtree of height h−1, and by the invariant the other
cannot be shorter than h−2. So:
N(h) = 1 + N(h−1) + N(h−2)
That is Fibonacci. The node count grows exponentially in the height, so the height grows logarithmically in the node count. Precisely:
h ≤ 1.4405 · log2(n + 2) − 0.3277
Remember that line: the test checks exactly this, not a number somebody eyeballed.
A rotation
A rotation reassigns three pointers and changes the height without breaking the BST invariant. That is the whole reason it is useful.
n b
/ \ / \
b C => A n
/ \ / \
A x x C
Follow the keys. Before, x is to the right of b, so x > b; and it is inside
b's subtree under n's left, so x < n. After, x is to the left of n —
still between b and n. The only place it can be, and that is where it goes.
The other direction is the mirror image.
Seven keys
Insert into an empty tree, in this order:
3 2 1 7 4 5 6
We write trees as key(left,right). An empty subtree is -, a leaf is just its
key. So 2(1,3) is root 2 with children 1 and 3, and 3(2,-) is 3 with a single
child on the left.
How we count
Two numbers per row:
- comparisons — how many key comparisons you made walking down to the spot.
The same number
c.Hit()counts in code; - rotations — how many times you turned a subtree. One rotation is one turn; a double case is two rotations.
And a third thing: the case, when some node broke the invariant and had to be
repaired. There are four: LL, RR, LR, RL.
The inserts
insert cmps rotations tree: before repair → after repair
3 0 0 3
2 1 0 3(2,-)
1 2 1 LL 3(2(1,-),-) → 2(1,3)
7 2 0 2(1,3(-,7))
4 3 2 RL 2(1,3(-,7(4,-))) → 2(1,4(3,7))
5 3 1 RR 2(1,4(3,7(5,-))) → 4(2(1,3),7(5,-))
6 3 2 LR 4(2(1,3),7(5(-,6),-)) → 4(2(1,3),6(5,7))
──
total 14 6
Rows without an arrow repaired nothing: the invariant already held.
Two rows up close. First 4, where the tree leaned inwards:
inserted 4: 2(1,3(-,7(4,-)))
node 3: left 0, right 2 → balance −2 invariant broken
its right child 7 leans left (+1) → inner case, RL
1) rotate 7 right: 3(-,4(-,7)) now "right-right"
2) rotate 3 left: 4(3,7)
And 6 — the same thing, mirrored:
inserted 6: 4(2(1,3),7(5(-,6),-))
node 7: left 2, right 0 → balance +2
its left child 5 leans right (−1) → inner case, LR
1) rotate 5 left: 7(6(5,-),-) now "left-left"
2) rotate 7 right: 6(5,7)
An inner case is not new machinery: first the child is rotated so that the case
becomes an outer one, then the parent. LL and RR are one turn, LR and RL
are two.
Four observations
They are worth not skipping, because the next steps are built from nothing else.
1. All four cases occurred, once each. Six rotations for seven inserts, not four — because two of the cases are double.
2. Each row repairs in at most one place. And that is not luck. Look at row
4: node 3 was 3(-,7) before the insert, height 2. After the repair 4(3,7)
stands where it was — height 2 again. The subtree returns to the height it had,
so the nodes above it notice nothing and need no repair.
3. The same seven keys in a plain BST give height 5, not 3. Lesson 11's tree
would build 3(2(1,-),7(4(-,5(-,6)),-)) from this input: the longest path is 5
comparisons. Here it is 3.
4. The "cmps" column totals 14. The six rotations are not in it. The counter sees key comparisons only, and a rotation makes none. That column returns in step 7.
What next
You now know what rebalance does. The next step writes it down as Go — mostly
written already.