Introduction
Related material. The GATE 2027 CS syllabus names binary search trees but not AVL trees. AVL trees are included because they are the standard way to keep a binary search tree balanced.
Search, insertion and deletion in a binary search tree take time proportional to the tree's height . Inserting keys in sorted order builds a path of height , so an unbalanced BST can degrade to per operation. A balanced BST adds a structural rule and repairs any violation after each update, keeping for every insertion order.
The AVL tree (Adelson-Velsky and Landis, 1962) was the first such scheme. Its rule is simple to state and to check, and its repair operations, rotations, are the same ones used by other balanced trees. This note covers the balance condition, the four rotation cases for insertion, deletion, the height bound through minimum-node (Fibonacci) trees, and a brief comparison with other balanced structures.
Conventions: heights follow Trees and Binary Trees: an empty tree has height , a single node height 0. Keys are distinct. The rotation code uses the pointer fields left and right; the balance factor is .
Prerequisites
- Binary Search Trees: search, insertion, deletion, inorder order.
- Trees and Binary Trees: height, node counts.
Core concepts
The AVL condition
For every node , define the balance factor
A BST is an AVL tree if for every node. Each node stores its height (or its balance factor), updated on the way back up from an insertion or deletion.
Checking only the root is not enough: a tree whose root has two subtrees of equal height can still contain an unbalanced node deeper down.
Rotations
A rotation rearranges three links around an edge while preserving inorder order (so the tree is still a BST), and changes the heights of the subtrees involved.
right rotation at y left rotation at x
y x x y
/ \ / \ / \ / \
x C ==> A y A y ==> x C
/ \ / \ / \ / \
A B B C B C A BInorder in both shapes is : the subtree changes parent, nothing else moves. A rotation uses time and workspace.
struct avl { int key, h; struct avl *left, *right; };
static int ht(const struct avl *t) { return t ? t->h : -1; }
static void fix(struct avl *t) { int a = ht(t->left), b = ht(t->right); t->h = 1 + (a > b ? a : b); }
struct avl *rot_right(struct avl *y) {
struct avl *x = y->left;
y->left = x->right; /* B moves from x to y */
x->right = y;
fix(y); fix(x); /* y is now below x: update y first */
return x; /* new root of this subtree */
}
/* rot_left is the mirror image */How it works: insertion
Insert as in an ordinary BST, then walk back up the path to the root, updating heights. The first node found with is repaired by one of four cases, named by the path from to the newly inserted key:
| Case | New key is in | Repair |
|---|---|---|
| LL | left subtree of 's left child | single right rotation at |
| RR | right subtree of 's right child | single left rotation at |
| LR | right subtree of 's left child | left rotation at the left child, then right rotation at |
| RL | left subtree of 's right child | right rotation at the right child, then left rotation at |

At most one repair per insertion. After the repair, the subtree rooted where was has the same height it had before the insertion, so no ancestor's balance factor changes and the walk can stop. An insertion therefore needs at most one single or one double rotation, plus height updates.
In terms of balance factors: at with , the case is LL if and LR if ; symmetrically for .
Deletion
Delete as in an ordinary BST (a node with two children is replaced by its inorder successor or predecessor), then walk up to the root, rebalancing every node with . Deletion uses the same four rotations, with one extra situation: if the taller child of has balance factor 0, use the single rotation (LL or RR). Unlike insertion, a rotation after deletion can reduce the subtree's height, so imbalance can propagate upward; a deletion may need rotations at up to levels. The total cost is still .
The height bound
Let be the minimum number of nodes in an AVL tree of height . The sparsest such tree has a root, one subtree of height and one of height , each as sparse as possible:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|---|
| 1 | 2 | 4 | 7 | 12 | 20 | 33 |
These extremal trees are the Fibonacci trees, and , where . Since grows like with the golden ratio , an AVL tree with nodes has height
so every operation is . The maximum number of nodes at height is (a perfect tree), as for any binary tree.
Using the table. The maximum height of an AVL tree with nodes is the largest with . With 20 nodes the maximum height is 5; with 12 nodes it is 4; with 7 nodes it is 3.
Worked examples
Each tree below is the result of inserting or deleting the given keys, preserving the AVL condition and BST order after every operation.
Example 1. Insert 10, 20, 30, 40, 50, 25 into an empty AVL tree.
- 10, 20, 30: node 10 has bf , case RR: left rotation at 10. Tree: 20 (10, 30).
- 40, 50: node 30 has bf , case RR: left rotation at 30. Tree: 20 (10, 40 (30, 50)).
- 25 goes left of 30. Node 20 has bf , its right child 40 has bf : case RL. Right rotation at 40, then left rotation at 20.
Final tree, preorder 30 20 10 25 40 50: root 30, left child 20 (children 10 and 25), right child 40 (right child 50). Height 2.
Example 2. Inserting 1, 2, ..., 7 in increasing order performs four single left rotations and ends with the perfect tree of height 2, preorder 4 2 1 3 6 5 7. The same insertions into an ordinary BST give a path of height 6.
Example 3. Insert 50, 20, 60, 10, 30, 40. Adding 40 (right of 30) makes 50 have bf with left child 20 at bf : case LR. Result, preorder 30 20 10 50 40 60.
Example 4. Minimum nodes for height 4: .
Other balanced search trees (orientation only)
| Structure | Balance rule | Height bound |
|---|---|---|
| AVL | at every node | about |
| Red-black tree | colouring rules; every root-to-leaf path has the same number of black nodes | at most |
| B-tree, B+ tree | all leaves at the same depth; nodes hold many keys | for order |
AVL trees are more rigidly balanced (faster searches); red-black trees need fewer rotations on updates. B-trees suit disks and are covered with databases (B-trees and B+ trees).
Important properties and observations
- Balance factor in at every node; heights are stored and updated bottom-up.
- Rotations preserve inorder order and take .
- Insertion: at most one single or double rotation. Deletion: possibly one per level.
- Minimum nodes ; maximum height for nodes from that table.
- Search, insert and delete are in the worst case. In the unit-cost pointer/key model, successful search has best (root), and average for a uniformly chosen stored key. For , inserting a new distinct key and deleting by key using the stated successor/predecessor procedure each take time in the best, average and worst cases; the average bound holds under any distribution over these operations. Even the shortest root-to-leaf path in an AVL tree has length : height drops by at most two per edge. Insertion reaches a missing child; deletion searches to a node with at most one child, directly or through its successor/predecessor. Empty-tree operations take . Iterative parent-pointer updates use workspace; recursive or stored-path versions use stack space.
GATE examination perspective
- Insert a sequence into an empty AVL tree and give the final tree, its root, its height or a traversal.
- Identify the rotation needed (LL, RR, LR, RL) after a given insertion.
- Minimum number of nodes in an AVL tree of height , or the maximum height with nodes; check the height convention in the question (some count the single node as height 1, which shifts the table by one).
- Worst-case complexity of search, insertion and deletion compared with an unbalanced BST.
Summary
Sources
- D. E. Knuth, The Art of Computer Programming, Vol. 3, 2nd ed., Addison-Wesley, 1998: Section 6.2.3 (balanced trees: AVL trees, the height bound via Fibonacci trees).
- T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009: Section 13.2 (rotations), Lemma 13.1 (red-black tree height), Problem 13-3 (AVL trees).