Lesson 15 +35 XP

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

BF(N) = Height(N.left) - Height(N.right)

AVL Tree RotationsAVL 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:

Nh = Nh-1 + Nh-2 + 1
This Fibonacci-like recurrence grows exponentially, proving that for any n nodes:
h < 1.44 log2(n + 2) - 0.328 = O(log n)

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 ZRotation NeededType
LL (Left child's Left subtree)Right Rotation at ZSingle
RR (Right child's Right subtree)Left Rotation at ZSingle
LR (Left child's Right subtree)Left Rotation at Z.left, then Right Rotation at ZDouble
RL (Right child's Left subtree)Right Rotation at Z.right, then Left Rotation at ZDouble

Rotation Pointer Mechanics in C

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:

  1. Mar → May → Nov triggers RR at Mar → Left-rotate so May becomes root (Mar, Nov).
  2. Insert Aug, Apr → Apr < Aug < Mar, creating LL at Mar → Right-rotate so Aug becomes parent of Apr and Mar.
  3. 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.

🟣 Important

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

InsertImbalanceTree after insertion (node(left, right))
Mar-Mar
May-Mar(−, May)
NovRR at Mar (BF −2)May(Mar, Nov)
Aug-May(Mar(Aug, −), Nov)
AprLL at Mar (BF +2)May(Aug(Apr, Mar), Nov)
JanLR 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))
FebRL at Aug (BF −2)Mar(Dec(Aug(Apr, −), Jan(Feb, July)), May(−, Nov))
JuneLR at Mar (BF +2)Jan(Dec(Aug(Apr, −), Feb), Mar(July(−, June), May(−, Nov)))
OctRR 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:

TEXT
                    Jan
              /             \
           Dec               Mar
          /    \           /     \
        Aug    Feb      July      Nov
        /                  \     /    \
      Apr                 June  May    Oct
                                          \
                                          Sep

Height 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

InsertRotationInsertRotation
B, H, A, G, WnoneSLR at W
NRR at BURR at P
PLR at WMRL at H
RnoneDnone
ELR at GLLL at N
Inone

Final tree, level by level: P / H S / B M R W / A E L N U / D G I

TEXT
                     P
               /           \
             H               S
          /     \          /   \
        B         M       R     W
       / \       / \           /
      A   E     L   N         U
         / \   /
        D   G I
🎯 END-TERM EXAM PREP

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