Published on

AVL Trees - Self-Balancing Binary Search Trees

Authors
  • avatar
    Name
    Balaram Shiwakoti
    Twitter

AVL trees were initially the most intimidating data structure for me during DSA preparation. I kept thinking, "Why can't we just use regular binary search trees?" But once I understood how unbalanced trees can degrade to O(n) performance and how AVL trees maintain O(log n) through rotations, I realized their brilliance. Let me share what I've learned about these self-balancing trees.

Introduction to AVL Trees

When I first learned about binary search trees, I was excited about their O(log n) search time. But then I discovered the worst case - when insertions create a skewed tree that's essentially a linked list with O(n) operations. That's where AVL trees come to the rescue!

AVL Tree (named after Adelson-Velsky and Landis) is a self-balancing binary search tree where the height difference between left and right subtrees of any node is at most 1.

AVL Tree Properties

Balance Factor

Balance Factor (BF) = Height of Left Subtree - Height of Right Subtree

For any node in an AVL tree:

  • BF ∈ 1
  • BF = -1: Right subtree is 1 level taller
  • BF = 0: Both subtrees have equal height
  • BF = 1: Left subtree is 1 level taller

Example:

    10 (BF = 0)
   /  \
  5    15 (BF = -1)
 / \     \
3   7     20

Height Property

For an AVL tree with n nodes:

  • Minimum height: ⌊log₂(n)⌋
  • Maximum height: 1.44 × log₂(n)
  • This guarantees O(log n) operations

I remember being amazed that such a simple constraint (height difference ≤ 1) could provide such strong performance guarantees!

AVL Tree Rotations

When insertion or deletion violates the AVL property, we restore balance using rotations. There are four types of rotations:

1. Right Rotation (LL Case)

When to use: Left subtree of left child is taller

Before rotation:        After rotation:
      z                      y
     / \                    / \
    y   T4                 x   z
   / \          -->       / \ / \
  x   T3                T1 T2 T3 T4
 / \
T1  T2

Algorithm:

  1. Store y = z.left and T3 = y.right
  2. Make y the new root
  3. Make z the right child of y
  4. Make T3 the left child of z
  5. Update heights

2. Left Rotation (RR Case)

When to use: Right subtree of right child is taller

Before rotation:        After rotation:
    z                        y
   / \                      / \
  T1  y                    z   x
     / \        -->       / \ / \
    T2  x               T1 T2 T3 T4
       / \
      T3 T4

Algorithm:

  1. Store y = z.right and T2 = y.left
  2. Make y the new root
  3. Make z the left child of y
  4. Make T2 the right child of z
  5. Update heights

3. Left-Right Rotation (LR Case)

When to use: Right subtree of left child is taller

Step 1: Left rotate on y    Step 2: Right rotate on z
      z                         z                      x
     / \                       / \                    / \
    y   T4                    x   T4                 y   z
   / \          -->         / \        -->         / \ / \
  T1  x                    y   T3                T1 T2 T3 T4
     / \                  / \
    T2 T3               T1  T2

Algorithm:

  1. First perform left rotation on z.left
  2. Then perform right rotation on z

4. Right-Left Rotation (RL Case)

When to use: Left subtree of right child is taller

Step 1: Right rotate on y   Step 2: Left rotate on z
    z                         z                      x
   / \                       / \                    / \
  T1  y                     T1  x                  z   y
     / \        -->            / \      -->       / \ / \
    x   T4                    T2  y             T1 T2 T3 T4
   / \                           / \
  T2 T3                        T3 T4

Algorithm:

  1. First perform right rotation on z.right
  2. Then perform left rotation on z

AVL Tree Operations

Insertion Operation

Algorithm Steps:

  1. Perform standard BST insertion (color new node red)
  2. Update height of current node
  3. Calculate balance factor
  4. If unbalanced, perform appropriate rotation:
    • LL Case: Right rotation
    • RR Case: Left rotation
    • LR Case: Left-Right rotation
    • RL Case: Right-Left rotation

Deletion Operation

Algorithm Steps:

  1. Perform standard BST deletion
  2. Update height of current node
  3. Calculate balance factor
  4. If unbalanced, perform appropriate rotation
  5. Handle three deletion cases:
    • Node with no children
    • Node with one child
    • Node with two children (replace with successor)

Complete Example Walkthrough

Let's trace through building an AVL tree by inserting: 10, 20, 30, 40, 50, 25

Insert 10:

10 (BF = 0)

Insert 20:

  10 (BF = -1)
    \
     20 (BF = 0)

Insert 30:

Before balancing:        After left rotation:
  10 (BF = -2)              20 (BF = 0)
    \                      /  \
     20 (BF = -1)        10    30
       \
        30

Insert 40:

    20 (BF = -1)
   /  \
  10   30 (BF = -1)
         \
          40

Insert 50:

Before balancing:           After left rotation on 30:
    20 (BF = -2)               20 (BF = -1)
   /  \                       /  \
  10   30 (BF = -2)          10   40 (BF = 0)
         \                       / \
          40 (BF = -1)          30  50
            \
             50

Insert 25:

Final tree:
      20 (BF = -1)
     /  \
   10    40 (BF = 1)
        /  \
      30    50
     /
   25

I found that drawing these step-by-step really helped me understand when rotations are needed!

Time and Space Complexity

Time Complexity

  • Search: O(log n)
  • Insertion: O(log n)
  • Deletion: O(log n)
  • All operations guaranteed due to height balance

Space Complexity

  • Storage: O(n) for n nodes
  • Recursion stack: O(log n) for operations

Comparison with Regular BST

OperationRegular BSTAVL Tree
Search (Average)O(log n)O(log n)
Search (Worst)O(n)O(log n)
Insert (Average)O(log n)O(log n)
Insert (Worst)O(n)O(log n)
Delete (Average)O(log n)O(log n)
Delete (Worst)O(n)O(log n)

Advantages and Disadvantages

Advantages

  1. Guaranteed O(log n) performance for all operations
  2. Self-balancing - no manual rebalancing needed
  3. Better than regular BST for search-heavy applications
  4. Predictable performance - no worst-case degradation

Disadvantages

  1. Extra storage for height information
  2. More complex implementation than regular BST
  3. Rotation overhead during insertions/deletions
  4. Not optimal for frequent insertions/deletions

Applications of AVL Trees

Database Indexing

  • B+ trees (used in databases) are based on similar balancing principles
  • Ensures consistent query performance
  • Critical for large-scale database systems

Memory Management

  • Operating system memory allocation
  • Virtual memory management
  • Ensures efficient memory lookup

Symbol Tables

  • Compiler symbol tables
  • Variable name lookup
  • Function resolution

In-Memory Databases

  • Redis sorted sets
  • In-memory key-value stores
  • Real-time analytics systems

AVL vs Other Self-Balancing Trees

AVL vs Red-Black Trees

AspectAVL TreesRed-Black Trees
Balance GuaranteeStricter (height diff ≤ 1)Looser (longest path ≤ 2× shortest)
Search PerformanceFasterSlightly slower
Insert/DeleteMore rotationsFewer rotations
MemoryHeight fieldColor bit
Use CaseSearch-heavyInsert/delete-heavy

AVL vs Splay Trees

AspectAVL TreesSplay Trees
BalanceAlways balancedSelf-adjusting
Worst CaseO(log n) guaranteedO(n) possible
Recently AccessedNo special treatmentMoved to root
ImplementationMore complexSimpler

Common Implementation Pitfalls

1. Forgetting Height Updates

  • Always update node height after insertion/deletion
  • Height = 1 + max(left_height, right_height)
  • Critical for correct balance factor calculation

2. Incorrect Balance Factor Calculation

  • Correct formula: left_height - right_height
  • Wrong formula: right_height - left_height
  • Affects rotation decision logic

3. Missing Edge Cases

  • Handle null nodes properly in height calculation
  • Check for null nodes in balance factor calculation
  • Ensure robust error handling

Optimization Techniques

Iterative Implementation

  • Avoid recursion stack overflow for large trees
  • Maintain path during insertion for bottom-up rebalancing
  • More complex but memory efficient

Bulk Loading

  • Efficiently build AVL tree from sorted array
  • Use divide-and-conquer approach
  • O(n) construction time for sorted input

Conclusion

AVL trees are a fundamental self-balancing data structure that guarantees O(log n) performance for all operations. Key takeaways:

Core Concepts:

  • Balance factor must be in 1
  • Four types of rotations restore balance
  • Height information enables efficient balancing

Performance:

  • Guaranteed O(log n) for search, insert, delete
  • Better than regular BST for search-heavy workloads
  • Slight overhead for balancing operations

For loksewa preparation, focus on:

  • Understanding balance factor and rotation types
  • Implementing insertion with proper rebalancing
  • Analyzing time and space complexity
  • Comparing with other tree structures
  • Recognizing when AVL trees are appropriate

Remember: AVL trees trade some insertion/deletion performance for guaranteed search performance. This makes them ideal for applications where searches are frequent and predictable performance is crucial!

The key insight is that maintaining a simple invariant (height difference ≤ 1) provides powerful performance guarantees - a beautiful example of how constraints can enable efficiency.