Storing min and max directly in each vEB node (without inserting min into sub-clusters) guarantees at most ONE recursive call per query: T(w) = T(w/2) + O(1) = O(log log U).
TL;DR30-second simplified summary▾
Bit-split x into high(x) = x >> (w/2) and low(x) = x & ((1<<(w/2))-1). For 32-bit integers, log2(32) = 5 recursive steps max regardless of n.
P — Breaking the O(log n) Comparison Barrier.
Comparison BST vs. Van Emde Boas Tree
A balanced BST storing n = 16,777,216 32-bit integers performs ~24 pointer-chasing comparisons per predecessor query, suffering ~24 L3/DRAM cache misses. Yet a 32-bit integer has only w = 32 bits of information. By treating the integer as a pair (high, low) of 16-bit halves, we can jump straight to the right bucket instead of comparing keys.
R & I — The Single-Recursive-Call Invariant.
vEB(U) Recursive Bit-Split Structure
Each vEB(U) node stores min, max, a summary tree of size sqrt(U), and sqrt(U) cluster trees of size sqrt(U). Crucially, min is stored ONLY in node.min and NOT inside cluster[high(min)]. Therefore, cluster[c].min can be read in O(1) time.
When querying predecessor(x):
- If
low(x) > cluster[high(x)].min(an O(1) check!), the predecessor is in the same cluster -> recurse ONLY oncluster[high(x)]. - Otherwise, recurse ONLY on
summary.predecessor(high(x))to find the previous non-empty clusterpred_c, and returncluster[pred_c].maxin O(1)!
M — Clean Python Implementation of vEB(U).
O(log log U) Predecessor Query Path
- Problem
- How do you support O(log log U) predecessor queries on w-bit integers?
- Naive Solution
- Store keys in a Red-Black Tree or B-Tree.
- Why It Fails
- Comparison trees require O(log n) comparisons regardless of key universe size.
- Alternatives
- Raw Van Emde Boas Tree (O(log log U) worst-case, O(U) space),y-fast Trie (O(log log U) expected, O(n) space),Red-Black Tree (O(log n) worst-case, O(n) space)
- Engineering Decision
- Use vEB when U <= 2^24 (or bit-packed flat arrays for U <= 2^32) and deterministic worst-case latency is required; use y-fast tries when U = 2^64 and space must be O(n).
- Trade-Offs
- Raw vEB allocates O(U) pointers unless lazy/hash-compressed.
If min were ALSO inserted into cluster[high(min)], what would happen to predecessor(x) time complexity?
Notice how insert(x) swaps x with min if x < min and inserts the old min only when cluster[high(x)] was previously empty (an O(1) base case), preserving the single-recursive-call bound:
import math
from typing import Optional, List
class VEBTree:
def __init__(self, u: int) -> None:
self.u = u
self.min: Optional[int] = None
self.max: Optional[int] = None
if u > 2:
half_bits = (u.bit_length() - 1) // 2
self.low_bits = half_bits
self.low_mask = (1 << half_bits) - 1
upper_size = u >> half_bits
lower_size = 1 << half_bits
self.summary = VEBTree(upper_size)
self.clusters: List[VEBTree] = [VEBTree(lower_size) for _ in range(upper_size)]
def _high(self, x: int) -> int:
return x >> self.low_bits
def _low(self, x: int) -> int:
return x & self.low_mask
def _index(self, h: int, l: int) -> int:
return (h << self.low_bits) | l
def predecessor(self, x: int) -> Optional[int]:
if self.u == 2:
return 0 if (x == 1 and self.min == 0) else None
if self.max is not None and x > self.max:
return self.max
if self.min is None or x <= self.min:
return None
h, l = self._high(x), self._low(x)
min_low = self.clusters[h].min
if min_low is not None and l > min_low:
offset = self.clusters[h].predecessor(l)
return self._index(h, offset) if offset is not None else self.min
pred_cluster = self.summary.predecessor(h)
if pred_cluster is None:
return self.min
return self._index(pred_cluster, self.clusters[pred_cluster].max)
E — Space Complexity & When Not to Use Raw vEB.
vEB Space Optimization Tiers
A raw vEB tree for U = 2^64 is impossible to allocate in RAM (O(U) space). For sparse 64-bit universes, replace the dense clusters[] array with a hash table (x-fast trie) or group O(log U) consecutive keys into balanced BSTs (y-fast trie) to achieve O(n) space with O(log log U) expected predecessor.