Find the next stored number after x in about lg lg u steps, where u is the size of the key space. A 32-bit key needs 5 halvings whether you store 10 keys or 10 million. The price is memory for every possible key.
Pick a key width and drag the number of stored keys. Watch the key split in half, level by level, while the balanced tree's bar grows. Then we build, piece by piece, the bit vector, the split into clusters, the trap of two recursive calls and the one trick that removes it.
You need a little Python and the idea of binary numbers. We build the rest from zero.
Based on Chapter 20, van Emde Boas Trees, of Introduction to Algorithms (CLRS, third edition) by Cormen, Leiserson, Rivest and Stein. Previous: Fibonacci Heaps. Next: Disjoint Sets.
Counts, not timings. vEB: the recursive calls of a successor query, lg lg u, plus the first call. Balanced tree: ⌊lg n⌋ + 1 comparisons in the worst case. The key is random.
Chapter 0
Meet the query this structure exists for, and see where a search tree stops scaling
A timer service stores the times of future events as whole numbers of milliseconds. Users add and remove events all day. The service asks one question again and again: what is the first event at or after time x?
That is a successor query: the smallest stored key larger than x. The mirror is a predecessor query: the largest stored key smaller than x. Both need the keys kept in order while they change.
A balanced binary search tree answers in about lg n comparisons, where n is the number of stored keys and lg is log base two. It can do no better with comparisons alone. Each comparison has two outcomes, and there are n + 1 possible answers, so it needs at least lg(n + 1) of them.
But our keys are not opaque. They are integers from a fixed range {0, 1, …, u − 1}. The range is the universe, and u is its size. A program can divide a key, take a remainder or read its bits. A van Emde Boas tree (vEB tree) uses that arithmetic and answers in O(lg lg u) steps. The number of stored keys does not enter.
The number line is the universe 0 to 255. Drag n to store more keys and move the query. Teal is x; green is its predecessor.
This page's own code measures both counts. Tree: nodes compared in a perfectly balanced tree built from the stored keys. vEB: calls made by CLRS's predecessor procedure. Keys are random, seeded by n.
With 4 keys the tree needs at most 3 comparisons, and the vEB tree can need 4 calls. With 128 keys the tree needs up to 8 and the vEB tree still at most 4. For u = 256, lg lg u = lg 8 = 3, so a query makes at most 3 recursive calls plus the first one.
The failure. Symptom: a sorted index of integer keys spends most of its time in comparisons. Measure: count comparisons per query at n, 2n and 4n; each doubling adds one. Fix: if the universe is small enough to allocate, a structure built on the key's bits removes n from the cost.
Chapter 1
Answer membership in one step and watch successor scan the whole row
Start with the plainest structure. Make an array B of u bits. Set B[x] to 1 when x is in the set and to 0 when it is not. This is a bit vector, the direct-address table of CLRS Chapter 11 with one bit per slot.
Insert, delete and member each touch one bit, so each costs O(1). Successor does not. To find the next key after x, read B[x + 1], then B[x + 2], and go on until a 1 appears. With two keys at 0 and u − 1, successor(0) reads u − 1 bits.
Tap a slot to store or remove a key. Pick x, then press Successor. Red cells are reads that found 0.
Universe u = 32. The count is the number of bits read, the same count the Python loop below makes.
For u = 232 the worst case reads about 4.3 billion bits. A 64-bit machine word can test 64 bits at once, which cuts the reads to 232/64 = 67,108,864 words. That is still linear in u.
pythonclass BitVector: def __init__(self, u): self.B = [0] * u # one bit per possible key def insert(self, x): self.B[x] = 1 # O(1) def member(self, x): return self.B[x] == 1 def successor(self, x): for y in range(x + 1, len(self.B)): # O(u) in the worst case if self.B[y]: return y return None
| Operation | Bit vector | Balanced BST | vEB tree (the goal) |
|---|---|---|---|
| Insert, delete, member | O(1) | O(lg n) | O(lg lg u) |
| Successor, predecessor | O(u) | O(lg n) | O(lg lg u) |
| Minimum, maximum | O(u) | O(lg n) | O(1) |
| Space | u bits | O(n) | O(u) |
The failure. Symptom: successor is fast on a dense set and slow on a sparse one. Measure: count bits read per query; it tracks the gap to the next key, not n. Fix: add a coarse layer that says which blocks of bits hold any key, so a scan can skip empty blocks.
Chapter 2
Fold the row of bits into a square, add a summary column, and name the two halves of a key
Fold the row of u bits into a square: √u rows of √u bits. For u = 16 that is 4 rows of 4. Each row is a cluster. Row 0 holds keys 0 to 3, row 1 holds keys 4 to 7, and so on.
Next to the square, keep a summary: one bit per cluster, set to 1 when that cluster holds any key. Now each key has two coordinates: its row and its place in the row.
Call the key x. Call its cluster number high(x), and its position inside the cluster low(x). Call the cluster size √u.
x equals high of x times root u, plus low of x.
Divide x by √u. The whole part is high(x), the cluster. The remainder is low(x), the position. Multiply high(x) by √u and add low(x) to rebuild x; CLRS calls this rebuild index(high, low).
u = 16, √u = 4, x = 11. high(11) = 11 ÷ 4 rounded down = 2. low(11) = 11 mod 4 = 3. Rebuild: 2 · 4 + 3 = 11. In bits, 1011 splits into 10 (2) and 11 (3).
Tap cells to store keys. Pick x and press Successor. Warm is x's cluster, blue its position, purple the summary.
One level only, scanning bits. The counts are cells read: the rest of x's cluster, then summary bits, then the first cluster found.
Successor now has two paths. Take the set {2, 3, 7, 11, 14}. Successor(5) looks in cluster 1 after position 1. It finds position 3 and returns index(1, 3) = 7. Successor(7) finds nothing after position 3 in cluster 1. It asks the summary for the next cluster with a key, gets cluster 2, and returns that cluster's minimum, 11.
Each path reads at most √u bits in the cluster, √u in the summary and √u in the next cluster: O(√u). For u = 232, √u = 65,536, so about 196,608 reads replace 4.3 billion.
The failure. Symptom: keys land in the wrong cluster. Measure: check index(high(x), low(x)) == x for every x. Fix: use the cluster size, not the cluster count, as the divisor; they differ when the bits are odd (Chapter 3).
Chapter 3
Apply the split again inside each cluster and count the levels down to one bit
A cluster is itself a small universe of √u keys. Split it the same way: its own clusters and its own summary. Do it again inside those. The recursion stops at a universe of 2 keys, the base case.
Count in bits. Call the number of bits in a key k, so u = 2k and k = lg u. A cluster of √u keys needs k/2 bits. Each level halves the bits: a 16-bit key becomes 8, then 4, 2 and 1. Call the number of halvings L.
L equals log base two of log base two of u.
Count the bits k in a key. Halve k until it reaches 1. The number of halvings is L. A query visits L + 1 structures: the full one and one at each level below.
u = 232: k = 32, then 16, 8, 4, 2, 1. That is L = 5 halvings and 6 structures visited. For u = 264, L = 6. For u = 16, L = 2: 16, then 4, then 2.
Pick a key width and a key. Each row is one level: warm bits pick the cluster, blue bits go down as the new key. Try 5 bits.
The split follows CLRS: with k bits, low takes the bottom ⌊k/2⌋ bits and high the top ⌈k/2⌉.
Odd bit counts need one rule. CLRS gives a universe of 2k keys exactly 2⌈k/2⌉ clusters of 2⌊k/2⌋ keys each. For u = 32 (5 bits) that is 8 clusters of 4: high takes 3 bits and low takes 2. Key 21 = 10101 goes to cluster 5 at position 1.
The failure. Symptom: a recursive build crashes or loops for u = 32. Measure: print √u; int(32 ** 0.5) gives 5, not a power of two. Fix: compute the two sizes from the bit count, as in the rule above.
Chapter 4
Make the split recursive, count the calls, and find that two of them undo the gain
CLRS first builds the recursion as directly as possible. A proto-vEB(u) holds √u clusters, each a proto-vEB(√u), and a summary, also a proto-vEB(√u). A proto-vEB(2) holds two bits. Proto-vEB(16) has 1 top node, 5 proto-vEB(4) nodes and 15 proto-vEB(2) nodes: 21 in all.
Member works well. It calls into one cluster per level, so it makes L + 1 calls. Successor does not. It cannot tell from outside whether x's cluster holds a later key. So it calls successor inside the cluster first. If that fails, it calls successor on the summary.
Call the work of one successor on universe u T(u). Call the fixed work of a call, outside its recursive calls, c.
T of u equals two T of root u, plus c.
Each call does c work and starts two calls one level down. Count calls by level: 1, 2, 4 and so on. There are L + 1 levels, so the bottom level has 2L = lg u calls, and the total is 2 lg u − 1. That is Θ(lg u), no better than a balanced tree.
u = 216: the levels have 16, 8, 4, 2 and 1 bits. The calls are 1 + 2 + 4 + 8 + 16 = 31 = 2 · 16 − 1. A balanced tree over all 65,536 keys needs at most 17 comparisons.
Each dot is one call, labelled by its bit width. Switch between two calls and one call per level, and drag the key width.
Successor calls only. CLRS's proto-vEB successor also calls MINIMUM, which itself branches twice, so its true cost is Θ(lg u lg lg u).
Insert has the same flaw: put low(x) into the cluster, and also put high(x) into the summary. Two calls again, and Θ(lg u). The single number in front of T(√u) decides everything.
The failure. Symptom: a recursive structure with "square root" in its design runs no faster than a tree. Measure: count calls per query for k = 8, 16 and 32 bits; if the count doubles with k, there are two calls per level. Fix: decide in O(1) which one branch to take (Chapter 5).
Chapter 5
Store min and max in every node, keep min out of the clusters, and insert with one call
The real vEB(u) adds two fields to every node: min and max, the smallest and largest key it holds. One rule makes them powerful: a node keeps min only in itself, never in a cluster. It keeps max in both.
Why keep min outside? An empty node becomes a one-key node by setting min = max = x, with no recursion. So when insert must touch a cluster and the summary, the cluster is empty and its update costs O(1). Only the summary gets a recursive call.
Press Insert next to replay 3, 7, 2, 11, 14. Yellow is a min kept outside. Teal boxes are the calls of the last insert.
CLRS's vEB-INSERT on u = 16. Cluster and summary boxes show local positions 0 to 3; small cells mark which positions each holds.
Trace 3, 7, 2, 11. Insert 3 sets the root's min = max = 3: one call. Insert 7 finds cluster 1 empty, inserts 1 into the empty summary (O(1)) and sets cluster 1 to min = max = 3: two calls. Insert 2 swaps with min 3, so 3 goes down into cluster 0: three calls. Insert 11 fills cluster 2: three calls.
The failure. Symptom: after insert(2), a search of cluster 0 does not find 2. Measure: that is correct; check the root's min instead. Fix: every procedure tests min before it recurses, or it misses one key per node.
Chapter 6
Read one field to pick the branch, then make one call
Proto-vEB called into the cluster to learn whether it held a later key. The vEB tree reads the cluster's max instead. If low(x) is below that max, the answer is in the cluster. If not, the answer is the min of the next non-empty cluster, which the summary finds.
Predecessor is the mirror, with one extra check. Min sits in no cluster, so the summary does not know it. If no earlier cluster exists, test min itself before you return None.
Pick x and the query, then press Trace. Teal boxes are the calls in order; the log shows each decision.
CLRS's vEB-TREE-SUCCESSOR and vEB-TREE-PREDECESSOR on the set 2, 3, 7, 11, 14.
Each call does fixed work and starts at most one call on a universe of √u. Call the work T(u) and the fixed part c.
T of u equals T of root u, plus c.
Each call does c work and starts at most one call one level down. The calls form a chain, one per level. With L + 1 levels, the chain has at most L + 1 = lg lg u + 1 calls.
u = 16: levels 16, 4, 2, so at most 3 calls. Successor(7) makes 3: the root, the summary and the summary's summary. It returns 11. For u = 216 the chain has 5 calls; proto-vEB made 31.
The failure. Symptom: predecessor(3) returns None although the set holds 2. Measure: 2 is the root's min and sits in no cluster, so the summary search finds nothing. Fix: after the summary returns None, return min if x > min.
Chapter 7
Promote a new min when the old one leaves, then drive every operation yourself
Delete uses the same trick backwards. A cluster that drops to zero keys held exactly one key, its min. Removing it sets min = max = None in O(1). So when delete must also clear the summary, the cluster part was cheap.
Delete 7 from {2, 3, 7, 11, 14}. Cluster 1 held only position 3, so it empties in one call. Then the summary deletes 1, and inside it one more one-key cluster empties. That is 5 calls in all, but 2 of them are O(1) one-key deletes. Then delete 2: it is min, so cluster 0 gives up 3 as the new min and empties.
Pick a key, then an operation. Load the running set, delete its min, and watch a new min rise.
The CLRS procedures on u = 16. "Calls" counts every call, including O(1) calls on one-key or empty nodes.
Try Load 0 to 15. Key 0 is the root's min, so cluster 0 holds only 1, 2 and 3, and its own min, 1, sits outside its sub-clusters. Every node keeps one key out.
| Operation | Time | Why one call is enough |
|---|---|---|
| Minimum, maximum | O(1) | stored in the node |
| Member | O(lg lg u) | check min and max, then one cluster |
| Successor, predecessor | O(lg lg u) | read max (or min) to pick the cluster or the summary |
| Insert | O(lg lg u) | an empty cluster fills in O(1) |
| Delete | O(lg lg u) | a cluster empties only from one key, in O(1) |
The failure. Symptom: after deleting the min, a later key vanishes. Measure: check that the promoted key was also deleted from its cluster. Fix: after promotion, set x to the promoted key and fall through to the cluster delete.
Chapter 8
Count the nodes a full tree allocates, and see how hashing and tries cut them
A vEB tree allocates every cluster up front, empty or not. Call the number of nodes for universe u N(u). A vEB(2) is one node.
N of u equals one, plus root u plus one, times N of root u.
Count this node. Then add the nodes of each of its √u + 1 children, each a full tree on √u keys. Start from N(2) = 1 and build up.
N(4) = 1 + 3 · 1 = 4. N(16) = 1 + 5 · 4 = 21. N(256) = 1 + 17 · 21 = 358. N(65,536) = 1 + 257 · 358 = 92,007, about 1.4 nodes per possible key. CLRS shows the space is O(u).
Each bar is a count of nodes on a log scale. Widen the keys, then shrink n, and watch the full tree ignore n.
Full vEB: the exact node count N(u) from the CLRS layout. Hashed vEB: the bound n(lg lg u + 1), at most one new node per level per insert. Tree: n nodes.
For 32-bit keys N(u) is about 6.03 billion nodes, even if you store three keys. At an illustrative 16 bytes per node that is about 90 GiB. For 16-bit keys it is 92,007 nodes, about 1.4 MiB at the same size.
The fixes keep the time and drop the universe from the space:
| Structure | Predecessor | Space | Idea |
|---|---|---|---|
| Balanced BST | O(lg n) | O(n) | comparisons only; any key type |
| vEB tree | O(lg lg u) | O(u) | this lesson |
| vEB with hashed clusters | O(lg lg u) expected | O(n lg lg u) | create a cluster only when a key arrives |
| y-fast trie (Willard, 1983) | O(lg lg u) expected | O(n) | hash tables over bit prefixes, plus small trees in buckets |
The failure. Symptom: building an empty vEB tree for 32-bit keys runs out of memory. Measure: compute N(u) before you allocate. Fix: hash the clusters, or use a balanced tree when n is far below u.
Chapter 9
Keep the terms, the numbers, the checks and the core code on one page
A vEB tree stores integers from a universe of u keys. Each node splits a key into high (the cluster) and low (the position). So each level halves the bits, and a query visits lg lg u + 1 levels. Min and max live in every node, and min stays out of the clusters. So each operation makes one real recursive call per level. The price is O(u) space.
| Term | Meaning | Number on this page |
|---|---|---|
| Universe u | all possible keys, 0 to u − 1 | u = 16 in the devices |
| high(x), low(x) | cluster number and position: x = high · √u + low | 11 → 2 and 3; 200 → 12 and 8 (u = 256) |
| Summary | a vEB on the numbers of the non-empty clusters | {0, 1, 2} after 3, 7, 2, 11 |
| Depth | L = lg lg u halvings, L + 1 levels | 32 bits: 5 halvings, 6 levels |
| Proto-vEB | two calls per level, 2 lg u − 1 calls | 31 calls for 16 bits |
| Min trick | min in the node only, so an empty cluster fills in O(1) | insert calls 1, 2, 3, 3 for 3, 7, 2, 11 |
| Successor | read cluster max, then one call | successor(7) = 11 in 3 calls |
| Space | N(u) = 1 + (√u + 1) N(√u) | 92,007 nodes for 16 bits |
pythonclass VEB: def __init__(self, u): # u = 2**k self.u, self.min, self.max = u, None, None if u > 2: k = u.bit_length() - 1 self.lo, up = 1 << (k // 2), 1 << ((k + 1) // 2) self.cluster = [VEB(self.lo) for _ in range(up)] self.summary = VEB(up) def high(self, x): return x // self.lo def low(self, x): return x % self.lo def index(self, i, j): return i * self.lo + j def insert(self, x): if self.min is None: self.min = self.max = x; return if x < self.min: x, self.min = self.min, x # old min goes down if self.u > 2: c = self.cluster[self.high(x)] if c.min is None: self.summary.insert(self.high(x)) # the one call c.min = c.max = self.low(x) # O(1) else: c.insert(self.low(x)) # the one call if x > self.max: self.max = x def successor(self, x): if self.u == 2: return 1 if x == 0 and self.max == 1 else None if self.min is not None and x < self.min: return self.min i, mc = self.high(x), self.cluster[self.high(x)].max if mc is not None and self.low(x) < mc: # peek, then one call return self.index(i, self.cluster[i].successor(self.low(x))) j = self.summary.successor(i) return None if j is None else self.index(j, self.cluster[j].min) V = VEB(16) for k in [2, 3, 7, 11, 14]: V.insert(k) print(V.successor(7), V.successor(14)) # 11 None
Now press Present or Teach. Explain from memory why proto-vEB makes 2 lg u − 1 calls. Then say how the min trick and the max peek bring it down to lg lg u + 1. Then go back to the hero, switch from 32-bit to 64-bit keys, and say how many halvings it added.
T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein, Introduction to Algorithms, third edition, MIT Press, Chapter 20, van Emde Boas Trees. The structure is due to Peter van Emde Boas (1975). The procedures follow the book; the devices and the toy sets on this page are our own.