Insert 15, 20, 24, 10, 13, 7, 30, 36, 25 into an empty AVL tree. Name each rotation and give the final preorder.
An AVL tree is a self-balancing binary search tree in which the heights of every node's two subtrees differ by at most one. Four rotations — LL, RR, LR, RL — restore balance after updates, guaranteeing O(log n) search, insertion and deletion.
A plain BST is only as good as its height. Inserting 10, 20, 30, 40, 50 in that order produces a chain of height 4, and every operation degrades to O(n). A balanced BST keeps its height O(log n) no matter what order the keys arrive in. The first such structure was published in 1962 by the Soviet mathematicians G. M. Adelson-Velsky and E. M. Landis, whose initials give the AVL tree its name.
Definition. An AVL tree is a binary search tree in which, at every node, the heights of the left and right subtrees differ by at most 1. Each node stores its height (or directly its balance factor), where the balance factor is BF = height(left subtree) − height(right subtree). Allowed values are +1 (left-heavy), 0 (balanced) and −1 (right-heavy); a node with BF = ±2 violates the AVL property. Whether a leaf's height is taken as 0 or 1 does not matter — BF is a difference of heights, so it is the same under both conventions.
30 (0) 10 (−2) ← violates
/ \ \
(0) 20 40 (−1) 20 (−1)
/ \ \ \
(0) 10 25 (0) 50 (0) 30 (0)A rotation is a local restructuring of two or three nodes that changes subtree heights but keeps the inorder sequence — and therefore the BST property — intact. It moves O(1) pointers. Let A be the critical node (the lowest node whose BF became ±2). The case is named by the first two steps of the path from A towards the newly inserted key.
LL case: key went into the LEFT subtree of A's LEFT child → rotate RIGHT at A
A (+2) B (0)
/ \ / \
(+1) B T3 ==> T1 A (0)
/ \ / \
T1 T2 T2 T3
RR case: key went into the RIGHT subtree of A's RIGHT child → rotate LEFT at A
A (−2) B (0)
/ \ / \
T1 B (−1) ==> (0) A T3
/ \ / \
T2 T3 T1 T2LR case: key went into the RIGHT subtree of A's LEFT child (B has BF −1)
A (+2) A C
/ \ / \ / \
(−1) B T4 ==> C T4 ==> B A
/ \ / \ / \ / \
T1 C B T3 T1 T2 T3 T4
/ \ / \
T2 T3 T1 T2
rotate LEFT at B rotate RIGHT at A
RL case: key went into the LEFT subtree of A's RIGHT child (B has BF +1)
A (−2) A C
/ \ / \ / \
T1 B (+1) ==> T1 C ==> A B
/ \ / \ / \ / \
C T4 T2 B T1 T2 T3 T4
/ \ / \
T2 T3 T3 T4
rotate RIGHT at B rotate LEFT at A| Case | New key is in… | BF of A / of its child | Fix | Rotations |
|---|---|---|---|---|
| LL | left subtree of A's left child | +2 / +1 | single right rotation at A | 1 |
| RR | right subtree of A's right child | −2 / −1 | single left rotation at A | 1 |
| LR | right subtree of A's left child | +2 / −1 | left rotation at the child, then right rotation at A | 2 |
| RL | left subtree of A's right child | −2 / +1 | right rotation at the child, then left rotation at A | 2 |
#include <stdio.h>
#include <stdlib.h>
struct AVL { int key, height; struct AVL *left, *right; };
int h(struct AVL *t) { return t ? t->height : 0; }
int imax(int a, int b) { return a > b ? a : b; }
void update(struct AVL *t) { t->height = 1 + imax(h(t->left), h(t->right)); }
int balance(struct AVL *t) { return h(t->left) - h(t->right); }
struct AVL *rotateRight(struct AVL *a) { /* fixes LL */
struct AVL *b = a->left;
a->left = b->right; /* T2 moves across */
b->right = a;
update(a); update(b); /* a is now below b */
return b; /* new root of this subtree */
}
struct AVL *rotateLeft(struct AVL *a) { /* fixes RR */
struct AVL *b = a->right;
a->right = b->left;
b->left = a;
update(a); update(b);
return b;
}
struct AVL *insert(struct AVL *t, int k) {
if (t == NULL) {
t = malloc(sizeof *t);
if (!t) exit(1);
t->key = k; t->height = 1; t->left = t->right = NULL;
return t;
}
if (k < t->key) t->left = insert(t->left, k);
else if (k > t->key) t->right = insert(t->right, k);
else return t; /* no duplicates */
update(t);
int bf = balance(t);
if (bf > 1 && k < t->left->key) return rotateRight(t); /* LL */
if (bf < -1 && k > t->right->key) return rotateLeft(t); /* RR */
if (bf > 1 && k > t->left->key) { t->left = rotateLeft(t->left); return rotateRight(t); } /* LR */
if (bf < -1 && k < t->right->key) { t->right = rotateRight(t->right); return rotateLeft(t); } /* RL */
return t;
}
void preorder(struct AVL *t) { if (t) { printf("%d ", t->key); preorder(t->left); preorder(t->right); } }
int main(void) {
int keys[] = {10, 20, 30, 40, 50, 25};
struct AVL *root = NULL;
for (int i = 0; i < 6; i++) root = insert(root, keys[i]);
preorder(root); printf("\n"); /* 30 20 10 25 40 50 */
return 0;
}Cost. The descent follows one root-to-leaf path of length O(log n), the height updates retrace the same path, and a rotation is O(1) — so insertion is O(log n) in the worst case. Search is an ordinary BST search, also O(log n) because the height is bounded.
| Insert | Balance check after the BST insertion | Case | Rotation | Tree afterwards |
|---|---|---|---|---|
| 10 | single node | — | none | 10 |
| 20 | BF(10) = −1 — allowed | — | none | 10(−, 20) |
| 30 | BF(10) = −2; its right child 20 has BF −1 | RR | left rotation at 10 | 20(10, 30) |
| 40 | BF(30) = −1, BF(20) = −1 — allowed | — | none | 20(10, 30(−, 40)) |
| 50 | BF(30) = −2 (lowest unbalanced node); right child 40 has BF −1 | RR | left rotation at 30 | 20(10, 40(30, 50)) |
| 25 | path 20 → 40 → 30 → left of 30. BF(30) = +1, BF(40) = +1, BF(20) = 0 − 2 = −2 | RL | right rotation at 40, then left rotation at 20 | 30(20(10, 25), 40(−, 50)) |
20 (−2) 20 30
/ \ / \ / \
10 40 (+1) ==> 10 30 ==> 20 40
/ \ / \ / \ \
30 50 25 40 10 25 50
/ \
25 50
rotate RIGHT at 40 rotate LEFT at 20The final tree has height 2, preorder 30 20 10 25 40 50 and inorder 10 20 25 30 40 50 (still sorted, as every rotation preserves inorder). A plain BST fed the same keys would have height 4. Three rebalancings occurred: two single rotations (RR at 10 and at 30) and one double rotation (RL at 20).
Deletion starts as an ordinary BST deletion (a node with two children is replaced by its inorder successor or predecessor). Then walk up from the parent of the node that was physically removed, updating heights and rebalancing every node whose BF reaches ±2. Because the deletion made one side shorter, the case is chosen by looking at the taller child on the other side: if that child's BF has the same sign as the critical node's or is 0, a single rotation suffices; if it has the opposite sign, a double rotation is needed.
20 (+2 after deleting 30) 10 (−1)
/ \ / \
(0) 10 30 ← deleted ==> 5 20 (+1)
/ \ /
5 15 15How tall can an AVL tree with n nodes get? Turn the question round: what is the fewest nodes N(h) an AVL tree of height h can have? Such a sparsest tree has a root whose one subtree is a sparsest tree of height h − 1 and whose other subtree is a sparsest tree of height h − 2 (any smaller and the root would be unbalanced). That gives a Fibonacci-like recurrence:
| Height h | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| Minimum nodes N(h) | 1 | 2 | 4 | 7 | 12 | 20 | 33 | 54 | 88 |
| Maximum nodes 2^(h+1) − 1 | 1 | 3 | 7 | 15 | 31 | 63 | 127 | 255 | 511 |
Because N(h) grows like φʰ (φ ≈ 1.618, the golden ratio), the height of an AVL tree with n nodes is at most about 1.44 log₂ n — a worst-case search is at most about 44% longer than in a perfectly balanced tree. To find the maximum height for a given n, take the largest h with N(h) ≤ n: 7 nodes → height at most 3; 20 nodes → 5; 100 nodes → 8 (N(8) = 88 ≤ 100 < N(9) = 143).
| Tree | Balance rule | Worst-case height | Rotations per insert / delete | Typical use |
|---|---|---|---|---|
| AVL (Adelson-Velsky, Landis 1962) | sibling subtree heights differ by ≤ 1 | ≈ 1.44 log₂ n | ≤ 1 (single or double) / O(log n) | lookup-heavy in-memory indexes |
| Red–black (Bayer 1972; named by Guibas and Sedgewick 1978) | every node red or black; root black; no red node has a red child; equal black count on every root-to-NULL path | ≤ 2 log₂(n + 1) | ≤ 2 / ≤ 3 | C++ std::map, Java TreeMap, Linux CFS scheduler |
| B-tree (Bayer and McCreight 1970) | multiway: up to m children per node, nodes at least half full, all leaves on the same level | ≈ log over ⌈m/2⌉ of n — very shallow | node splits and merges instead of rotations | database and file-system indexes (B+ trees) |
| Splay tree (Sleator and Tarjan 1985) | no balance information; every accessed node is rotated to the root | O(n) worst, O(log n) amortised | many, amortised O(log n) | caches, recently-used data |
Try the problem first, then reveal the working one step at a time.
Insert 15, 20, 24, 10, 13, 7, 30, 36, 25 into an empty AVL tree. Name each rotation and give the final preorder.
(a) What is the minimum number of nodes in an AVL tree of height 6 (single node = height 0)? (b) What is the maximum height of an AVL tree with 50 nodes?
For each insertion order into an empty AVL tree, name the rotation: (a) 30, 20, 10 (b) 10, 20, 30 (c) 30, 10, 20 (d) 10, 30, 20.
Insert 1, 2, 3, 4, 5, 6, 7 into an empty AVL tree. Draw the final tree and count the rotations.
Is the BST 50(30(20(10, −), 40), 60) an AVL tree? If not, which rotation fixes it and what is the result?
What is the minimum number of nodes in an AVL tree of height 4 and of height 5, counting height in edges? What are the answers if height counts levels?
Insert 50, 20, 60, 10, 8, 15, 32, 46, 11, 48 into an empty AVL tree. Identify each rotation and give the final preorder.
Starting from the worked-example tree 30(20(10, 25), 40(−, 50)), delete 10, then 25, then 20. Show any rotations.
12 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.