Definition · RotationA right rotation at a node x with left child y: y takes x’s place, x becomes the right child of y, and y’s former right subtree becomes x’s left subtree. A left rotation is the mirror image. Both are a fixed number of pointer writes.
A rotation requires the child it lifts to exist: there is no right rotation at a node with no left child, and the lab reports that as “not possible” rather than silently returning the tree unchanged — because a panel that could not tell those apart would show “in-order unchanged” as a success.
Theorem · A rotation preserves the in-order sequenceLet x have left child y, with subtrees A and B under y and C under x. In-order before: A, y, B, x, C. After a right rotation at x, y is the root with A on its left and x on its right, and x has B on its left and C on its right. In-order after: A, y, B, x, C.
The two sequences are the same symbol by symbol, so no key has changed its position in the sorted order and the search-tree invariant holds after if it held before. The heights do change, by at most one at each of the two nodes, and that is the entire point of performing one.
rotate right at 50 keys 50 30 70 20 40 10
50 30
30 70 20 50
20 40 10 40 70
10
in-order 10 20 30 40 50 70 in-order 10 20 30 40 50 70
height 3 height 2
The lab draws both trees with the balance factor written above each node, and prints the two in-order sequences to be compared character by character. It also reports whether the rotation was possible at all. Try one at a node whose relevant child is missing and the panel says so, which matters because “nothing changed” and “the sequence was preserved” look identical from the outside.
Not every rotation shortens the tree. Rotating left at 30 in the tree built from 50 30 70 20 40 35 45 leaves the height at 3: the in-order sequence is preserved, the pointers moved, and the tree is no shorter. A rotation is a tool, not a repair; knowing which rotation to perform where is the next lesson.
The balance condition, and how tall a tree it allows
Definition · AVL treeA binary search tree in which, at every node, the heights of the two subtrees differ by at most 1. The difference h_L − h_R is the node’s balance factor, and it must be −1, 0 or 1 everywhere.
The height of an empty subtree is taken as −1 and of a leaf as 0. Every node stores its height, which is one extra integer and is what makes the condition checkable in constant time.
Theorem · The AVL condition forces a logarithmic heightLet N(h) be the fewest nodes in an AVL tree of height h. Then N(0) = 1, N(1) = 2, and N(h) = N(h−1) + N(h−2) + 1.
A tree of height h has a subtree of height exactly h−1. The other subtree has height at least h−2, because the balance condition forbids a difference of 2 or more. To make the total as small as possible, take the smallest trees of those two heights, plus the root: that is the recurrence. Since N(h) + 1 is the Fibonacci number F(h+3), N(h) grows like φʰ, so a tree on n nodes has h ≤ log_φ n, which is about 1.44 log₂ n.
h N(h) from the recurrence fits inside n nodes?
0 1 by definition
1 2 by definition
2 4 N(1) + N(0) + 1
3 7 N(2) + N(1) + 1
4 12 N(3) + N(2) + 1
5 20 N(4) + N(3) + 1
6 33 N(5) + N(4) + 1
7 54 N(6) + N(5) + 1
8 88 N(7) + N(6) + 1
N(h) + 1 = 2 3 5 8 13 21 34 55 89 Fibonacci
To bound the height of an AVL tree on n nodes, read up the N(h) column for the largest h whose entry is at most n. Six nodes: N(2) = 4 ≤ 6 and N(3) = 7 > 6, so the height is at most 2. A thousand nodes: N(13) = 986 fits and N(14) = 1596 does not, so the height is at most 13.
Compare that with the closed form. 1.44 log₂ 1000 is 14.35, so the closed form permits 14 where the exact table permits 13. Both are correct bounds and the table is tighter, which is why the lab evaluates the recurrence instead of the logarithm: the recurrence is integer arithmetic with nothing to round, and the constant 1.44 is itself a rounded 1/log₂ φ. Quoting 1.44 log₂ n is fine as a shape; using it as a number when an exact table is available is throwing away accuracy for no reason.