Balanced trees: AVL rotations
Compute balance factors, find the lowest node that an insertion unbalances in an AVL tree, and fix it with a single or a double rotation, and say why this keeps every operation at O(log n).
It opens on
Insert 5 and the left side of this tree is three lines deep, the right side one. Which of 10, 20 or 30 should be on top of those three to even it out?
Why it matters
A plain search tree is only as fast as it is short, and sorted input makes it a chain; an AVL tree repairs its own shape after every insert with a constant amount of work, so search, insert and delete stay at about log n steps whatever the order of the keys.
Good to know first
- Binary search trees, search and insert: every key on the left is smaller and on the right larger, and insert returns the node so the parent can store it
- Binary search trees, deletion: the rule can hold while the shape is as bad as a chain
- Trees and binary trees: the height of a node is the number of lines on the longest path down, and the empty tree has height −1
- Big-O, Big-Omega and Big-Theta: O(log n) grows far slower than n
Step by step
Balance factors
Compute the balance factor of every node of a search tree, say which nodes break the AVL rule, and give the middle key of the long path as the one to lift.
Take 40, a leaf with two empty subtrees. Its balance factor is −1 minus −1, which is 0. Take 10, whose left subtree is just the node 5, of height 0, and whose right subtree is empty, of height −1. Its balance factor is 0 minus −1, which is +1.
A common mix-up
A tree is balanced if it looks even at the root.
In fact: The rule is about every node, and a tree whose root is fine can still break it deeper down.
Why the rule keeps the tree short
Compute the fewest nodes N(h) of an AVL tree of height h from N(h) = N(h − 1) + N(h − 2) + 1, and conclude that its height is at most about 1.44 log₂ n.
N(2) is N(1) plus N(0) plus 1, which is 2 plus 1 plus 1, so 4. Then N(3) is 4 plus 2 plus 1, which is 7.
A common mix-up
An AVL tree is a perfectly full tree.
In fact: It may have gaps; the thinnest AVL tree of height 3 has 7 nodes, and a full one has 15.
The single rotation
Find the lowest unbalanced node after an insertion in the left-left case and fix it with a right rotation, tracing rotate_right, and say a rotation costs Θ(1) and keeps the order of the keys.
In the code y is 30, and x = y.left is 20. Then y.left = x.right makes 25, which was 20's right child, the new left child of 30. Then x.right = y makes 30 the right child of 20. The key 25 is larger than 20 and smaller than 30, so it still fits on 30's left, and the inorder list stays 5, 10, 20, 25, 30, 40.
A common mix-up
A rotation reorders or deletes keys.
In fact: It only changes three links, and the inorder list is the same before and after.
The double rotation, and the cost
Fix the left-right case with a left rotation of the child followed by a right rotation, state that one insertion needs at most one single or double rotation, and give the cost of insert as O(log n).
In the tree, balance(40) is 2, which is over 1, and balance(35) is −1, which is less than 0, so t.left becomes rotate_left(35), which is 37 with 35 as its left child. Then rotate_right(40) lifts 37 and sinks 40 to its right.
A common mix-up
One rotation always fixes an imbalance.
In fact: On a zigzag a single rotation leaves the tree as unbalanced as before; the left-right and right-left cases need two.
Try it
A node has a left subtree of height 0 and a right subtree of height 2. What is its balance factor, and does it break the rule?Answer
−2, and yes
Hint: Subtract the right height from the left height
What is the fewest nodes of an AVL tree of height 6, and can a tree of height 6 have 30 nodes?Answer
33, and no
Hint: N(6) is N(5) plus N(4) plus 1, which is 20 plus 12 plus 1
Insert 9, 5, 2 in that order into an empty AVL tree. Which node is out of balance, and which key is the root afterwards?Answer
9 is out of balance, and 5 is the root, with 2 and 9 below it
Hint: 9 has a left child 5 with a left child 2
Insert 60, 80, 70 in that order into an empty AVL tree. Which case is it, and which key is the root afterwards?Answer
Right-left, and 70 is the root with 60 and 80 below it
Hint: 80 has a left child 70, so rotate 80 right first
The node 20 has the subtree under 10 on its left and the leaf 25 on its right. What is its balance factor?Answer
+1, because the left subtree has height 1 and the right has height 0.
