AVL Tree

Self-balancing BST, balance factor, LL/RR/LR/RL rotations

Introduction

An AVL Tree is a self-balancing Binary Search Tree (BST) where the difference between the heights of the left and right subtrees of any node cannot be more than 1.

AVL stands for:

  • Adelson
  • Velsky
  • Landis

The tree was introduced by Soviet mathematicians Georgy Adelson-Velsky and Evgenii Landis in 1962.

AVL Trees automatically maintain balance after insertion and deletion operations, ensuring efficient search performance.


Why Do We Need AVL Trees?

Consider a normal Binary Search Tree.

Balanced BST

        50
       /  \
     30    70
    / \    / \
   20 40 60 80

Height:

O(log n)

Search Complexity:

O(log n)

Skewed BST

Insert:

10, 20, 30, 40, 50

Result:

10
  \
   20
     \
      30
        \
         40
           \
            50

Height:

O(n)

Search Complexity:

O(n)

The BST behaves like a Linked List.

AVL Trees solve this problem by maintaining balance automatically.


Properties of AVL Tree

An AVL Tree satisfies:

Property 1

It is a Binary Search Tree.

Left Subtree < Root < Right Subtree

Property 2

For every node:

| Height(Left) - Height(Right) | ≤ 1

Property 3

Balance is maintained after insertion and deletion.


Height of a Node

Height is the number of edges in the longest path from a node to a leaf.

Example:

        10
       /  \
      5   20
         /
       15

Heights:

15 = 0
20 = 1
5  = 0
10 = 2

Balance Factor

The Balance Factor determines whether a node is balanced.

Formula:

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

Example

        10
       / \
      5  20

Balance Factor:

BF = 0 - 0 = 0

Balanced.


Example

        10
       /
      5
     /
    2

Balance Factor:

BF = 2 - 0 = 2

Unbalanced.


AVL Tree Balance Conditions

A node is balanced if:

BF = -1
BF = 0
BF = +1

A node is unbalanced if:

BF < -1
or
BF > +1

Rotations in AVL Tree

When imbalance occurs, AVL Tree performs rotations.

There are four types of rotations:

  1. LL Rotation
  2. RR Rotation
  3. LR Rotation
  4. RL Rotation

1. LL Rotation (Left Left Case)

Occurs when insertion happens in the left subtree of the left child.


Example

Insert:

30
20
10

Tree:

      30
     /
    20
   /
 10

Balance Factor:

30 = +2

Unbalanced.


Solution

Perform Right Rotation.

Before:

      30
     /
    20
   /
 10

After:

      20
     /  \
   10   30

Balanced.


2. RR Rotation (Right Right Case)

Occurs when insertion happens in the right subtree of the right child.


Example

Insert:

10
20
30

Tree:

10
  \
   20
     \
      30

Unbalanced.


Solution

Perform Left Rotation.

After Rotation:

      20
     /  \
   10   30

Balanced.


3. LR Rotation (Left Right Case)

Occurs when insertion happens in the right subtree of the left child.


Example

Insert:

30
10
20

Tree:

      30
     /
    10
      \
       20

Unbalanced.


Solution

Step 1

Left Rotation on 10

      30
     /
    20
   /
 10

Step 2

Right Rotation on 30

      20
     /  \
   10   30

Balanced.


4. RL Rotation (Right Left Case)

Occurs when insertion happens in the left subtree of the right child.


Example

Insert:

10
30
20

Tree:

10
  \
   30
   /
 20

Unbalanced.


Solution

Step 1

Right Rotation on 30

10
  \
   20
     \
      30

Step 2

Left Rotation on 10

      20
     /  \
   10   30

Balanced.


Summary of Rotations

CaseRotation
LLRight Rotation
RRLeft Rotation
LRLeft + Right Rotation
RLRight + Left Rotation

Node Structure

Java Implementation

class Node {

    int key;
    int height;

    Node left;
    Node right;

    Node(int key) {
        this.key = key;
        this.height = 1;
    }
}

Utility Functions

Height Function

private int height(Node node) {

    if(node == null)
        return 0;

    return node.height;
}

Balance Factor

private int getBalance(Node node) {

    if(node == null)
        return 0;

    return height(node.left)
           - height(node.right);
}

Right Rotation

private Node rightRotate(Node y) {

    Node x = y.left;
    Node t2 = x.right;

    x.right = y;
    y.left = t2;

    y.height =
        Math.max(height(y.left),
                 height(y.right)) + 1;

    x.height =
        Math.max(height(x.left),
                 height(x.right)) + 1;

    return x;
}

Left Rotation

private Node leftRotate(Node x) {

    Node y = x.right;
    Node t2 = y.left;

    y.left = x;
    x.right = t2;

    x.height =
        Math.max(height(x.left),
                 height(x.right)) + 1;

    y.height =
        Math.max(height(y.left),
                 height(y.right)) + 1;

    return y;
}

AVL Tree Insertion

Steps:

  1. Insert node like BST.
  2. Update height.
  3. Calculate balance factor.
  4. Detect imbalance.
  5. Apply appropriate rotation.

Insertion Algorithm

Insert Node
      ↓
Update Height
      ↓
Calculate Balance Factor
      ↓
Identify Case
      ↓
Perform Rotation

AVL Insertion Complexity

OperationComplexity
SearchO(log n)
InsertO(log n)
DeleteO(log n)

AVL Tree Deletion

Deletion is more complex than insertion.

Steps:

  1. Delete node like BST.
  2. Update height.
  3. Calculate balance factor.
  4. Perform necessary rotations.

Possible cases:

  • LL
  • RR
  • LR
  • RL

may occur after deletion.


Traversals

AVL Tree supports all BST traversals.


Inorder

Left → Root → Right

Produces sorted order.


Preorder

Root → Left → Right

Postorder

Left → Right → Root

Level Order

Breadth First Traversal

AVL Tree Example

Insert:

10
20
30
40
50
25

Final AVL Tree:

         30
        /  \
      20    40
     / \      \
   10  25     50

Balanced Tree.


Time Complexity Analysis

OperationBST Worst CaseAVL Tree
SearchO(n)O(log n)
InsertO(n)O(log n)
DeleteO(n)O(log n)

Space Complexity

Storage Requirement:

O(n)

Additional height field:

O(1)

per node.


Advantages of AVL Tree

Faster Searching

Guaranteed:

O(log n)

Self-Balancing

No manual balancing required.

Predictable Performance

Avoids skewed BST structures.

Efficient Range Queries

Supports ordered traversal efficiently.


Disadvantages of AVL Tree

Complex Implementation

Rotations increase implementation complexity.

Extra Memory

Each node stores height information.

More Rotations

Compared to Red-Black Trees.


AVL Tree vs Binary Search Tree

FeatureBSTAVL
BalancedNoYes
SearchO(n) WorstO(log n)
InsertO(n) WorstO(log n)
DeleteO(n) WorstO(log n)
Rotations RequiredNoYes

AVL Tree vs Red-Black Tree

FeatureAVLRed-Black
Search PerformanceBetterSlightly Slower
Insert/DeleteMore RotationsFewer Rotations
BalanceStrictLess Strict
ImplementationMore ComplexSimpler

Applications of AVL Trees

Database Indexing

Efficient searching and retrieval.


In-Memory Lookup Tables

Fast access to sorted data.


Compiler Symbol Tables

Store identifiers efficiently.


Routing Tables

Network routing systems use balanced trees.


Dictionary Implementations

Efficient key-value lookups.


Interview Questions

Easy

  1. What is an AVL Tree?
  2. What is Balance Factor?
  3. Why do AVL Trees need rotations?
  4. Difference between BST and AVL?

Medium

  1. Explain LL, RR, LR, RL rotations.
  2. Insert a node in AVL Tree.
  3. Delete a node in AVL Tree.
  4. Calculate Balance Factor.

Advanced

  1. AVL vs Red-Black Tree.
  2. Implement AVL Tree from scratch.
  3. Analyze AVL Tree height mathematically.
  4. Prove AVL Tree operations are O(log n).

Summary

AVL Tree is a self-balancing Binary Search Tree that maintains balance using rotations.

Key Formula

Balance Factor =
Height(Left) - Height(Right)

Balanced Values

-1
0
+1

Rotations

CaseRotation
LLRight Rotation
RRLeft Rotation
LRLeft + Right
RLRight + Left

Complexity

OperationComplexity
SearchO(log n)
InsertO(log n)
DeleteO(log n)

AVL Trees provide guaranteed logarithmic performance and are one of the most important self-balancing tree structures in Data Structures and Algorithms.