AVL Trees
A BST can degenerate into a linked list. An AVL tree prevents this by detecting imbalance after every modification and fixing it with a rotation — a constant-time structural rearrangement. Result: O(log n) guaranteed, always.
What is an AVL tree?
An AVL tree is a self-balancing binary search tree. For every node, the heights of its left and right subtrees differ by at most 1. After each insert or delete, it fixes any imbalance with rotations, so search, insert and delete are guaranteed O(log n). It's stricter than a red-black tree, so lookups are slightly faster but updates do more rotations.
// balance factor = height(left) - height(right); must be -1, 0 or 1
function rotateRight(y) {
const x = y.left;
y.left = x.right;
x.right = y;
return x; // x is the new subtree root
}The balance factor
Every node stores its balance factor = height(left subtree) − height(right subtree). An AVL tree maintains the invariant that every node's balance factor is in {-1, 0, +1}.
Perfectly balanced AVL tree — all balance factors are 0
20 (root)
bf = 0
10, 30
bf = 0
5, 15, 25, 35
bf = 0 (leaves)
All balance factors are 0 — a perfectly balanced AVL tree. Height = 2 for 7 nodes.
The 4 rotation cases
Detect imbalance
After every insert or delete, recalculate the balance factor at each node: balance = height(left) - height(right). If |balance| > 1, a rotation is needed.
Live demo — BST shape explorer
Observe how different insertion orders produce different tree shapes. In an AVL tree, rotations would rebalance automatically to keep height at O(log n).
Tree is empty — insert a value or click a preset above
→ Insert values to build your BST
Rotation pseudocode
1rotateRight(y): // y is unbalanced, x is left child2 x <- y.left3 T2 <- x.right // save x's right subtree4 x.right <- y // x's right is now y5 y.left <- T2 // y's left is T26 updateHeight(y) // update in bottom-up order7 updateHeight(x)8 return x // x is the new root910rotateLeft(x): // mirror of rotateRight11 y <- x.right12 T2 <- y.left13 y.left <- x14 x.right <- T215 updateHeight(x)16 updateHeight(y)17 return y1819balance(node): // called after every insert/delete20 bf <- balanceFactor(node) // height(left) - height(right)21 if bf > 1: // left-heavy22 if balanceFactor(node.left) < 0:23 node.left <- rotateLeft(node.left) // LR case24 return rotateRight(node) // LL case25 if bf < -1: // right-heavy26 if balanceFactor(node.right) > 0:27 node.right <- rotateRight(node.right) // RL case28 return rotateLeft(node) // RR case29 return node // already balanced
AVL vs BST vs Red-Black
Plain BST
+ Simplest to implement
− O(n) worst case with sorted input
AVL Tree
+ Strictest balance — height never exceeds 1.44 log n. Fastest lookups.
− More rotations on insert/delete than Red-Black
Red-Black Tree
+ Fewer rotations — better insert/delete throughput. Used in most std libraries.
− Slightly taller than AVL (height up to 2 log n)
Time complexity
Logarithmic time
Logarithmic time
Logarithmic time
Constant time
🎯 Quick Check
You insert a node and find a node with balance factor +2 and its left child has balance factor +1. Which rotation fixes this?
AVL Trees: summary
Key points
- Four imbalance cases: LL, RR (single rotation), and LR, RL (double rotation).
- Each node stores its height (or balance factor) to detect imbalance in O(1).
- Height is always under about 1.44 log₂ n.
Frequently asked questions
›AVL tree vs red-black tree?
AVL trees are more strictly balanced, which makes lookups faster. Red-black trees rotate less, which makes inserts and deletes cheaper; that's why most standard libraries use red-black trees.