Simple Algorithm
Algorithms & Data Structures · Optimal · 26 min

The Balanced-Tree Zoo

AVL, WAVL, weight-balanced and scapegoat trees: which invariant buys what.

Every balanced search tree buys O(log⁡n)O(\log n) 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 2log⁡2n2\log_2 n, and state their rotation and rebalancing bounds.
  • Define weight-balanced BB[α\alpha] trees, bound their height, and explain why rare rebalancing at heavy nodes suits augmented structures.
  • Analyze scapegoat trees: no balance data in the nodes, O(log⁡n)O(\log n) worst-case search, and O(log⁡n)O(\log n) 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 xx has an integer rank r(x)r(x), a missing child has rank −1-1, and the rank difference of a child yy of xx is r(x)−r(y)r(x)-r(y). A node whose two children have rank differences ii and jj is an i,ji,j-node. Different rules on rank differences give different trees:

TreeRank ruleWhat the rank measures
AVLEvery node is a 1,1-node or a 1,2-nodeThe height
Red-blackRank differences 0 or 1, where a 0-child is red; no 0-child has a 0-child; missing children are 1-childrenThe black height
WAVL (weak AVL)Rank differences 1 or 2, and every leaf has rank 0, that is, is a 1,1-nodeBetween the height and twice the height
AVL trees are exactly the WAVL trees without 2,2-nodes.
DerivationProof of the height bound
  1. n0=1,n1=2,nk ≥ 1+2 nk−2(k≥2)n_0=1,\qquad n_1=2,\qquad n_k\ \ge\ 1+2\,n_{k-2}\quad(k\ge2)

    Let nkn_k be the fewest nodes in a WAVL tree of rank kk. 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 k≥2k\ge2 cannot have a missing child (that would be a rank difference above 2), so it has two children of rank at least k−2k-2:

  2. n ≥ 2⌈r(root)/2⌉ ⟹ r(root) ≤ 2log⁡2nn\ \ge\ 2^{\lceil r(\text{root})/2\rceil}\ \Longrightarrow\ r(\text{root})\ \le\ 2\log_2 n

    By induction nk≥2⌈k/2⌉n_k\ge2^{\lceil k/2\rceil}: it holds for k=0,1k=0,1, and 1+2⋅2⌈(k−2)/2⌉=1+2⌈k/2⌉1+2\cdot2^{\lceil(k-2)/2\rceil}=1+2^{\lceil k/2\rceil}. Applied to the root:

  3. h ≤ r(root) ≤ 2log⁡2nh\ \le\ r(\text{root})\ \le\ 2\log_2 n

    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 2log⁡2n−O(1)2\log_2 n-O(1) tall even without deletions, while a WAVL tree built by insertions alone is an AVL tree, at most about 1.44log⁡2n1.44\log_2 n tall, and deletions erode that bound only gradually, never beyond 2log⁡2n2\log_2 n. Where an AVL deletion may rotate at every level, and alternating insertions and deletions can force Θ(log⁡n)\Theta(\log n) 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.

Check your understanding

Which kind of node does a WAVL tree allow but an AVL tree forbid?

  1. A 1,1-node
  2. A 1,2-node
  3. A 2,2-node
  4. 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

DerivationHeight
  1. 2 ≤ w(a node at depth d) ≤ (1−α)d (n+1)2\ \le\ w(\text{a node at depth } d)\ \le\ (1-\alpha)^d\,(n+1)

    Each step down keeps at most a fraction 1−α1-\alpha of the weight, the root has weight n+1n+1, and every node has weight at least 2:

  2. h ≤ log⁡1/(1−α)n+12h\ \le\ \log_{1/(1-\alpha)}\frac{n+1}{2}

    So every depth, and the height, satisfies

  3. h ≤ log⁡2n+12 = 2log⁡2(n+1)−2h\ \le\ \log_{\sqrt2}\frac{n+1}{2}\ =\ 2\log_2(n+1)-2

    With α=1−1/2≈0.293\alpha=1-1/\sqrt2\approx0.293, the largest value for which single and double rotations always restore balance, 1/(1−α)=21/(1-\alpha)=\sqrt2:

Single and double rotations restore the balance condition after an update when 2/11≤α≤1−1/22/11\le\alpha\le1-1/\sqrt2, with O(1)O(1) 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 ww has been rebalanced, Ω(w)\Omega(w) 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 O(1)O(1) per level, O(log⁡n)O(\log n) 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 2log⁡2n2\log_2 n 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 nn and qq, the largest size since the last full rebuild, and uses a parameter 1/2<α<11/2<\alpha<1. Updates are plain BST updates followed by a check:

text
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 ← n

Sizes 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.

DerivationWhy a scapegoat exists, and why rebuilding is cheap
  1. 1 ≤ αd q ⟹ d ≤ log⁡1/αq1\ \le\ \alpha^{d}\,q\ \Longrightarrow\ d\ \le\ \log_{1/\alpha}q

    A scapegoat exists. If every ancestor uu of the new node zz had size(child toward z)≤α size(u)\text{size}(\text{child toward } z)\le\alpha\,\text{size}(u), sizes would shrink by a factor α\alpha per level down the path, and zz at depth dd would satisfy 1≤αdq1\le\alpha^d q, contradicting that zz is too deep:

  2. ∣size(u.left)−size(u.right)∣ ≤ 1right after a rebuild\big|\text{size}(u.left)-\text{size}(u.right)\big|\ \le\ 1\quad\text{right after a rebuild}

    Credits. Charge every insertion and deletion cc credits for each node on its search path, O(log⁡n)O(\log n) 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:

  3. #updates below u since its last rebuild > (2α−1) s  ⟹  c (2α−1) s ≥ cost of rebuilding u=O(s)\#\text{updates below } u \text{ since its last rebuild}\ >\ (2\alpha-1)\,s\ \ \Longrightarrow\ \ c\,(2\alpha-1)\,s\ \ge\ \text{cost of rebuilding } u=O(s)

    A scapegoat has saved enough. When uu becomes a scapegoat, with s=size(u)s=\text{size}(u), its heavier child has more than αs\alpha s nodes and the lighter one fewer than (1−α)s−1(1-\alpha)s-1, so their difference exceeds (2α−1)s+1(2\alpha-1)s+1. Each update below uu changes the difference by at most 1 and left cc credits at uu, so for a constant c=O(1/(2α−1))c=O\big(1/(2\alpha-1)\big):

  4. amortized cost of an insertion or deletion = O(log⁡n)\text{amortized cost of an insertion or deletion}\ =\ O(\log n)

    Full rebuilds happen after more than (1−α)q(1-\alpha)q deletions since the previous one and cost O(n)=O(q)O(n)=O(q), which those deletions pay for. Altogether:

Predict before you look

A scapegoat tree with α=2/3\alpha=2/3 has q=1000q=1000. What is the greatest depth a newly inserted node may have without triggering a rebuild?

Show the answer

17

log⁡3/21000=ln⁡1000/ln⁡1.5≈6.908/0.405≈17.04\log_{3/2}1000=\ln1000/\ln1.5\approx6.908/0.405\approx17.04, so depths up to ⌊17.04⌋=17\lfloor17.04\rfloor=17 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 α\alpha trades height for work. In a quick experiment inserting 4,096 keys in increasing order, α=0.6\alpha=0.6 gave height 16 for about 34 node visits of rebuilding work per insertion, and α=0.75\alpha=0.75 gave height 27 for about 18.

The zoo, side by side

TreeInvariantHeightRestructuring per updateExtra space per nodeWhere it shines
AVL (1962)Sibling heights differ by at most 1<1.4405log⁡2(n+2)−1.33<1.4405\log_2(n+2)-1.33At most one single or double rotation per insertion; O(log⁡n)O(\log n) per deletionA height or a 2-bit balance factorLookup-heavy workloads
Red-black (1972, 1978)No red-red edge; equal black counts≤2log⁡2(n+1)\le2\log_2(n+1)≤2\le2 rotations per insertion, ≤3\le3 per deletion; O(1)O(1) amortized recolorings1 bitStandard libraries, OS kernels
WAVL (2009)Rank differences 1 or 2; leaves have rank 0≤2log⁡2n\le2\log_2 n, and ≤log⁡φm\le\log_\varphi m after mm insertions≤2\le2 rotations per update; O(1)O(1) amortized1 bitA drop-in improvement on red-black trees
Weight-balanced BB[α\alpha] (1972)Each side holds at least a fraction α\alpha of the weight≤log⁡1/(1−α)n+12\le\log_{1/(1-\alpha)}\frac{n+1}{2}O(1)O(1) amortized rotations; heavy nodes rarelyThe subtree sizeOrder statistics; augmented structures; Haskell's Data.Map
Scapegoat (1989, 1993)Depth at most log⁡1/αq+1\log_{1/\alpha}q+1log⁡1/αn+O(1)\log_{1/\alpha}n+O(1)No rotations; O(log⁡n)O(\log n) amortized rebuildingNoneTrees that cannot rotate, such as k-d trees
Treap (1989)Heap order on random prioritiesExpected ≈4.311ln⁡n\approx4.311\ln n; average depth ≈1.39log⁡2n\approx1.39\log_2 nFewer than 2 expected rotationsA prioritySplit, merge and implicit keys
Zip tree (2019)Heap order on geometric ranks, ties broken by keyExpected depth ≤1.5log⁡2n+O(1)\le1.5\log_2 n+O(1)No rotations: unzip and zip a pathA small rankFew random bits; mirrors a skip list
Splay (1985)NoneUp to n−1n-1; O(log⁡n)O(\log n) amortized per accessO(log⁡n)O(\log n) amortized rotations per accessNoneSkewed or local access patterns
B-tree (1972)t−1t-1 to 2t−12t-1 keys per node; all leaves at one depth≤log⁡tn+12\le\log_t\frac{n+1}{2}O(1)O(1) amortized splits and mergesWide nodesDisks, SSDs and caches
Worst-case heights unless marked expected or amortized; φ\varphi is the golden ratio and mm the number of insertions.
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 ss descendants costs O(s)O(s). 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 ww is rebalanced, Ω(w)\Omega(w) updates must happen below it before its next rebalancing, so an O(w)O(w) rebuild costs O(1)O(1) amortized per update at each of the O(log⁡n)O(\log n) levels. Height- and color-based invariants give no such guarantee: an AVL tree, for instance, can be forced to rotate at Θ(log⁡n)\Theta(\log n) 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 2log⁡2n2\log_2 n, or log⁡φm\log_\varphi m in terms of insertions.
  • BB[α\alpha] trees bound each side's share of the weight: h≤log⁡1/(1−α)n+12h\le\log_{1/(1-\alpha)}\frac{n+1}{2}, O(1)O(1) amortized rotations, subtree sizes for free, and rare rebalancing at heavy nodes, which suits augmented structures.
  • Scapegoat trees store nothing per node: searches are O(log⁡n)O(\log n) in the worst case, and partial rebuilding makes updates O(log⁡n)O(\log n) 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.

Question 1

Which balanced tree stores no balance information at all in its nodes?

  1. The WAVL tree
  2. The weight-balanced tree
  3. The scapegoat tree
  4. 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.

Question 2

A WAVL tree is built by mm insertions and no deletions. What is it?

  1. A red-black tree that may be 2log⁡2m2\log_2 m tall
  2. An AVL tree, with height at most about log⁡φm≈1.44log⁡2m\log_\varphi m\approx1.44\log_2 m
  3. A perfectly balanced tree
  4. A 2-3 tree
Show the answer

B. An AVL tree, with height at most about log⁡φm≈1.44log⁡2m\log_\varphi m\approx1.44\log_2 m

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 log⁡φm\log_\varphi m and 2log⁡2n2\log_2 n.

Question 3

When a newly inserted node of a scapegoat tree is too deep, why must an ancestor with size(child toward the node)>α⋅size(ancestor)\text{size}(\text{child toward the node})>\alpha\cdot\text{size}(\text{ancestor}) exist?

  1. Because the root always qualifies
  2. Because if every ancestor sent at most a fraction α\alpha of its size toward the new node, the node's depth would be at most log⁡1/αq\log_{1/\alpha}q
  3. Because the new node's parent always qualifies
  4. Because rebuilding has made every subtree perfectly balanced
Show the answer

B. Because if every ancestor sent at most a fraction α\alpha of its size toward the new node, the node's depth would be at most log⁡1/αq\log_{1/\alpha}q

Along the path, sizes would shrink by a factor α\alpha per level, from at most qq at the root to 1 at the new node, which bounds the depth by log⁡1/αq\log_{1/\alpha}q. A deeper node therefore has a heavy-sided ancestor: the scapegoat.

Question 4

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?

  1. Testing membership
  2. Finding the minimum key
  3. Selecting the kk-th smallest key
  4. Iterating in sorted order
Show the answer

C. Selecting the kk-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 O(log⁡n)O(\log n).

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.