DSA - AVL Trees
Context & Motivation
- Problem: The worst-case height of a standard Binary Search Tree (BST) is
, causing operations (search, insert, delete) to degrade to time. - Goal: Keep the tree height bounded by
to ensure efficient operations. - Solution: AVL Trees (Adelson-Velsky and Landis), a self-balancing binary search tree where the height difference between left and right subtrees is strictly controlled.
AVL Tree Definition
An AVL tree is a BST that satisfies the Height-Balance Property:
For every node, the height difference between its left and right subtrees is at most 1.
Height Definition: The height of a node is the number of edges to the deepest leaf. The height of an empty tree is defined as -1.
Balance Factor: Checks if a node is balanced.
: Balanced. - Difference
: Unbalanced, requires rotation.
Height Analysis
The strict balance ensures the tree remains shallow (logarithmic height).
- Minimum Nodes Formula: The minimum number of nodes
required for an AVL tree of height follows the recurrence relation:
(Base cases:
- Complexity: Because height is
, Search, Insert, and Delete operations take O(logn) time.
3. Operations & Rebalancing
We maintain balance using Rotations. A rotation is a local change to the tree structure that preserves the BST ordering property while adjusting heights.
A. Insertion
Insert: Insert the new key as a leaf (standard BST insertion).
Trace Up: Trace the path from the new leaf back towards the root.
Check & Fix: For every node encountered, check the height difference. If unbalanced, perform the appropriate rotation.
- Note: For insertion, one rebalancing operation (single or double rotation) at the deepest unbalanced node is sufficient to fix the entire tree.
B. Deletion (Distinct from Insertion)
Deletion is more complex because fixing one imbalance might create a new imbalance higher up the tree.
Delete: Remove the node using standard BST deletion logic (e.g., replace with successor if two children).
Trace Up: Trace the path from the deletion point (parent of the deleted node) to the root.
Check & Fix: Check balance at each node on the path.
- If unbalanced, perform rotation.
- Crucial Difference: After a rotation during deletion, we must continue tracing upward to the root, as the parent node might now become unbalanced.
Rotation Cases (The 4 Scenarios)
When a node
| Case | Description | Location Type | Fix |
|---|---|---|---|
| 1. Left-Left (LL) | Insertion into the left subtree of the left child | "Outside" | Single Right Rotation |
| 2. Left-Right (LR) | Insertion into the right subtree of the left child | "Inside" | Double Rotation (Left-Right) |
| 3. Right-Left (RL) | Insertion into the left subtree of the right child | "Inside" | Double Rotation (Right-Left) |
| 4. Right-Right (RR) | Insertion into the right subtree of the right child | "Outside" | Single Left Rotation |
Special Case for Deletion
In deletion, a node might become heavy on one side (e.g., Right heavy), but its child is perfectly balanced (Left and Right subtrees of the child are equal height).
- Handling: Treat this as the "Outside" case (Case 1 or Case 4) because it is easier. A single rotation solves it.
Visualizing Rotations
Single Right Rotation (Fixes LL)
- Scenario: Node
k2is left-heavy, and its left childk1is left-heavy (or balanced). - Action:
k1moves up,k2moves down to becomek1's right child.k1's original right subtree (Y) becomesk2's left subtree .
2. Single Left Rotation (Fixes RR)
- Scenario: Node
k1is right-heavy, and its right childk2is right-heavy. - Action:
k2moves up,k1moves down to becomek2's left child.
3. Double Rotation (Fixes LR & RL)
Single rotations fail for "inside" cases because moving the child up doesn't reduce the depth of the inner subtree.
LR (Left-Right):
- Left Rotate the left child (
k1) to convert the shape into LL. - Right Rotate the unbalanced node (
k3/root).
- Left Rotate the left child (
RL (Right-Left):
- Right Rotate the right child (
k3) to convert shape into RR. - Left Rotate the unbalanced node (
k1/root).
- Right Rotate the right child (
6. Implementation Details
Storing Height
To maintain
- Strategy: Keep a
heightfield in every node. - Update: Update the
heightfield whenever the subtree is modified (insertion, deletion, or rotation). - Calculation:
node.height = max(left.height, right.height) + 1.
Updating Height During Rotation
When rotating, pointers change, so heights must be updated immediately.
- Order: Update the deeper (lower) node first (the new child), then update the new root.
7. Quick Reference: Unbalance Situations
Insert/Delete Strategy Summary:
LL (Left-Left): Imbalance is on the Left child's Left subtree.
- Fix: Single Right Rotation on the unbalanced node.
LR (Left-Right): Imbalance is on the Left child's Right subtree.
- Fix: Left Rotation on the child, then Right Rotation on the unbalanced node.
RL (Right-Left): Imbalance is on the Right child's Left subtree.
- Fix: Right Rotation on the child, then Left Rotation on the unbalanced node.
RR (Right-Right): Imbalance is on the Right child's Right subtree.
- Fix: Single Left Rotation on the unbalanced node.
Special Note for Deletion:
- If the heavy child is perfectly balanced, treat it as LL (if Left heavy) or RR (if Right heavy) and perform a Single Rotation.
Practice
Q1
What is the maximum AVL Tree height of 90 nodes?
The maximum AVL tree height for 90 nodes is 8.
Derivation:
Let
- h=0:
- h=1:
- h=2:
- h=3:
- h=4:
- h=5:
- h=6:
- h=7:
- h=8:
- h=9:
Conclusion: Since
Q2
Show the result of inserting 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty AVL Tree.
Here is the step-by-step insertion process into the AVL Tree for the sequence: 2, 1, 4, 5, 9, 3, 6, 7.
Step 1: Insert 2, 1, 4
- Insert 2: Root.
- Insert 1: Left of 2. Balanced.
- Insert 4: Right of 2. Balanced.
- Result: The tree is perfectly balanced.
Step 2: Insert 5
- Insert 5: Right of 4.
- Balance Check: Node 4 (Balance -1), Node 2 (Balance -1).
- Result: Balanced.
Step 3: Insert 9 (Rebalance)
Insert 9: Right of 5.
Imbalance: Node 4 becomes unbalanced (Right-Right Case).
- Node 4 balance: -2 (Height Left -1, Height Right 1).
- Right child (5) balance: -1.
Action: Single Left Rotation at Node 4.
- 5 becomes the parent of 4 and 9.
Step 4: Insert 3 (Rebalance)
Insert 3: Left of 4.
Imbalance: Node 2 becomes unbalanced (Right-Left Case).
- Node 2 balance: -2 (Height Left 0, Height Right 2).
- Right child (5) balance: +1 (Left Heavy).
Action: Double Rotation (RL).
- Right Rotation at Node 5 (Child 4 moves up).
- Left Rotation at Node 2 (Child 4 moves up to root).
Step 5: Insert 6 (Rebalance)
Insert 6: Left of 9.
Imbalance: Node 5 becomes unbalanced (Right-Left Case).
- Node 5 balance: -2 (Height Left -1, Height Right 1).
- Right child (9) balance: +1 (Left Heavy).
Action: Double Rotation (RL) at Node 5.
- Right Rotation at Node 9 (6 moves up).
- Left Rotation at Node 5 (6 moves up to become parent).
Step 6: Insert 7 (Rebalance)
Insert 7: Left of 9.
Imbalance: Node 6 becomes unbalanced (Right-Left Case).
- Node 6 balance: -2 (Height Left 0, Height Right 2).
- Right child (9) balance: +1 (Left Heavy).
Action: Double Rotation (RL) at Node 6.
- Right Rotation at Node 9 (7 moves up).
- Left Rotation at Node 6 (7 moves up to become parent).
Final Result
The resulting AVL Tree has 4 as the root, with left child 2 and right child 7.
- Root: 4
- Left Subtree: 2 (Children: 1, 3)
- Right Subtree: 7 (Left Child: 6 with child 5; Right Child: 9)