Skip to main content

AVL Tree

Status

This note is complete, reviewed, and considered stable.

An AVL Tree (Adelson-Velsky and Landis Tree) is a self-balancing Binary Search Tree (BST) in which the height difference between the left and right subtrees of every node is at most 1.

AVL Trees automatically perform rotations after insertions and deletions to maintain balance, ensuring that search, insertion, and deletion operations remain efficient.

Why Do We Need AVL Trees?​

A regular BST can become skewed depending on the insertion order.

Example​

Insert:

10, 20, 30, 40, 50

Resulting BST:

The tree behaves like a linked list.

Height = O(n)
Search = O(n)
Insert = O(n)
Delete = O(n)

AVL Trees prevent this degeneration by maintaining balance.

Balanced AVL Tree:

Height ≈ O(log n)

AVL Tree Properties​

Every AVL Tree must satisfy:

Binary Search Tree Property​

For every node:

Left Subtree < Node < Right Subtree

Example:

Balance Property​

For every node:

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

Height of a Node​

The height of a node is the number of edges in the longest path from that node to a leaf.

Example:

Heights:

10 → 0
20 → 1
40 → 0
30 → 2

Balance Factor​

The balance factor determines whether a node is balanced.

Formula​

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

Possible balanced values:

-1, 0, +1

Unbalanced:

<-1 or >+1

Example​

BF(30) = 0
BF(30) = +1
BF(30) = -1
BF(30) = +2

Node 30 is unbalanced.

Rotations​

AVL Trees use rotations to restore balance.

There are four possible imbalance cases:

  1. Left-Left (LL)
  2. Right-Right (RR)
  3. Left-Right (LR)
  4. Right-Left (RL)

Left-Left (LL) Case​

Occurs when:

Node becomes left-heavy
Insertion occurs in left subtree of left child

Example:

Insert:

30, 20, 10

Before balancing:

Balance Factor:

BF(30) = +2

Perform a Right Rotation.

After balancing:

Right Rotation​

Before:

After:

Right-Right (RR) Case​

Occurs when:

Node becomes right-heavy
Insertion occurs in right subtree of right child

Example:

Insert:

10, 20, 30

Before balancing:

Balance Factor:

BF(10) = -2

Perform a Left Rotation.

After balancing:

Left Rotation​

Before:

After:

Left-Right (LR) Case​

Occurs when:

Node becomes left-heavy
Insertion occurs in right subtree of left child

Insert:

30, 10, 20

Before balancing:

Step 1: Left Rotation on 10​

Step 2: Right Rotation on 30​

Right-Left (RL) Case​

Occurs when:

Node becomes right-heavy
Insertion occurs in left subtree of right child

Insert:

10, 30, 20

Before balancing:

Step 1: Right Rotation on 30​

Step 2: Left Rotation on 10​

Summary of Rotations​

CaseConditionFix
LLLeft of LeftRight Rotation
RRRight of RightLeft Rotation
LRRight of LeftLeft Rotation + Right Rotation
RLLeft of RightRight Rotation + Left Rotation

AVL Tree Insertion​

Step 1​

Insert the node exactly as in a BST.

Example:

Step 2​

Move upward from the inserted node toward the root.

Update:

Height
Balance Factor

Step 3​

Check for imbalance.

Balance Factor > 1
Balance Factor < -1

Step 4​

Apply the appropriate rotation.

LL → Right Rotation
RR → Left Rotation
LR → Left Rotation + Right Rotation
RL → Right Rotation + Left Rotation

AVL Tree Deletion​

Deletion follows normal BST deletion first.

BST Deletion Cases​

Leaf Node​

Delete:

20

One Child​

Delete:

20

Promote 10.

Two Children​

Replace the node with:

Inorder Successor
or
Inorder Predecessor

Then delete the replacement node.

Rebalancing After Deletion​

Unlike insertion, deletion may cause multiple ancestors to become unbalanced.

Therefore:

Delete Node
Update Heights
Update Balance Factors
Rotate If Needed
Continue Toward Root

AVL Height Analysis​

AVL Trees guarantee logarithmic height.

Minimum number of nodes required for a given height follows:

N(h) = 1 + N(h-1) + N(h-2)

This recurrence is similar to Fibonacci numbers.

Therefore:

Height = O(log n)

More precisely:

Height ≤ 1.44 log₂(n + 2)

Time Complexity​

OperationComplexity
SearchO(log n)
InsertO(log n)
DeleteO(log n)
Find MinimumO(log n)
Find MaximumO(log n)

Space Complexity​

Tree Storage:

O(n)

Extra per node:

Value
Left Pointer
Right Pointer
Height

AVL Node Structure​

Node
├── value
├── left
├── right
└── height

Example:

AVL Tree vs BST​

FeatureBSTAVL
Self BalancingNoYes
Worst HeightO(n)O(log n)
SearchO(n)O(log n)
InsertO(n)O(log n)
DeleteO(n)O(log n)
Extra MemoryNoHeight Field

AVL Tree vs Red-Black Tree​

FeatureAVLRed-Black
BalancingStrictRelaxed
Search SpeedFasterSlightly Slower
RotationsMoreFewer
Insert/DeleteSlowerFaster
Use CaseRead-Heavy SystemsGeneral-Purpose Systems

Advantages​

  • Guaranteed O(log n) search.
  • Prevents skewed trees.
  • Faster lookups than Red-Black Trees.
  • Strict balancing.
  • Predictable performance.

Disadvantages​

  • More complex implementation.
  • Additional memory for height.
  • More rotations during updates.
  • Insertions and deletions can be slower than Red-Black Trees.

Key Takeaways​

  • AVL Tree is a self-balancing Binary Search Tree.
  • Every node maintains a balance factor.
  • Balance Factor = Height(Left) − Height(Right).
  • Allowed balance factors are -1, 0, and +1.
  • Rotations restore balance after insertions and deletions.
  • Four rotation cases exist: LL, RR, LR, and RL.
  • AVL Trees guarantee O(log n) height.
  • Search, insertion, and deletion all run in O(log n) time.