van Emde Boas Trees

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.

Halve the key until one bit is left

1,000,000

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

The Next Number After x

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.

Count the steps of one query

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.

32
140

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.

The road

  1. Bit vectors: member in one step, successor in u steps.
  2. Split the universe: clusters, a summary, and high and low.
  3. Halve the bits: why the depth is lg lg u (the hero, made exact).
  4. Proto-vEB: recursion with two calls, and why it fails.
  5. The vEB tree: min kept outside, and insert with one call.
  6. Successor and predecessor: peek at max, then one call.
  7. Delete, and the whole tree: every operation on vEB(16) (the showcase).
  8. Space, and what to use instead: O(u) memory and its fixes.
  9. The field guide: terms, numbers, code and the credit.
Keys are 32-bit integers and the set holds 10 million of them. Which number decides the cost of a vEB successor query?

Chapter 1

Bit Vectors

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.

Scan for the next key

Tap a slot to store or remove a key. Pick x, then press Successor. Red cells are reads that found 0.

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
OperationBit vectorBalanced BSTvEB tree (the goal)
Insert, delete, memberO(1)O(lg n)O(lg lg u)
Successor, predecessorO(u)O(lg n)O(lg lg u)
Minimum, maximumO(u)O(lg n)O(1)
Spaceu bitsO(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.

A bit vector has u = 1,000,000 slots and holds 500 keys. What is the worst-case cost of one successor query?

Chapter 2

Split the Universe

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.

Say it aloud

x equals high of x times root u, plus low of x.

x
x a key, a whole number from 0 to u − 1.
high(x)
high of x the cluster number: x divided by √u, rounded down. It is the top half of x's bits.
·
times multiply.
√u
root u the size of one cluster and the number of clusters: 4 when u = 16.
low(x)
low of x the position inside the cluster: the remainder of x divided by √u. It is the bottom half of x's bits.
In plain words

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

With numbers

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

Search one cluster, then the summary

Tap cells to store keys. Pick x and press Successor. Warm is x's cluster, blue its position, purple the summary.

7

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

For u = 256, how many clusters are there, and what are high(200) and low(200)?

Chapter 3

Halve the Bits

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.

Say it aloud

L equals log base two of log base two of u.

L
L the number of halvings from the full key down to the base case: the recursive calls in one query.
lg
log base two how many times you halve a number to reach 1.
lg u
log base two of u the bit width k of a key: 32 for a 32-bit universe.
In plain words

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.

With numbers

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.

Follow one key down

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.

200

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.

For u = 264, how many halvings does a key go through before the base case?

Chapter 4

Proto-vEB: the Two-Call Trap

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.

Say it aloud

T of u equals two T of root u, plus c.

T(u)
T of u the work of one successor call on a universe of u keys, counted in calls.
2
two the cluster call and the summary call, in the worst case both.
T(√u)
T of root u the work of one call on a universe of √u keys.
c
c the fixed work of one call: a division, a remainder, a few reads.
In plain words

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.

With numbers

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.

Draw the calls of one query

Each dot is one call, labelled by its bit width. Switch between two calls and one call per level, and drag the key width.

16 bits

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

Why is proto-vEB successor no faster than a balanced tree?

Chapter 5

The vEB Tree and the Min Trick

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.

  1. u: the universe size of this node.
  2. min, max: read in O(1). Empty means both are None.
  3. cluster[0 .. √u − 1]: each a vEB(√u) holding every key except min.
  4. summary: a vEB(√u) holding the numbers of the non-empty clusters.

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.

  1. If the node is empty, set min = max = x and stop.
  2. If x < min, swap them. The old min goes down instead.
  3. If cluster high(x) is empty, insert high(x) into the summary (one call) and set that cluster's min = max = low(x).
  4. Otherwise insert low(x) into that cluster (one call). The summary already knows it.
  5. If x > max, set max = x.

Insert with one call per level

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.

9

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.

vEB(16): min = 2, max = 11 2 lives only here cluster[0]: min = 3, max = 3 key 3 cluster[1]: min = 3, max = 3 key 7 cluster[2]: min = 3, max = 3 key 11 summary: min = 0, max = 2 clusters 0, 1, 2 summary.cluster[0]: min = max = 1 (0 is the summary's own min) summary.cluster[1]: min = max = 0 (summary entry 2)

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.

Why does a vEB node keep min out of its clusters?

Chapter 6

Successor and Predecessor

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.

  1. If x < min, return min. No recursion.
  2. Read max of cluster high(x). If low(x) < that max, call successor in that cluster.
  3. Otherwise call successor on the summary with high(x). If it returns cluster j, return index(j, min of cluster j).

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.

Trace one query

Pick x and the query, then press Trace. Teal boxes are the calls in order; the log shows each decision.

7

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.

Say it aloud

T of u equals T of root u, plus c.

T(u)
T of u the work of one successor call on a universe of u keys, counted in calls.
T(√u)
T of root u the one recursive call: into the cluster or into the summary, never both.
c
c the fixed work: compare with min, read one max, compute high and low.
In plain words

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.

With numbers

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.

How does vEB successor decide, without a recursive call, whether the answer is in x's own cluster?

Chapter 7

Delete, and the Whole Tree

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.

  1. If min = max, the node holds one key. Set both to None and stop.
  2. If x is min, find the second smallest key: the min of the first non-empty cluster. Make it the new min, then delete it from its cluster.
  3. Delete low(x) from cluster high(x).
  4. If that cluster is now empty, delete high(x) from the summary.
  5. If x was max, read the new max from the last non-empty cluster, or set max = min.

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.

Drive the whole tree

Pick a key, then an operation. Load the running set, delete its min, and watch a new min rise.

2

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.

OperationTimeWhy one call is enough
Minimum, maximumO(1)stored in the node
MemberO(lg lg u)check min and max, then one cluster
Successor, predecessorO(lg lg u)read max (or min) to pick the cluster or the summary
InsertO(lg lg u)an empty cluster fills in O(1)
DeleteO(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

Space, and What to Use Instead

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.

Say it aloud

N of u equals one, plus root u plus one, times N of root u.

N(u)
N of u the number of vEB nodes allocated for a universe of u keys.
1
one this node: its min, max and pointers.
√u + 1
root u plus one the children: √u clusters and one summary.
·
times each child has the same size.
N(√u)
N of root u the nodes inside one child.
In plain words

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.

With numbers

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

Count the memory

Each bar is a count of nodes on a log scale. Widen the keys, then shrink n, and watch the full tree ignore n.

20 bits
1,000

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:

StructurePredecessorSpaceIdea
Balanced BSTO(lg n)O(n)comparisons only; any key type
vEB treeO(lg lg u)O(u)this lesson
vEB with hashed clustersO(lg lg u) expectedO(n lg lg u)create a cluster only when a key arrives
y-fast trie (Willard, 1983)O(lg lg u) expectedO(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.

A full vEB tree over u = 220 stores 100 keys. About how many nodes does it allocate?

Chapter 9

The Field Guide

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.

Terms and numbers

TermMeaningNumber on this page
Universe uall possible keys, 0 to u − 1u = 16 in the devices
high(x), low(x)cluster number and position: x = high · √u + low11 → 2 and 3; 200 → 12 and 8 (u = 256)
Summarya vEB on the numbers of the non-empty clusters{0, 1, 2} after 3, 7, 2, 11
DepthL = lg lg u halvings, L + 1 levels32 bits: 5 halvings, 6 levels
Proto-vEBtwo calls per level, 2 lg u − 1 calls31 calls for 16 bits
Min trickmin in the node only, so an empty cluster fills in O(1)insert calls 1, 2, 3, 3 for 3, 7, 2, 11
Successorread cluster max, then one callsuccessor(7) = 11 in 3 calls
SpaceN(u) = 1 + (√u + 1) N(√u)92,007 nodes for 16 bits

Before you use one

  1. Check that keys are integers in a known range 0 to u − 1.
  2. Compute N(u) and the memory before you allocate.
  3. If n is far below u, hash the clusters or use a balanced tree.
  4. Derive the two cluster sizes from the bit count, not from a float square root.
  5. Test every procedure against a sorted list on random inserts and deletes.
  6. Remember the min check in predecessor and the promotion in delete.

The core code

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

Keep going

The takeaway. One recursive call on √u per level gives lg lg u. Keep min out of the clusters and read max before you recurse, and every operation keeps to that one call.
Which two facts together make every vEB operation O(lg lg u)?

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.

Source

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.

CLRS Chapter 20 · van Emde Boas Trees
Next: Disjoint Sets · Back to Gleams