# Van Emde Boas tree

van Emde Boas tree
Type Non-binary tree
Invented 1975
Invented by Peter van Emde Boas
Asymptotic complexity
in big O notation
Space O(M)
Search O(log log M)
Insert O(log log M)
Delete O(log log M)

A van Emde Boas tree (Dutch pronunciation: [vɑn 'ɛmdə 'boːɑs]), also known as a vEB tree or van Emde Boas priority queue, is a tree data structure which implements an associative array with m-bit integer keys. It performs all operations in O(log m) time, or equivalently in O(log log M) time, where M = 2m is the maximum number of elements that can be stored in the tree. The M is not to be confused with the actual number of elements stored in the tree, by which the performance of other tree data-structures is often measured. The vEB tree has good space efficiency when it contains many elements, as discussed below. It was invented by a team led by Dutch computer scientist Peter van Emde Boas in 1975.

## Supported operations

A vEB supports the operations of an ordered associative array, which includes the usual associative array operations along with two more order operations, FindNext and FindPrevious:

• Insert: insert a key/value pair with an m-bit key
• Delete: remove the key/value pair with a given key
• Lookup: find the value associated with a given key
• Find: find the key/value pair with the smallest key which is greater than a given k
• FindPrevious: find the key/value pair with the largest key which is smaller than a given k

A vEB tree also supports the operations Minimum and Maximum, which return the minimum and maximum element stored in the tree respectively. These both run in O(1) time, since the minimum and maximum element are stored as attributes in each tree.

## How it works An example van Emde Boas tree with dimension 5 and the root's aux structure after 1, 2, 3, 5, 8 and 10 have been inserted.

For the sake of simplicity, let log2 m = k for some integer k. Define M = 2m. A vEB tree T over the universe {0, ..., M−1} has a root node that stores an array T.children of length M. T.children[i] is a pointer to a vEB tree that is responsible for the values {iM, ..., (i+1)M−1}. Additionally, T stores two values T.min and T.max as well as an auxiliary vEB tree T.aux.

Data is stored in a vEB tree as follows: The smallest value currently in the tree is stored in T.min and largest value is stored in T.max. Note that T.min is not stored anywhere else in the vEB tree, while T.max is. If T is empty then we use the convention that T.max=−1 and T.min=M. Any other value x is stored in the subtree T.children[i] where i = ⌊x/M. The auxiliary tree T.aux keeps track of which children are non-empty, so T.aux contains the value j if and only if T.children[j] is non-empty.

### FindNext

The operation FindNext(T, x) that searches for the successor of an element x in a vEB tree proceeds as follows: If x<T.min then the search is complete, and the answer is T.min. If x≥T.max then the next element does not exist, return M. Otherwise, let i = x/M. If x<T.children[i].max then the value being searched for is contained in T.children[i] so the search proceeds recursively in T.children[i]. Otherwise, we search for the value i in T.aux. This gives us the index j of the first subtree that contains an element larger than x. The algorithm then returns T.children[j].min. The element found on the children level needs to be composed with the high bits to form a complete next element.

function FindNext(T, x)
if x < T.min then
return T.min
if x ≥ T.max then // no next element
return M
i = floor(x/√M)
lo = x mod √M

if lo < T.children[i].max then
return (√M i) + FindNext(T.children[i], lo)
j = FindNext(T.aux, i)
return (√M j) + T.children[j].min
end


Note that, in any case, the algorithm performs O(1) work and then possibly recurses on a subtree over a universe of size M1/2 (an m/2 bit universe). This gives a recurrence for the running time of $T(m)=T(m/2)+O(1)$ , which resolves to O(log m) = O(log log M).

### Insert

The call insert(T, x) that inserts a value x into a vEB tree T operates as follows:

1. If T is empty then we set T.min = T.max = x and we are done.
2. Otherwise, if x<T.min then we insert T.min into the subtree i responsible for T.min and then set T.min = x. If T.children[i] was previously empty, then we also insert i into T.aux
3. Otherwise, if x>T.max then we insert x into the subtree i responsible for x and then set T.max = x. If T.children[i] was previously empty, then we also insert i into T.aux
4. Otherwise, T.min< x < T.max so we insert x into the subtree i responsible for x. If T.children[i] was previously empty, then we also insert i into T.aux.

In code:

function Insert(T, x)
if T.min > T.max then // T is empty
T.min = T.max = x;
return
if x < T.min then
swap(x, T.min)
if x > T.max then
T.max = x
i = floor(x / √M)
lo = x mod √M
Insert(T.children[i], lo)
if T.children[i].min == T.children[i].max then
Insert(T.aux, i)
end


The key to the efficiency of this procedure is that inserting an element into an empty vEB tree takes O(1) time. So, even though the algorithm sometimes makes two recursive calls, this only occurs when the first recursive call was into an empty subtree. This gives the same running time recurrence of $T(m)=T(m/2)+O(1)$ as before.

### Delete

Deletion from vEB trees is the trickiest of the operations. The call Delete(T, x) that deletes a value x from a vEB tree T operates as follows:

1. If T.min = T.max = x then x is the only element stored in the tree and we set T.min = M and T.max = −1 to indicate that the tree is empty.
2. Otherwise, if x == T.min then we need to find the second-smallest value y in the vEB tree, delete it from its current location, and set T.min=y. The second-smallest value y is T.children[T.aux.min].min, so it can be found in O(1) time. We delete y from the subtree that contains it.
3. If x≠T.min and x≠T.max then we delete x from the subtree T.children[i] that contains x.
4. If x == T.max then we will need to find the second-largest value y in the vEB tree and set T.max=y. We start by deleting x as in previous case. Then value y is either T.min or T.children[T.aux.max].max, so it can be found in O(1) time.
5. In any of the above cases, if we delete the last element x or y from any subtree T.children[i] then we also delete i from T.aux

In code:

function Delete(T, x)
if T.min == T.max == x then
T.min = M
T.max = −1
return
if x == T.min then
hi = T.aux.min * √M
j = T.aux.min
T.min = x = hi + T.children[j].min
i = floor(x / √M)
lo = x mod √M
Delete(T.children[i], lo)
if T.children[i] is empty then
Delete(T.aux, i)
if x == T.max then
if T.aux is empty then
T.max = T.min
else
hi = T.aux.max * √M
j = T.aux.max
T.max = hi + T.children[j].max
end


Again, the efficiency of this procedure hinges on the fact that deleting from a vEB tree that contains only one element takes only constant time. In particular, the last line of code only executes if x was the only element in T.children[i] prior to the deletion.

### Python implementation

from math import ceil, log2

"""
van Emde Boas Tree is a data structure which gives O(log(log(u))
query time for operations like
insert, search, delete, successor and predecessor
VEB class contains attribute
min, max, u, w, cluster and summary
initially min=max=NULL
u = size of universe (the range of total possible entries)
w = word length (number of bits in u)
w = log2(u)
cluster is an array of VEB structures of size of sqrt(u)
summary is a VEB of size sqrt(u)
when the size of VEB structure reaches we don't store clusters and summary vector
min and max are enough to store this structure.
"""

class VEB:
"""Index of x can be determined as the
cluster number and the position inside the cluster
for example lets consider 11
in binary it is written as 1011
so first half parts of the binary strinig give cluster number
and 2nd half gives the postiton inside cluster
cluster number= int(10)= 2
position inside cluster= int(11)=3
so 11 is in 2nd cluster at 3rd position
where counting starts from 0th position
0,1,2,3|4,5,6,7|8,9,10,11|12,13,14,15
^
here we use 'c' to denote cluster number
and 'i' to denote index inside the cluster
so x can be represented as <c,i>
where x = c * sqrt(u) + i
"""

def high(self, x):
# high(x)=x//int(sqrt(u))
return x >> (self.w // 2)

def low(self, x):
# low(x)= x%int(sqrt(u))
return x & (1 << (self.w // 2)) - 1

def index(self, i, j):
# return i*int(sqrt(self.u))+j
return i << (self.w // 2) | j

def __init__(self, u):
"""
This have been implemented using hash table
to reduce the space complexity from O(U) to O(n*log(log(u))
because u can be very large. for example if word size = 64 bits
u= 2^64 = 16 million TB which can't be stored practically on ram.
where as n*log*log*u can be O(3n) which can be easily stored.
I have a different code for array implementation too.
"""

self.w = ceil(log2(u))
# self.u = 2 ** self.w
self.min = self.max = None

if self.w >= 1:  # when u==2^w=2 min and max are enough so we stop recursion
self.cluster = {}
self.summary = None

def member(self, x):
"""Function to check if x is present in tree or not."""
if x == self.min or x == self.max:
return True
elif self.w == 1:
return False
else:
c = self.high(x)
i = self.low(x)
if c in self.cluster:
return self.cluster[c].member(i)
else:
return False

def insert(self, x):
if self.min is None:
self.min = x
self.max = x
return
else:
if x < self.min:
x, self.min = self.min, x
c = self.high(x)
i = self.low(x)
if self.w > 1:
if c not in self.cluster:
self.cluster[c] = VEB(2 ** (self.w // 2))
if self.cluster[c].min is None:
if self.summary is None:
self.summary = VEB(2 ** (self.w // 2))
self.summary.insert(c)
if c not in self.cluster:
self.cluster[c] = VEB(2 ** (self.w // 2))
self.cluster[c].insert(i)
if x > self.max:
self.max = x

def succesor(self, x):
if self.w == 1:
if x == 0 and self.max == 1:
return 1
else:
return None
elif self.min is not None and x < self.min:
return self.min
else:
c = self.high(x)
i = self.low(x)
if c in self.cluster:
maxlow = self.cluster[c].max
else:
maxlow = None
if maxlow is not None and i < maxlow:
offset = self.cluster[c].succesor(i)
return self.index(c, offset)
else:
if self.summary is not None:
succ_cluster = self.summary.succesor(self.high(x))
else:
succ_cluster = None
if succ_cluster is None:
return None
else:
offset = self.cluster[succ_cluster].min
return self.index(succ_cluster, offset)

def predecessor(self, x):
if self.w == 1:
if x == 1 and self.min == 0:
return 0
else:
return None
elif self.max is not None and x > self.max:
return self.max
else:
c = self.high(x)
i = self.low(x)
if c in self.cluster:
min_low = self.cluster[c].min
else:
min_low = None
if min_low is not None and i > min_low:
offset = self.cluster[c].predecessor(i)
return self.index(c, offset)
else:
if self.summary is not None:
prev_cluster = self.summary.predecessor(c)
else:
prev_cluster = None
if prev_cluster is None:
if self.min is not None and x > self.min:
return self.min
else:
return None
else:
offset = self.cluster[prev_cluster].max
return self.index(prev_cluster, offset)

def delete(self, x):
if self.min is None:
return
if x < self.min or x > self.max:
return
if self.min == self.max:
self.min = self.max = None
elif self.w == 1:
if x == 0:
self.min = 1
else:
self.min = 0
self.max = self.min
else:
c = self.high(x)
i = self.low(x)
if x == self.min:
if self.summary:
first_cluster = self.summary.min
else:
first_cluster = None
if first_cluster:
x = self.index(first_cluster, self.cluster[first_cluster].min)
self.min = x
if c in self.cluster:
self.cluster[c].delete(i)
if self.cluster[c].min is None:
self.summary.delete(c)
if x == self.max:
summary_max = self.summary.max
if summary_max is None:
self.max = self.min
else:
self.max = self.index(summary_max, self.cluster[summary_max].max)
elif x == self.max:
self.max = self.index(c, self.cluster[c].max)


### Discussion

The assumption that log m is an integer is unnecessary. The operations $x{\sqrt {M}}$ and $x{\bmod {\sqrt {M}}}$ can be replaced by taking only higher-order m/2⌉ and the lower-order m/2⌋ bits of x, respectively. On any existing machine, this is more efficient than division or remainder computations.

The implementation described above uses pointers and occupies a total space of O(M) = O(2m). This can be seen as follows. The recurrence is $S(M)=O({\sqrt {M}})+({\sqrt {M}}+1)\cdot S({\sqrt {M}})$ . Resolving that would lead to $S(M)\in (1+{\sqrt {M}})^{\log \log M}+\log \log M\cdot O({\sqrt {M}})$ . One can, fortunately, also show that S(M) = M−2 by induction.

In practical implementations, especially on machines with shift-by-k and find first zero instructions, performance can further be improved by switching to a bit array once m equal to the word size (or a small multiple thereof) is reached. Since all operations on a single word are constant time, this does not affect the asymptotic performance, but it does avoid the majority of the pointer storage and several pointer dereferences, achieving a significant practical savings in time and space with this trick.

An obvious optimization of vEB trees is to discard empty subtrees. This makes vEB trees quite compact when they contain many elements, because no subtrees are created until something needs to be added to them. Initially, each element added creates about log(m) new trees containing about m/2 pointers all together. As the tree grows, more and more subtrees are reused, especially the larger ones. In a full tree of 2m elements, only O(2m) space is used. Moreover, unlike a binary search tree, most of this space is being used to store data: even for billions of elements, the pointers in a full vEB tree number in the thousands.

However, for small trees the overhead associated with vEB trees is enormous: on the order of ${\sqrt {M}}$ . This is one reason why they are not popular in practice. One way of addressing this limitation is to use only a fixed number of bits per level, which results in a trie. Alternatively, each table may be replaced by a hash table, reducing the space to O(n log M) (where n is the number of elements stored in the data structure) at the expense of making the data structure randomized. Other structures, including y-fast tries and x-fast tries have been proposed that have comparable update and query times and also use randomized hash tables to reduce the space to O(n) or O(n log M).