AVL tree visualizer: insert, delete and watch every rotation
An AVL tree is a binary search tree that rebalances itself after every insertion and deletion, so its height stays within about 1.44 log₂ n. Type keys into the visualiser to watch each comparison, every balance factor that changes, and the left, right and double rotations that fix the tree.
On this page
AVL tree visualiserInsert, delete or search for a key, then step through every comparison and rotation.
- Current node
- Search path
- Unbalanced (|balance| = 2)
- Just rotated
- Inserted or found
The small number beside each node is its balance factor: height of the left subtree minus height of the right.
Pseudocode
Tree
In-order traversal
—
What an AVL tree is
A binary search tree keeps every key in a node’s left subtree smaller than the node and every key in its right subtree larger. Searching, inserting and deleting all walk one path from the root, so they cost time proportional to the tree’s height. The catch is that the height depends on the order the keys arrive in. Insert 1, 2, 3, …, 15 into a plain binary search tree and you get a chain of height 15, no better than a linked list.
The AVL tree, published by Georgy Adelson-Velsky and Evgenii Landis in 1962 and named after them, was the first self-balancing binary search tree. It adds one rule:
The difference is the node’s balance factor,
where the height of an empty subtree is 0 and a leaf has height 1. (Some books count a leaf as height 0 and an empty tree as −1. Balance factors come out the same either way.) The visualiser prints each node’s balance factor beside it. A positive value means the node leans left, a negative value means it leans right.
The four rotation cases
An insertion or deletion changes heights only along the path it walked. On the way back up that path, each node’s height is recomputed. The first node whose balance factor reaches +2 or −2 is repaired with a rotation: a constant-time relinking of two or three nodes that keeps the in-order sequence of keys unchanged but moves height from the heavy side to the light side.
Call the unbalanced node . Which rotation it needs depends on where the extra height is:
| Case | Shape | Fix | Try it |
|---|---|---|---|
| Left-left (LL) | has balance +2 and its left child leans left or is level | One right rotation at | 30, 20, 10 |
| Right-right (RR) | has balance −2 and its right child leans right or is level | One left rotation at | 10, 20, 30 |
| Left-right (LR) | has balance +2 and its left child leans right | Left rotation at the left child, then right rotation at | 30, 10, 20 |
| Right-left (RL) | has balance −2 and its right child leans left | Right rotation at the right child, then left rotation at | 10, 30, 20 |
Pick each case from the visualiser’s example list to see it happen. In a right rotation at , its left child moves up and becomes ’s right child. ’s old right subtree, whose keys lie between and , is reattached as ’s new left subtree. Three links change and nothing else does. A left rotation is the mirror image.
The double rotations exist because a single rotation can’t fix a zig-zag. If the extra height is in the inner grandchild (left child’s right subtree), a right rotation at just moves that grandchild to the other side and leaves the tree unbalanced the other way. Rotating the child first turns the zig-zag into a straight line, which one more rotation fixes. The middle key of the three ends up on top.
Insertion, step by step
- Walk down from the root as in any binary search tree and attach the new key as a leaf.
- Walk back up the same path, recomputing each node’s height as .
- At the first node with balance ±2, apply the matching rotation. After an insertion, one rotation (single or double) always restores the subtree to the height it had before the insertion, so nothing above it changes and the insertion is finished.
Worked example: insert 10, 20, 30, 40, 50, 25
- 10, 20, 30. After 30, node 10 has an empty left subtree and a right subtree of height 2: balance −2, right-right case. A left rotation at 10 makes 20 the root with children 10 and 30.
- 40. It goes right of 30. Balance factors along the path are −1 (30) and −1 (20). No rotation.
- 50. It goes right of 40. Now 30 has balance −2, another right-right case. A left rotation at 30 lifts 40, giving 20 with children 10 and 40, and 40 with children 30 and 50.
- 25. Walk 20 → 40 → 30 and attach 25 as 30’s left child. Back at 20, the left subtree has height 1 and the right height 3: balance −2. The new key went into the right child’s left subtree, so this is the right-left case. Rotate right at 40, then left at 20.
- Result: 30 is the root. In level order the tree is 30, 20, 40, 10, 25, 50, its height is 3, and it took 4 rotations in total (two singles and one double).
This is the visualiser’s starting example: press play to watch it, or use the arrow keys to step.
Deletion
Deletion starts like deletion from a plain binary search tree. A leaf is removed, a node with one child is replaced by that child, and a node with two children takes the key of its in-order successor (the smallest key in its right subtree), which is then deleted from the right subtree instead. That successor has at most one child, so the second deletion is easy.
Then, as with insertion, walk back up recomputing heights and rotating where a balance factor hits ±2. There are two differences:
- A deletion can leave the sibling subtree perfectly level (balance 0). That case is treated as a single rotation (LL or RR), which is why the delete pseudocode tests
balance(left) ≥ 0rather than comparing keys. - A rotation after a deletion can make the subtree one level shorter than it was, which may unbalance an ancestor. So deletion may rotate at several nodes on the way up, at most one per level, which is still .
Try the “bigger tree to delete from” example and delete 80, then 75: the first deletion triggers a rotation at the root.
Why the height stays logarithmic
Let be the fewest nodes an AVL tree of height can have. The sparsest such tree has a root, one subtree of height and, to use as few nodes as possible, the other of height . Both subtrees must themselves be sparsest, so
| Height h | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Fewest nodes N(h) | 1 | 2 | 4 | 7 | 12 | 20 | 33 | 54 | 88 | 143 |
Adding 1 to both sides gives , the Fibonacci recurrence, so . Fibonacci numbers grow like with , and solving for gives
So an AVL tree is never more than about 44% taller than a perfectly balanced tree. With a million keys the worst possible AVL height is 28, against 20 for a perfect tree, and a search needs at most that many comparisons. Inserting the sorted keys 1 to 1,023 gives a tree of height 10, the minimum possible, after 1,013 rotations; a plain binary search tree would have height 1,023.
These numbers also answer two common exam questions: “What is the minimum number of nodes in an AVL tree of height 5?” (12) and “What is the maximum height of an AVL tree with 7 nodes?” (4, since but ).
Running time
| Operation | Plain BST (worst case) | AVL tree (worst case) | Rotations per operation |
|---|---|---|---|
| Search | 0 | ||
| Insert | At most 1 (single or double) | ||
| Delete | Up to | ||
| In-order traversal | 0 |
Each node stores its height (or just its balance factor, which needs two bits), so the space overhead is per node. See Big-O notation for what these bounds mean, and binary search for the same halving idea on a sorted array.
AVL trees compared with red-black trees
Red-black trees are the other classic balanced search tree, and the one most standard libraries use for ordered maps and sets. The trade-off is simple:
- AVL trees are more tightly balanced (height at most about 1.44 log₂ n against 2 log₂ n for red-black trees), so lookups are slightly faster. They suit read-heavy workloads such as in-memory indexes.
- Red-black trees rotate less on updates: at most two rotations per insertion and three per deletion, against a possible rotation at every level of an AVL deletion. They suit write-heavy workloads.
Both guarantee for every operation. For data on disk, B-trees beat both, because they keep many keys per node and so touch far fewer disk blocks. A hash table is faster still for exact lookups, but it can’t answer ordered questions such as “the smallest key above 40” or list keys in order.
Using the visualiser
- Insert, Delete, Search act on the current tree and play the steps at once. Pause, then use the arrow keys, Home and End, or drag the slider to move through them.
- Build from empty inserts a whole sequence, one key at a time, so you can replay every rotation. Up to 40 keys, each a whole number from −9999 to 9999.
- Plain BST height is the height an ordinary binary search tree would have after the same insertions, which shows what the rotations saved.
- The address bar updates as you go, so you can copy the link to share the current tree. It stores the keys in level order, which rebuilds exactly the same tree.
References
- G. M. Adelson-Velsky and E. M. Landis, “An algorithm for the organization of information”, Soviet Mathematics Doklady 3 (1962), 1259–1263.
- D. E. Knuth, The Art of Computer Programming, Volume 3: Sorting and Searching, 2nd ed., Addison-Wesley, 1998. Section 6.2.3, balanced trees.
- T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein, Introduction to Algorithms, 4th ed., MIT Press, 2022. Chapter 13 and its AVL tree problem.
- R. Sedgewick and K. Wayne, Algorithms, 4th ed., Addison-Wesley, 2011. Section 3.3, balanced search trees.