The Balanced-Tree Zoo
AVL, WAVL, weight-balanced and scapegoat trees: which invariant buys what.
Every balanced search tree buys height with an invariant, and the invariants differ in what they cost. AVL trees are the shortest but may rotate all the way up on a deletion; red-black trees rotate at most three times but can be about 40% taller; weight-balanced trees track subtree sizes and get order statistics for free; scapegoat trees store nothing extra at all and now and then rebuild a whole subtree. This lesson lines them up and asks which invariant buys what.
What you’ll learn
- Describe AVL, red-black and WAVL trees by rules on ranks, prove that WAVL trees have height at most , and state their rotation and rebalancing bounds.
- Define weight-balanced BB[] trees, bound their height, and explain why rare rebalancing at heavy nodes suits augmented structures.
- Analyze scapegoat trees: no balance data in the nodes, worst-case search, and amortized updates by partial rebuilding.
- Compare AVL, red-black, WAVL, weight-balanced, scapegoat, treap, zip, splay and B-trees by invariant, height, restructuring and extra space, and choose one for a workload.
One language for balance: ranks
Haeupler, Sen and Tarjan describe balanced binary trees with ranks. Every node has an integer rank , a missing child has rank , and the rank difference of a child of is . A node whose two children have rank differences and is an -node. Different rules on rank differences give different trees:
| Tree | Rank rule | What the rank measures |
|---|---|---|
| AVL | Every node is a 1,1-node or a 1,2-node | The height |
| Red-black | Rank differences 0 or 1, where a 0-child is red; no 0-child has a 0-child; missing children are 1-children | The black height |
| WAVL (weak AVL) | Rank differences 1 or 2, and every leaf has rank 0, that is, is a 1,1-node | Between the height and twice the height |
Let be the fewest nodes in a WAVL tree of rank . A node of rank 0 is a leaf. A node of rank 1 cannot be a leaf, since leaves have rank 0, so it has a child of rank 0. A node of rank cannot have a missing child (that would be a rank difference above 2), so it has two children of rank at least :
By induction : it holds for , and . Applied to the root:
Every rank difference is at least 1, so ranks strictly decrease along every downward path and the deepest leaf has rank 0. The height is at most the root's rank:
Why it matters: a red-black tree can be tall even without deletions, while a WAVL tree built by insertions alone is an AVL tree, at most about tall, and deletions erode that bound only gradually, never beyond . Where an AVL deletion may rotate at every level, and alternating insertions and deletions can force rotations per deletion again and again (Amani, Lai & Tarjan, 2015), a WAVL deletion rotates at most twice. The price: deletions can leave 2,2-nodes behind, so the tree is no longer an AVL tree.
Which kind of node does a WAVL tree allow but an AVL tree forbid?
- A 1,1-node
- A 1,2-node
- A 2,2-node
- A 0,1-node
Show the answer
C. A 2,2-node
Both allow 1,1- and 1,2-nodes. WAVL trees also allow 2,2-nodes, which deletions create and which let deletion stop rebalancing early. Rank difference 0 is allowed in neither; it is how the red-black rule encodes a red child.
Weight balance: BB[α] trees
Each step down keeps at most a fraction of the weight, the root has weight , and every node has weight at least 2:
So every depth, and the height, satisfies
With , the largest value for which single and double rotations always restore balance, :
Single and double rotations restore the balance condition after an update when , with amortized rotations per update (Blum & Mehlhorn, 1980). For augmentation there is a stronger promise, a standard consequence of the balance condition: once a node of weight has been rebalanced, updates must happen below it before it needs rebalancing again. So even if rebalancing a node costs time proportional to its weight, as when every node carries a secondary structure built over its whole subtree, the amortized cost is per level, per update.
Weight balance also keeps subtree sizes, so select and rank come for free. Haskell's Data.Map and Data.Set are weight-balanced trees, in a variant due to Adams; Hirai and Yamamoto (2011) used the Coq proof assistant to pin down which integer parameters keep such trees balanced after every operation, after finding that several published implementations could lose balance on deletion. Jugé (2025) strengthened the condition to compare each node with its grandchildren as well, which keeps the height below and lowers node depths by a few percent at no extra cost.
Scapegoat trees: rebuild instead of rotate
A scapegoat tree (Andersson, 1989, as general balanced trees; Galperin & Rivest, 1993) keeps no balance information in its nodes at all. The tree stores two counters, its size and , the largest size since the last full rebuild, and uses a parameter . Updates are plain BST updates followed by a check:
function INSERT(T, key):
insert key as a new leaf z, as in a plain BST; n ← n + 1; q ← max(q, n)
if depth(z) > ⌊log_{1/α} q⌋: # z is too deep
walk up from z to the first ancestor u with
size(child of u toward z) > α · size(u) # the scapegoat
rebuild the subtree of u into a perfectly balanced tree # O(size(u))
function DELETE(T, key):
delete key as in a plain BST; n ← n − 1
if n < α · q:
rebuild the whole tree; q ← nSizes are computed by walking the subtrees on the way up, which costs no more than the rebuild they lead to. Rebuilding flattens the subtree into a sorted list by an inorder walk and builds a perfectly balanced tree from the middle outward.
A scapegoat exists. If every ancestor of the new node had , sizes would shrink by a factor per level down the path, and at depth would satisfy , contradicting that is too deep:
Credits. Charge every insertion and deletion credits for each node on its search path, credits per operation, since paths are that short. Right after a subtree is rebuilt, every node in it has two children whose sizes differ by at most 1:
A scapegoat has saved enough. When becomes a scapegoat, with , its heavier child has more than nodes and the lighter one fewer than , so their difference exceeds . Each update below changes the difference by at most 1 and left credits at , so for a constant :
Full rebuilds happen after more than deletions since the previous one and cost , which those deletions pay for. Altogether:
A scapegoat tree with has . What is the greatest depth a newly inserted node may have without triggering a rebuild?
Show the answer
17
, so depths up to are allowed, and a node at depth 18 starts the search for a scapegoat. A perfectly balanced tree of 1000 keys has height 9; the slack between 9 and 17 is what makes rebuilds rare.
Partial rebuilding fits wherever rotations are impossible or expensive: k-d trees and other multidimensional trees, nodes that carry large secondary structures, or nodes that must not move in memory. The parameter trades height for work. In a quick experiment inserting 4,096 keys in increasing order, gave height 16 for about 34 node visits of rebuilding work per insertion, and gave height 27 for about 18.
The zoo, side by side
| Tree | Invariant | Height | Restructuring per update | Extra space per node | Where it shines |
|---|---|---|---|---|---|
| AVL (1962) | Sibling heights differ by at most 1 | At most one single or double rotation per insertion; per deletion | A height or a 2-bit balance factor | Lookup-heavy workloads | |
| Red-black (1972, 1978) | No red-red edge; equal black counts | rotations per insertion, per deletion; amortized recolorings | 1 bit | Standard libraries, OS kernels | |
| WAVL (2009) | Rank differences 1 or 2; leaves have rank 0 | , and after insertions | rotations per update; amortized | 1 bit | A drop-in improvement on red-black trees |
| Weight-balanced BB[] (1972) | Each side holds at least a fraction of the weight | amortized rotations; heavy nodes rarely | The subtree size | Order statistics; augmented structures; Haskell's Data.Map | |
| Scapegoat (1989, 1993) | Depth at most | No rotations; amortized rebuilding | None | Trees that cannot rotate, such as k-d trees | |
| Treap (1989) | Heap order on random priorities | Expected ; average depth | Fewer than 2 expected rotations | A priority | Split, merge and implicit keys |
| Zip tree (2019) | Heap order on geometric ranks, ties broken by key | Expected depth | No rotations: unzip and zip a path | A small rank | Few random bits; mirrors a skip list |
| Splay (1985) | None | Up to ; amortized per access | amortized rotations per access | None | Skewed or local access patterns |
| B-tree (1972) | to keys per node; all leaves at one depth | amortized splits and merges | Wide nodes | Disks, SSDs and caches |
Think first You are augmenting a tree whose every node stores a sorted array of all the keys in its subtree, so restructuring a node with descendants costs . Which balancing scheme keeps the amortized cost of rebalancing low, and why?
A weight-balanced tree, or scapegoat-style partial rebuilding. After a node of weight is rebalanced, updates must happen below it before its next rebalancing, so an rebuild costs amortized per update at each of the levels. Height- and color-based invariants give no such guarantee: an AVL tree, for instance, can be forced to rotate at levels, including near the root, on every deletion of an alternating sequence of insertions and deletions.
Key takeaways
- Ranks unify the zoo: AVL trees allow 1,1- and 1,2-nodes; WAVL trees also allow 2,2-nodes, keep at most 2 rotations per update, and have height at most , or in terms of insertions.
- BB[] trees bound each side's share of the weight: , amortized rotations, subtree sizes for free, and rare rebalancing at heavy nodes, which suits augmented structures.
- Scapegoat trees store nothing per node: searches are in the worst case, and partial rebuilding makes updates amortized.
- Choose by what you pay for: the shortest height (AVL, WAVL), the fewest rotations (red-black, WAVL), sizes (weight-balanced), no extra space (scapegoat, splay), simple split and join (treaps), or wide nodes (B-trees).
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Which balanced tree stores no balance information at all in its nodes?
- The WAVL tree
- The weight-balanced tree
- The scapegoat tree
- The AVL tree
Show the answer
C. The scapegoat tree
A scapegoat tree keeps only two counters for the whole tree, its size and its largest size since the last full rebuild; it detects imbalance by depth and repairs it by rebuilding a subtree.
A WAVL tree is built by insertions and no deletions. What is it?
- A red-black tree that may be tall
- An AVL tree, with height at most about
- A perfectly balanced tree
- A 2-3 tree
Show the answer
B. An AVL tree, with height at most about
Insertion rebalancing never creates a 2,2-node, so without deletions every node is a 1,1- or 1,2-node: exactly the AVL rank rule. Deletions may create 2,2-nodes, and even then the height stays below and .
When a newly inserted node of a scapegoat tree is too deep, why must an ancestor with exist?
- Because the root always qualifies
- Because if every ancestor sent at most a fraction of its size toward the new node, the node's depth would be at most
- Because the new node's parent always qualifies
- Because rebuilding has made every subtree perfectly balanced
Show the answer
B. Because if every ancestor sent at most a fraction of its size toward the new node, the node's depth would be at most
Along the path, sizes would shrink by a factor per level, from at most at the root to 1 at the new node, which bounds the depth by . A deeper node therefore has a heavy-sided ancestor: the scapegoat.
Haskell's Data.Map is a weight-balanced tree. Which operation does weight balance give it for free, though a red-black tree needs an augmentation for it?
- Testing membership
- Finding the minimum key
- Selecting the -th smallest key
- Iterating in sorted order
Show the answer
C. Selecting the -th smallest key
Weight balance stores every subtree's size, which is exactly the augmentation that select and rank need; Data.Map offers both in .
Further reading
- B. Haeupler, S. Sen & R. E. Tarjan, "Rank-balanced trees," ACM Transactions on Algorithms 11(4) (2015); first presented at WADS 2009.
- J. Nievergelt & E. M. Reingold, "Binary search trees of bounded balance," SIAM Journal on Computing 2(1) (1973), 33–43.
- N. Blum & K. Mehlhorn, "On the average number of rebalancing operations in weight-balanced trees," Theoretical Computer Science 11(3) (1980), 303–320.
- I. Galperin & R. L. Rivest, "Scapegoat trees," Proceedings of the 4th ACM-SIAM Symposium on Discrete Algorithms (SODA) (1993), 165–174.
- R. E. Tarjan, C. Levy & S. Timmel, "Zip trees," ACM Transactions on Algorithms 17(4) (2021); first presented at WADS 2019.