An AVL tree is a self-balancing balanced binary tree used in data structures and data structures and algorithms to maintain efficient search, insertion, and deletion operations. The tree stays balanced by checking the balance factor in AVL tree after every update. If a node becomes unbalanced, the tree performs one or more rotations to restore balance.

Balancing ensures the height of the tree remains close to O(log n), allowing search, insertion, and deletion to run efficiently even with large datasets.
An AVL tree is a special type of Binary Search Tree (BST) introduced by Georgy Adelson-Velsky and Evgenii Landis in 1962. Unlike a regular BST, it automatically maintains balance after every insertion or deletion.
The key rule is that the height difference between the left and right subtree of every node must never exceed 1.
AVL Tree Properties
It is a Binary Search Tree.
It is a self-balancing tree.
The balance factor of every node must be -1, 0, or +1.
Search operations remain fast because the tree height stays minimal.
What Is the Balance Factor in AVL Tree?
The balance factor in AVL tree determines whether a node is balanced.
Formula
Balance Factor = Height of Left Subtree − Height of Right SubtreeBalance Factor | Status |
|---|---|
-1 | Balanced |
0 | Balanced |
+1 | Balanced |
Less than -1 | Right Heavy |
Greater than +1 | Left Heavy |
Whenever a node's balance factor becomes 2 or −2, the tree must be balanced using rotations.
How to Balance an AVL Tree?
AVL tree balancing is performed using tree rotations whenever an insertion or deletion makes the tree unbalanced.
The balancing process follows these steps:
Insert or delete a node.
Update the height of each ancestor node.
Calculate the balance factor.
Detect the imbalance type.
Perform the appropriate rotation.
Update node heights again.
This process restores the balanced structure without changing the Binary Search Tree property.
Types of AVL Tree Rotations
There are four balancing cases.
Imbalance | Rotation Used |
|---|---|
Left Left (LL) | Single Right Rotation |
Right Right (RR) | Single Left Rotation |
Left Right (LR) | Double Rotation |
Right Left (RL) | Double Rotation |
What Is Single Rotation?
A Single Rotation fixes an imbalance when the extra node is inserted in a straight line.
There are two types:
Right Rotation (LL Case)
Left Rotation (RR Case)
LL Rotation (Single Right Rotation)
Before Rotation
30
/
20
/
10After inserting 10, node 30 becomes left-heavy.
After Rotation
20
/ \
10 30Only one right rotation is required.
RR Rotation (Single Left Rotation)
Before Rotation
10
\
20
\
30Node 10 becomes right-heavy.
After Rotation
20
/ \
10 30Only one left rotation restores balance.
Difference Between Single and Double Rotations
Feature | Single Rotation | Double Rotation |
|---|---|---|
Number of Rotations | One | Two |
Cases | LL and RR | LR and RL |
Tree Shape | Straight line | Zig-zag |
Complexity | O(1) | O(1) |
Operations | One left or right rotation | Combination of left and right rotations |
Purpose | Fix simple imbalance | Fix complex imbalance |
AVL Tree Insertion
During AVL tree insertion, every newly inserted node is first placed like a normal Binary Search Tree node.
After insertion:
Heights are updated.
Balance factors are calculated.
If necessary, rotations restore balance.
This automatic balancing keeps the tree efficient regardless of insertion order.
AVL Tree Example with Solution
Consider inserting:
40, 20, 60, 10, 30, 25After inserting 25, the tree becomes unbalanced due to an LR case.
Before Balancing
40
/
20
/ \
10 30
/
25Perform:
Left Rotation on 20's right child.
Right Rotation on 40.
Balanced Tree
30
/ \
20 40
/ \ \
10 25 60This AVL tree example with solution shows how double rotations maintain optimal height.
Time Complexity
Operation | Time Complexity |
|---|---|
Search | O(log n) |
Insert | O(log n) |
Delete | O(log n) |
Rotation | O(1) |
Because the tree height remains logarithmic, AVL trees provide consistently fast performance.
Why AVL Trees Are Preferred
AVL trees offer several advantages over ordinary Binary Search Trees:
Guaranteed logarithmic search time.
Faster lookup in read-heavy applications.
Automatic balancing after updates.
Efficient memory organization.
Predictable performance for large datasets.
These characteristics make AVL trees widely used in databases, indexing systems, memory management, and compiler implementations.
Frequently Asked Questions
Why is an AVL tree called a balanced binary tree?
It is called a balanced binary tree because the height difference between the left and right subtrees of every node is restricted to at most one, ensuring efficient operations.
What is the balance factor in an AVL tree?
The balance factor is calculated as:
Height of Left Subtree − Height of Right Subtree
A node is balanced if its balance factor is -1, 0, or +1.
When is a single rotation used?
A single rotation is used in LL and RR imbalance cases where the inserted node follows a straight-line path.
Also read : What are AVL Tree Rotations?

