Loading lessons...
AVL Trees & Self-Balancing Rotations
AVL Trees & Self-Balancing Rotations
Named after Adelson-Velsky and Landis (1962), an AVL Tree is a height-balanced BST where the Balance Factor (BF) of every node stays in {-1, 0, +1}:
AVL Tree Rotations
1. Why AVL Trees Guarantee O(log n) Height
Let Nh be the minimum number of nodes in an AVL tree of height h. Because the two subtrees can differ in height by at most 1:
2. The 4 Imbalance Cases & Rotations
After inserting a node, walk back up toward the root updating heights. At the first ancestor Z with |BF(Z)| = 2:
| Insertion Path from Z | Rotation Needed | Type |
|---|---|---|
| LL (Left child's Left subtree) | Right Rotation at Z | Single |
| RR (Right child's Right subtree) | Left Rotation at Z | Single |
| LR (Left child's Right subtree) | Left Rotation at Z.left, then Right Rotation at Z | Double |
| RL (Right child's Left subtree) | Right Rotation at Z.right, then Left Rotation at Z | Double |
Rotation Pointer Mechanics in C
struct AVLNode* rightRotate(struct AVLNode *y) {
struct AVLNode *x = y->left;
struct AVLNode *T2 = x->right;
x->right = y;
y->left = T2;
y->height = 1 + max(height(y->left), height(y->right));
x->height = 1 + max(height(x->left), height(x->right));
return x; // new subtree root
}
struct AVLNode* leftRotate(struct AVLNode *x) {
struct AVLNode *y = x->right;
struct AVLNode *T2 = y->left;
y->left = x;
x->right = T2;
x->height = 1 + max(height(x->left), height(x->right));
y->height = 1 + max(height(y->left), height(y->right));
return y; // new subtree root
}3. Classic Alphabetical Trace (CIC-209 Deck)
Insert months in alphabetical comparison order: Mar, May, Nov, Aug, Apr, Jan, Dec, July, Feb, June, Oct, Sep:
Mar → May → Novtriggers RR atMar→ Left-rotate soMaybecomes root (Mar,Nov).- Insert
Aug, Apr→Apr < Aug < Mar, creating LL atMar→ Right-rotate soAugbecomes parent ofAprandMar. - Insert
Jan, Dec, July, Feb, June, Oct, Sep→ triggers LR and RL double rotations, keeping every node's |BF| ≤ 1.
4. AVL Deletion Rebalancing
Perform standard BST deletion, then walk back up to the root updating heights and applying LL/RR/LR/RL rotations whenever |BF| = 2. Unlike insertion (which needs at most 1 rotation), deletion may require O(log n) rotations up to the root.
Because height is strictly bounded by O(log n), AVL Search, Insert, and Delete are guaranteed O(log n) in the worst case!
Complete Months Trace (computed step by step)
Comparison is alphabetical (string order). BF = height(left) − height(right).
| Insert | Imbalance | Tree after insertion (node(left, right)) |
|---|---|---|
| Mar | - | Mar |
| May | - | Mar(−, May) |
| Nov | RR at Mar (BF −2) | May(Mar, Nov) |
| Aug | - | May(Mar(Aug, −), Nov) |
| Apr | LL at Mar (BF +2) | May(Aug(Apr, Mar), Nov) |
| Jan | LR at May (BF +2) | Mar(Aug(Apr, Jan), May(−, Nov)) |
| Dec | - | Mar(Aug(Apr, Jan(Dec, −)), May(−, Nov)) |
| July | - | Mar(Aug(Apr, Jan(Dec, July)), May(−, Nov)) |
| Feb | RL at Aug (BF −2) | Mar(Dec(Aug(Apr, −), Jan(Feb, July)), May(−, Nov)) |
| June | LR at Mar (BF +2) | Jan(Dec(Aug(Apr, −), Feb), Mar(July(−, June), May(−, Nov))) |
| Oct | RR at May (BF −2) | Jan(Dec(Aug(Apr, −), Feb), Mar(July(−, June), Nov(May, Oct))) |
| Sep | - | Jan(Dec(Aug(Apr, −), Feb), Mar(July(−, June), Nov(May, Oct(−, Sep)))) |
Final tree, level by level:
Jan
/ \
Dec Mar
/ \ / \
Aug Feb July Nov
/ \ / \
Apr June May Oct
\
SepHeight 4 for 12 keys, and every node satisfies |BF| ≤ 1.
Practice: Insert B H A G W N P R S U M D E L I
| Insert | Rotation | Insert | Rotation |
|---|---|---|---|
| B, H, A, G, W | none | S | LR at W |
| N | RR at B | U | RR at P |
| P | LR at W | M | RL at H |
| R | none | D | none |
| E | LR at G | L | LL at N |
| I | none |
Final tree, level by level: P / H S / B M R W / A E L N U / D G I
P
/ \
H S
/ \ / \
B M R W
/ \ / \ /
A E L N U
/ \ /
D G I📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. What is an AVL Tree? Explain LL, RR, LR, and RL rotations with neat diagrams. (10 Marks)
- Write: Balance Factor formula (BF ∈ {-1, 0, +1}), O(log n) height guarantee, and before/after diagrams for all 4 rotations.
- Q2. Construct an AVL Tree by inserting
Mar, May, Nov, Aug, Apr, Jan, Dec, July, Feb, June, Oct, Sep. (10 Marks) - Write: Show the tree after each insertion, mark the Balance Factor at each node, and explicitly name the rotation (RR, LL, LR, RL) whenever |BF| = 2.