AVL Trees
A BST that balances itself with rotationsA plain BST can turn into a long line and become slow. An AVL tree checks the balance factor of every node on the way back up after an insert or delete, and when one becomes ±2 it rotates a few links to make the tree short again. Every operation stays O(log n).
- Balance factorheight(left) − height(right)
- Ruleevery balance is −1, 0 or +1
- Fixrotate: LL, RR, LR, RL
- Current
- Path / y, x
- Unbalanced
- New / fixed
- +1balance = height(left) − height(right)
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
Every operation, in plain steps
These are exactly the steps the visualizer animates.
What does an AVL node store?
The same as a BST node, plus its height. Here a leaf has height 1 and NULL has height 0. From the heights of the two children we get the balance factor = height(left) − height(right). In an AVL tree it must always be −1, 0 or +1 — the small badge on every node in the visualizer shows it.
struct Node {
int key;
struct Node *left, *right;
int height; // leaf = 1
};
int height(struct Node *n) {
return n == NULL ? 0 : n->height;
}
int balance(struct Node *n) {
return height(n->left) - height(n->right);
}
Balance factor
- balance = height(left) − height(right).
- +1: left side is one level taller.
- −1: right side is one level taller.
- ±2: too unbalanced — rotate!
Insert
- Insert like a normal BST (a new leaf).
- Walk back up to the root.
- Update each node's height and balance.
- At most ONE rotation (single or double) fixes it.
LL and RR: one rotation
- LL: the extra height is left of the left child.
- Rotate RIGHT at the unbalanced node z.
- RR is the mirror image: rotate LEFT.
- Only 3 links change — O(1).
LR and RL: two rotations
- LR: extra height is right of the left child (a zig-zag).
- First rotate LEFT at the child y — now it is LL.
- Then rotate RIGHT at z.
- RL is the mirror image.
Delete
- Delete like a normal BST.
- Walk back up, updating heights and balances.
- Pick the case from the CHILD's balance.
- Unlike insert, several rotations may be needed.
Build tree
- Start with an empty tree.
- Insert the typed values one by one, in that order.
- An insert rotates only when a balance becomes ±2.
- Compare the height with a plain BST built the same way.
Try each rotation (Rotate tab)
- Pick LL, RR, LR or RL.
- A small unbalanced tree appears, with the red ±2.
- Follow z, y and x, and the subtree that moves across.
- The sorted (inorder) order never changes.
Why it is worth it
- Height stays below about 1.44 · log₂ n.
- Search, insert and delete are all O(log n)…
- …even for sorted input (try the Sorted button).
- Rotations never change the sorted (inorder) order.
Pseudocode
Language-independent steps. The visualizer shows the same logic in C, Python and Java.
How fast is each operation?
n = number of nodes. An AVL tree's height is always O(log n), so no operation ever degrades to O(n) like a skewed BST. A single rotation is O(1). Building a tree of n values is n inserts, so O(n log n).
| Operation | Best | Average | Worst | Speed |
|---|---|---|---|---|
| Search | O(1) |
O(log n) |
O(log n) |
Grows with n |
| Insert | O(log n) |
O(log n) |
O(log n) |
Grows with n |
| Delete | O(log n) |
O(log n) |
O(log n) |
Grows with n |
| One rotation | O(1) |
O(1) |
O(1) |
Instant |
| Build tree (n values) | O(n log n) |
O(n log n) |
O(n log n) |
Grows with n |
The four cases
| Case | Unbalanced node z | Where the extra height is | Fix |
|---|---|---|---|
| LL | balance +2 | Left child's LEFT side | Right rotation at z |
| RR | balance −2 | Right child's RIGHT side | Left rotation at z |
| LR | balance +2 | Left child's RIGHT side | Left rotation at y, then right rotation at z |
| RL | balance −2 | Right child's LEFT side | Right rotation at y, then left rotation at z |
BST vs AVL tree
| Plain BST | AVL tree | |
|---|---|---|
| Height | Between log n and n | Always about log n |
| Search / insert / delete (worst) | O(n) | O(log n) |
| Sorted input 10, 20, 30… | Becomes a straight line | Rotates and stays short |
| Extra memory | None | One height per node |
| Extra work | None | Height updates + at most a few rotations |
Check your understanding
Answer each question — you will see the explanation straight away.
A node's left subtree has height 3 and its right subtree has height 1. What is its balance factor?
balance = height(left) − height(right) = 3 − 1 = +2, so this node must be rebalanced.
You insert 30, 20, 10 into an empty AVL tree. Which case happens, and what is the new root?
30 gets balance +2 with the extra height on the left of its left child — case LL. A right rotation makes 20 the root.
You insert 30, 10, 20 into an empty AVL tree. Which case is it?
30 is left-heavy, but 20 is the RIGHT child of its left child 10 — a zig-zag. Rotate left at 10, then right at 30.
Does a rotation change the inorder (sorted) order of the values?
A rotation only changes which node is the parent. Everything left of a node stays smaller and everything right stays larger.
What is the worst-case time to search an AVL tree with n nodes?
The height of an AVL tree is always O(log n), and a search follows one path from the root.
After an INSERT, how many places may need rebalancing?
After one fix the subtree gets back its old height, so nothing above it changes. A delete, however, may need several rotations.
Trees quiz
Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.