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:
- LL Rotation
- RR Rotation
- LR Rotation
- 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
| Case | Rotation |
|---|---|
| LL | Right Rotation |
| RR | Left Rotation |
| LR | Left + Right Rotation |
| RL | Right + 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:
- Insert node like BST.
- Update height.
- Calculate balance factor.
- Detect imbalance.
- Apply appropriate rotation.
Insertion Algorithm
Insert Node
↓
Update Height
↓
Calculate Balance Factor
↓
Identify Case
↓
Perform Rotation
AVL Insertion Complexity
| Operation | Complexity |
|---|---|
| Search | O(log n) |
| Insert | O(log n) |
| Delete | O(log n) |
AVL Tree Deletion
Deletion is more complex than insertion.
Steps:
- Delete node like BST.
- Update height.
- Calculate balance factor.
- 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
| Operation | BST Worst Case | AVL Tree |
|---|---|---|
| Search | O(n) | O(log n) |
| Insert | O(n) | O(log n) |
| Delete | O(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
| Feature | BST | AVL |
|---|---|---|
| Balanced | No | Yes |
| Search | O(n) Worst | O(log n) |
| Insert | O(n) Worst | O(log n) |
| Delete | O(n) Worst | O(log n) |
| Rotations Required | No | Yes |
AVL Tree vs Red-Black Tree
| Feature | AVL | Red-Black |
|---|---|---|
| Search Performance | Better | Slightly Slower |
| Insert/Delete | More Rotations | Fewer Rotations |
| Balance | Strict | Less Strict |
| Implementation | More Complex | Simpler |
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
- What is an AVL Tree?
- What is Balance Factor?
- Why do AVL Trees need rotations?
- Difference between BST and AVL?
Medium
- Explain LL, RR, LR, RL rotations.
- Insert a node in AVL Tree.
- Delete a node in AVL Tree.
- Calculate Balance Factor.
Advanced
- AVL vs Red-Black Tree.
- Implement AVL Tree from scratch.
- Analyze AVL Tree height mathematically.
- 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
| Case | Rotation |
|---|---|
| LL | Right Rotation |
| RR | Left Rotation |
| LR | Left + Right |
| RL | Right + Left |
Complexity
| Operation | Complexity |
|---|---|
| Search | O(log n) |
| Insert | O(log n) |
| Delete | O(log n) |
AVL Trees provide guaranteed logarithmic performance and are one of the most important self-balancing tree structures in Data Structures and Algorithms.