Ordinary Search Tree
A scapegoat tree stores keys in ordinary binary-search-tree order, and search is the textbook loop. Nothing is recorded, rotated, or splayed on the way down.
node *find(node *x, key_t k) {
while (x && x->key != k)
x = (k < x->key) ? x->left : x->right;
return x;
}The structure differs only during updates. It lets local imbalance accumulate, then rebuilds an entire offending subtree into a nearly complete one. Search is O(log n) worst-case because the height is bounded at all times; insertion and deletion are O(log n) amortized, although one update can take linear time. Read-only lookups can therefore run concurrently without any coordination with each other.
Balance Without Node Metadata
An AVL tree stores a height or balance factor in every node and a red-black tree stores a colour. A scapegoat-tree node stores only its key, value, and child links; the balance state lives in two counters on the tree object.
typedef struct node {
key_t key;
struct node *left, *right; // no height, colour, size, or priority
} node;
typedef struct {
node *root;
size_t n; // keys stored now
size_t q; // upper bound on n since the last full rebuild
} sgtree;Implementations may add parent pointers or keep the insertion path on a stack, but those are navigation aids, not balance metadata. Subtree sizes are computed when needed and never stored.
Alpha And Height
A parameter α, with 1/2 < α < 1, controls the permitted imbalance. The tree keeps every leaf within depth ⌊log₁/α q⌋, and that single number is the whole balance rule.
static int h_alpha(size_t q) {
return (int)floor(log((double)q) / log(1.0 / ALPHA));
}
// alpha = 2/3, q = 1000: log 1000 / log 1.5 = 17.03 -> depth 17
// a perfectly balanced tree of 1000 keys has depth 9Because deletion rebuilds when n < αq, the counters satisfy αq ≤ n ≤ q, so the bound is O(log n). The tree need not be weight-balanced at every node; the global height bound is what guarantees logarithmic search.
Insertion Depth Test
Insertion is an ordinary binary-search-tree insert that remembers its path. It increments n, raises q if needed, and compares the new leaf's depth against h_alpha(q).
void insert(sgtree *t, key_t k) {
node *path[MAX_DEPTH];
int d = bst_insert_with_path(t, k, path); // path[d] is the new leaf
t->n++;
if (t->n > t->q) t->q = t->n;
if (d > h_alpha(t->q))
rebuild_scapegoat(t, path, d); // depth bound broken
}Most insertions stop after the comparison with no restructuring. A leaf deeper than the bound proves that some ancestor on its path has become too lopsided, and the next step finds it. The test costs one comparison of two integers, which is the entire per-insert balance overhead when no rebuild follows.
Finding The Scapegoat
Walk up from the new leaf, tracking the size of the subtree just left behind. An ancestor w is a scapegoat when that child holds more than an α fraction of w's subtree.
void rebuild_scapegoat(sgtree *t, node **path, int d) {
size_t child = 1; // the new leaf alone
for (int i = d - 1; i >= 0; i--) {
node *w = path[i];
node *sib = (w->left == path[i + 1]) ? w->right : w->left;
size_t total = child + 1 + size(sib); // count only the sibling fresh
if (child > ALPHA * total) { // w is a scapegoat
replace_child(t, i ? path[i - 1] : NULL, w, rebuild(w, total));
return;
}
child = total;
}
}Each step counts only the sibling subtree it has not seen, so the total counting work is bounded by the size of the scapegoat's subtree, which the rebuild costs anyway. Any unbalanced ancestor restores the bound, since rebuilding it shortens every path below it; taking the first one found keeps the rebuild as small as possible.
Subtree Rebuilding
The scapegoat's subtree is flattened by an in-order walk into an array of node pointers, then rebuilt by making each range's median its root. The nodes are reused; only links change.
static size_t flatten(node *x, node **a, size_t i) {
if (!x) return i;
i = flatten(x->left, a, i);
a[i++] = x;
return flatten(x->right, a, i);
}
static node *build(node **a, size_t lo, size_t hi) { // [lo, hi)
if (lo == hi) return NULL;
size_t mid = lo + (hi - lo) / 2;
a[mid]->left = build(a, lo, mid);
a[mid]->right = build(a, mid + 1, hi);
return a[mid];
}Both passes are O(s) for s nodes and the result is as balanced as s allows. The temporary array costs O(s) space; Galperin and Rivest give a variant using logarithmic space. Because every node in the rebuilt subtree is visited, the same pass can recompute any augmented field, such as subtree sums for an order-statistic tree, at no extra asymptotic cost.
Why A Scapegoat Exists
The depth test is safe only if a too-deep leaf always has an unbalanced ancestor to rebuild. The argument is a counting one.
suppose no ancestor on the path is alpha-unbalanced:
subtree at depth 1 has at most alpha * q nodes
subtree at depth 2 has at most alpha^2 * q nodes
...
subtree at depth d has at most alpha^d * q nodes
d > log_{1/alpha} q implies alpha^d * q < 1
but the depth-d subtree holds the new leaf, so it has at least 1 nodeThe contradiction shows that at least one ancestor sends more than an α fraction of its nodes down the path. Rebuilding that ancestor's subtree restores the depth bound for every leaf beneath it, because a rebuilt subtree of s nodes has height ⌊log₂ s⌋. The proof uses only subtree sizes, which is why no per-node field is needed to maintain the invariant.
Amortized Insertion
One rebuild can touch every node, so one insertion is O(n) worst-case. Over a sequence it is O(log n), because a freshly rebuilt subtree needs many updates before it can be unbalanced again.
after a rebuild, every node has |size(l) - size(r)| <= 1
for w (size s) to become a scapegoat:
size(heavy child) > alpha * s
size(l) - size(r) > (2 alpha - 1) * s
each update below w changes the difference by at most 1
=> Omega(s) updates passed through w since it was rebuilt
charge O(1) to each of the O(log n) ancestors of every updateThe credits a subtree of size s collects pay for its O(s) rebuild. Amortized analysis covers the accounting method used here. The bound is the same O(log n) as an AVL tree, but the work arrives in bursts rather than as a constant number of rotations per update.
Deletion And Global Rebuild
Deletion is the ordinary binary-search-tree splice. Removing a node cannot make any path longer, so no local repair is needed; the only risk is that q drifts far above n and the height bound loosens.
void erase(sgtree *t, key_t k) {
if (!bst_delete(&t->root, k)) return; // ordinary splice
t->n--;
if (t->n < ALPHA * t->q) { // q too loose: rebuild everything
t->root = rebuild(t->root, t->n);
t->q = t->n;
}
}A full rebuild costs O(n) and happens only after (1 − α)q deletions, so deletion is O(log n) amortized with a linear worst case for the one deletion that triggers it. Setting q = n after the rebuild is what lets the next round of insertions start from a tight height bound again.
Choosing Alpha
Smaller α permits less imbalance and keeps search paths short, but rebuilds start sooner. Larger α tolerates deeper trees and rebuilds less often.
maximum leaf depth at q = 1,000,000 (perfectly balanced: 20)
alpha = 0.55 23
alpha = 0.60 27
alpha = 2/3 34
alpha = 0.75 48
alpha = 0.90 131The asymptotic bounds hold for any fixed α strictly between 1/2 and 1; the choice changes constants and how bursty the update latency is. Values near 2/3 are the common default, giving search paths under twice the optimum. A read-heavy index can afford a lower value, since the extra rebuilds are paid only by writers.
Engineering Trade-offs
Scapegoat trees avoid per-node balance fields and never restructure during lookup, so a search is a pure read that needs no write to shared memory. A rebuild produces a compact, nearly complete subtree and can refresh augmented summaries for every node in it in one ordered pass, which local rotations cannot.
The cost is bursty updates: a single insertion or deletion may rebuild a large subtree, and the deletion that drops n below αq rebuilds the whole tree. Computing subtree sizes and holding the update path add temporary work and storage. Systems that need bounded per-update latency usually prefer an AVL or red-black tree; splay trees share amortized updates but also mutate on every access, and a treap trades the deterministic bound for random priorities.
Applications
The sources reviewed for this chapter do not identify a prominent production system that documents using a scapegoat tree. Usable library containers exist, but those are implementations rather than evidence of downstream deployment. The structure remains a plausible fit for search-heavy ordered maps when lookup must not mutate the index and bursty update latency is acceptable.
Documented uses are mainly research systems. A hybrid aerial-underwater vehicle path-planning study used a scapegoat tree to find collision-free positions around dynamic obstacles. A cloud-storage integrity design used a scapegoat-based Merkle hash tree, or SGMHT, to support simultaneous authenticated updates to multiple data blocks. Galperin and Rivest also extended partial rebuilding theoretically to k-d trees, orthogonal range-query structures, and quadtrees.
Summary
A scapegoat tree is a binary search tree that enforces a global logarithmic height bound without storing any balance metadata in its nodes. Two counters, n and q, and a parameter α define the bound; an insertion that lands too deep walks up to a weight-unbalanced ancestor and rebuilds that subtree from its sorted nodes.
Search is O(log n) worst-case and never changes the tree. Insertion and deletion are O(log n) amortized, but an individual update can cost O(n), and deletions trigger a full rebuild once n falls below αq. The design exchanges small incremental rotations for occasional linear rebuilds.