๐ŸŒณ AVL Trees Study Guide

Self-Balancing Binary Search Trees

๐Ÿ“š Introduction to AVL Trees

What is an AVL Tree?

An AVL tree is a self-balancing binary search tree named after its inventors Adelson-Velsky and Landis. It maintains a balanced structure by ensuring that for every internal node, the heights of its two children differ by at most 1.

Key Property: For every internal node v, the heights of the children can differ by at most 1.

Mathematically: |height(left) - height(right)| โ‰ค 1

Why AVL Trees?

  • Guaranteed Performance: Operations are always O(log n)
  • No Worst Case: Unlike regular BST which can degrade to O(n)
  • Automatic Balancing: Self-adjusts after insertions and deletions
  • Efficient Searching: Maintains logarithmic height

๐ŸŽฏ AVL Tree Properties

Balance Factor

The balance factor of a node is calculated as:

Balance Factor = height(left subtree) - height(right subtree)

Valid balance factors: -1, 0, +1

  • +1: Left subtree is one level taller
  • 0: Both subtrees have equal height
  • -1: Right subtree is one level taller
  • +2 or -2: โš ๏ธ Tree is unbalanced! (needs rotation)

Height of AVL Tree

h < 2log(n) + 2

Therefore: h = O(log n)
Important Proof Concept:
Let n(h) = minimum number of nodes in an AVL tree of height h
โ€ข n(1) = 1
โ€ข n(2) = 2
โ€ข n(h) = 1 + n(h-1) + n(h-2)

Since n(h-1) > n(h-2), we get n(h) > 2n(h-2)
By induction: n(h) > 2^(h/2-1)
Taking logarithms: h < 2log n(h) + 2

๐Ÿ”„ Tree Rotations

Rotations are the fundamental operations used to restore balance in an AVL tree. There are 4 types of rotations based on the structure of imbalance.

When to Use Rotations?

  • When balance factor becomes +2 or -2
  • After insertion or deletion operations
  • To maintain the AVL property

1๏ธโƒฃ Single Right Rotation (LL Case)

When: Left-Left imbalance (same sign)

Pattern: Left subtree is taller, and imbalance is in left child's left subtree

z (imbalanced)
โ†™
y
โ†™
x

โฌ‡ Right Rotation

  y
โ†™ โ†˜
x   z

2๏ธโƒฃ Single Left Rotation (RR Case)

When: Right-Right imbalance (same sign)

Pattern: Right subtree is taller, and imbalance is in right child's right subtree

z (imbalanced)
  โ†˜
  y
  โ†˜
  x

โฌ‡ Left Rotation

  y
โ†™ โ†˜
z   x

3๏ธโƒฃ Left-Right Rotation (LR Case)

When: Left-Right imbalance (opposite signs)

Pattern: Left subtree is taller, but imbalance is in left child's right subtree

z
โ†™
y
 โ†˜
 x

โฌ‡ Left then Right

  x
โ†™ โ†˜
y   z

4๏ธโƒฃ Right-Left Rotation (RL Case)

When: Right-Left imbalance (opposite signs)

Pattern: Right subtree is taller, but imbalance is in right child's left subtree

z
 โ†˜
 y
โ†™
x

โฌ‡ Right then Left

  x
โ†™ โ†˜
z   y
๐Ÿ’ก Quick Rule:
  • Same Sign (both left or both right) โ†’ Single Rotation
  • Opposite Signs (left-right or right-left) โ†’ Double Rotation

Trinode Restructuring

A general approach to handle all rotation cases:

  1. Let (a, b, c) be the inorder listing of nodes x, y, z
  2. Perform rotations to make b the topmost node
  3. a becomes the left child, c becomes the right child
  4. Subtrees Tโ‚€, Tโ‚, Tโ‚‚, Tโ‚ƒ are distributed appropriately

โž• Insertion in AVL Trees

Algorithm Steps

  1. Insert as in BST: Find the correct position and insert the new node
  2. Update Heights: Travel up from the inserted node, updating heights
  3. Check Balance: At each ancestor node, check the balance factor
  4. Perform Rotation: If unbalanced (|BF| = 2), perform appropriate rotation
  5. Continue Upward: Continue checking until root is reached

๐Ÿ“ Example: Inserting 54

Consider an AVL tree with nodes: 44, 17, 78, 32, 50, 88, 48, 62

After inserting 54:

  • Node 62 becomes unbalanced (balance factor = -2)
  • Pattern: Right-Left (RL case)
  • Solution: Right rotation at 62, then restructure
  • Result: Balanced tree with 54 in correct position
Key Insight: Only ONE rotation is needed for insertion! The rotation fixes all imbalances created by the insertion.

Complexity Analysis

Time Complexity: O(log n)
  • Finding insertion point: O(log n)
  • Updating heights: O(log n)
  • Single rotation: O(1)
  • Total: O(log n)

โž– Deletion in AVL Trees

Algorithm Steps

  1. Delete as in BST: Remove the node using standard BST deletion
  2. Start from Parent: Begin at the parent of the deleted node
  3. Update Heights: Recalculate heights going upward
  4. Check Balance: Check balance factor at each node
  5. Perform Rotations: If unbalanced, perform appropriate rotation
  6. Continue to Root: Must check ALL the way to the root
โš ๏ธ Critical Difference from Insertion:
Deletion may require MULTIPLE rotations as you travel up to the root! Each rotation might create a new imbalance higher up.

๐Ÿ“ Example: Deleting 32

From a tree with nodes: 44, 17, 62, 32, 50, 78, 48, 54, 88

  1. Delete node 32 (leaf node)
  2. Parent 17 becomes unbalanced
  3. Perform rotation at 17
  4. Check parent nodes up to root
  5. Perform additional rotations if needed

BST Deletion Cases

  • Case 1: Node has no children (leaf) โ†’ Simply remove
  • Case 2: Node has one child โ†’ Replace node with its child
  • Case 3: Node has two children โ†’ Replace with inorder successor/predecessor

Complexity Analysis

Time Complexity: O(log n)
  • Finding node: O(log n)
  • Deleting node: O(1)
  • Rebalancing (multiple rotations): O(log n)
  • Total: O(log n)

โšก Performance Analysis

Operation AVL Tree (Average) AVL Tree (Worst) BST (Average) BST (Worst)
Search O(log n) O(log n) O(log n) O(n)
Insertion O(log n) O(log n) O(log n) O(n)
Deletion O(log n) O(log n) O(log n) O(n)
Traversal O(n) O(n) O(n) O(n)

Space Complexity

O(n) - AVL trees store n nodes plus height information at each node

๐Ÿ’ก Why AVL Trees are Better:
  • Predictable: No worst-case O(n) operations
  • Balanced: Always maintains logarithmic height
  • Efficient: All operations guaranteed O(log n)
  • Reliable: Performance doesn't depend on insertion order

๐Ÿ’ป Key Implementation Concepts

Node Structure

public class AVLNode { int key; int height; AVLNode left, right; public AVLNode(int key) { this.key = key; this.height = 1; // New node is initially at height 1 } }

Essential Functions

1. Height Calculation

protected int height(Position p) { return tree.getAux(p); // Stored as auxiliary data } protected void recomputeHeight(Position p) { int h = 1 + Math.max(height(left(p)), height(right(p))); tree.setAux(p, h); }

2. Balance Factor Check

protected boolean isBalanced(Position p) { return Math.abs(height(left(p)) - height(right(p))) <= 1; }

3. Taller Child Selection

protected Position tallerChild(Position p) { if (height(left(p)) > height(right(p))) return left(p); // Left is taller if (height(left(p)) < height(right(p))) return right(p); // Right is taller // Equal height: match parent's orientation if (p == left(parent(p))) return left(p); // Return aligned child else return right(p); }

4. Rebalancing After Insertion

protected void rebalanceInsert(Position p) { rebalance(p); // Check balance and fix if needed } protected void rebalance(Position p) { int oldHeight, newHeight; do { oldHeight = height(p); if (!isBalanced(p)) { // Perform trinode restructuring p = restructure(tallerChild(tallerChild(p))); recomputeHeight(left(p)); recomputeHeight(right(p)); } recomputeHeight(p); newHeight = height(p); p = parent(p); } while (oldHeight != newHeight && p != null); }

5. Rebalancing After Deletion

protected void rebalanceDelete(Position p) { if (!isRoot(p)) rebalance(parent(p)); // Must check from parent up to root }

๐Ÿ“– Study Tips & Common Mistakes

โœ… Key Concepts to Master

  • Understanding balance factor calculation
  • Identifying which rotation to use (same vs opposite signs)
  • Recognizing that insertion needs only ONE rotation
  • Remembering that deletion may need MULTIPLE rotations
  • Height calculation after rotations
  • Trinode restructuring process

โš ๏ธ Common Mistakes to Avoid

  1. Forgetting to update heights after rotations
  2. Stopping at first rotation during deletion (must go to root!)
  3. Wrong rotation selection - remember same sign = single, opposite = double
  4. Not maintaining BST property during rotations
  5. Calculating balance factor incorrectly (it's left - right, not right - left)

๐ŸŽฏ Practice Problems

  1. Insert nodes 45, 70, 35, 3, 74, 25, 81, 4 into an empty AVL tree. Show all rotations.
  2. Delete node 25 from the tree created above. Show all rebalancing steps.
  3. Given a tree of height h, prove the minimum number of nodes is approximately Fibonacci(h+2).
  4. Compare AVL trees with Red-Black trees - when would you use each?

๐Ÿ” Quick Reference

Rotation Decision Tree:
Is node unbalanced (|BF| = 2)?
โ”œโ”€ NO โ†’ Continue checking parent
โ””โ”€ YES โ†’ Which subtree is taller?
    โ”œโ”€ Left (BF = +2)
    โ”‚   โ”œโ”€ Left child's left subtree taller โ†’ LL: Single Right Rotation
    โ”‚   โ””โ”€ Left child's right subtree taller โ†’ LR: Double Rotation (Left-Right)
    โ””โ”€ Right (BF = -2)
        โ”œโ”€ Right child's right subtree taller โ†’ RR: Single Left Rotation
        โ””โ”€ Right child's left subtree taller โ†’ RL: Double Rotation (Right-Left)
                    

๐Ÿ“‹ Summary

AVL Tree Advantages

  • โœ… Guaranteed O(log n) for search, insert, delete
  • โœ… More strictly balanced than Red-Black trees
  • โœ… Better search performance (fewer comparisons)
  • โœ… Predictable performance regardless of input order

AVL Tree Disadvantages

  • โŒ More rotations during insertion/deletion than Red-Black trees
  • โŒ Extra space for height storage at each node
  • โŒ More complex implementation than simple BST
  • โŒ Slightly slower insertions/deletions compared to Red-Black trees

๐ŸŽ“ Remember for Exams

  • Height of AVL tree with n nodes: h = O(log n)
  • Balance factor range: -1, 0, +1
  • All operations: O(log n)
  • Space complexity: O(n)
  • Insertion: 1 rotation max
  • Deletion: O(log n) rotations possible
  • Rotation time: O(1)

๐ŸŽ“ Practice Questions

Question 1: Define AVL Tree Balance Factor

Q: What is the balance factor of a node, and what values indicate a valid AVL tree versus an unbalanced one?

A: The balance factor of a node is calculated as height(left subtree) - height(right subtree). Valid balance factors in an AVL tree are -1, 0, or +1. A value of +2 means the left subtree is two levels taller (rotate right), and -2 means the right subtree is two levels taller (rotate left). Any node with |BF| = 2 is unbalanced and triggers a rotation.

Question 2: The Four Rotation Types

Q: How do you determine which of the four rotations (LL, RR, LR, RL) to apply?

A: Check the balance factor of the unbalanced node and its taller child:
- BF = +2 and child BF = +1 (LL): Single right rotation at the unbalanced node.
- BF = -2 and child BF = -1 (RR): Single left rotation at the unbalanced node.
- BF = +2 and child BF = -1 (LR): Left rotation on the child, then right rotation on the unbalanced node.
- BF = -2 and child BF = +1 (RL): Right rotation on the child, then left rotation on the unbalanced node.
The quick rule: same sign โ†’ single rotation; opposite signs โ†’ double rotation.

Question 3: Why Is AVL Height O(log n)?

Q: Sketch the proof that an AVL tree with n nodes has height h < 2 log n + 2.

A: Define n(h) as the minimum number of nodes in an AVL tree of height h. The recurrence is n(h) = 1 + n(h-1) + n(h-2), with n(1) = 1, n(2) = 2. Since n(h-1) > n(h-2), we get n(h) > 2ยทn(h-2), which by induction gives n(h) > 2^(h/2 - 1). Solving for h yields h < 2 log n(h) + 2, so h = O(log n). This is the fundamental reason all AVL operations are O(log n).

Question 4: Trace an Insertion

Q: Insert the values 30, 20, 40, 10, 25 into an empty AVL tree, step by step. Which rotation (if any) is needed after each insertion?

A: 1. Insert 30 โ†’ root, balanced.
2. Insert 20 โ†’ left child of 30, balanced.
3. Insert 40 โ†’ right child of 30, balanced.
4. Insert 10 โ†’ left child of 20, balanced.
5. Insert 25 โ†’ right child of 20. Node 30 has BF = +2 (left subtree height 2, right height 1). Node 20 has BF = -1 (LR case: opposite signs). Apply Left-Right double rotation: left-rotate at 20, then right-rotate at 30. Result: 25 becomes the new root of that subtree, with 20 as its left child and 30 as its right child.

Question 5: Insertion vs. Deletion โ€” Number of Rotations

Q: Why does insertion require at most ONE rotation, while deletion may require O(log n) rotations?

A: During insertion, a single rotation restores the height of the subtree to what it was before the insertion. Because the height is restored, no ancestor can become unbalanced โ€” the fix propagates no further. During deletion, a rotation may reduce the height of a subtree by one, which can propagate imbalance upward to the parent, grandparent, and so on all the way to the root. Each level may need its own rotation, giving up to O(log n) rotations in the worst case.

Question 6: AVL vs. Regular BST Worst Case

Q: Give a concrete example of a sequence of insertions that causes a regular BST to degrade to O(n) search, and explain why the same sequence causes no problem for an AVL tree.

A: Inserting 1, 2, 3, 4, 5 in sorted order into a BST creates a right-skewed chain (like a linked list) with height n-1 = 4, so search is O(n). In an AVL tree, after inserting 3 the tree is balanced (1โ†’2โ†’3 triggers an RR left rotation, making 2 the root with 1 and 3 as children). Subsequent insertions are similarly rebalanced. The AVL tree's height stays at โŒŠlog nโŒ‹, guaranteeing O(log n) search regardless of insertion order.

Question 7: Exam Trap โ€” Updating Heights

Q: After performing a rotation, which nodes' heights must be updated and in what order? What goes wrong if you skip this step?

A: After a rotation that makes node b the new root of a subtree (with children a and c), you must update heights in the order: recomputeHeight(a), recomputeHeight(c), then recomputeHeight(b) โ€” children before parent. If you skip height updates, balance factor checks above the rotated subtree will use stale values and incorrectly report the tree as balanced (or unbalanced) when it is not, leading to missed rotations or spurious rotations on subsequent operations.

Question 8: Space and Time Complexity Summary

Q: State the time and space complexity for search, insert, delete, and traversal in an AVL tree, and justify each.

A: - Search: O(log n) โ€” height is O(log n), we follow at most one root-to-leaf path.
- Insert: O(log n) โ€” BST insert O(log n) + at most one O(1) rotation + O(log n) height updates.
- Delete: O(log n) โ€” BST delete O(log n) + O(log n) rotations (one per level) each O(1).
- Traversal: O(n) โ€” every node visited once.
- Space: O(n) โ€” n node objects, each storing an extra integer for height.