van Emde Boas tree | |
Type: | Non-binary tree |
Invented By: | Peter van Emde Boas |
Invented Year: | 1975 |
Space Avg: | O(M) |
Space Worst: | O(M) |
Search Avg: | O(loglogM) |
Search Worst: | O(loglogM) |
Insert Avg: | O(loglogM) |
Insert Worst: | O(loglogM) |
Delete Avg: | O(loglogM) |
Delete Worst: | O(loglogM) |
A van Emde Boas tree (in Dutch; Flemish pronounced as /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 -bit integer keys. It was invented by a team led by Dutch computer scientist Peter van Emde Boas in 1975.[1] It performs all operations in time (assuming that an
m
O(loglogM)
M=2m
M
The standard vEB tree has inadequate space efficiency. For example, for storing 32-bit integers (i.e., when
m=32
M=232
O(n)
n
O(nlogM)
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:[2]
A vEB tree also supports the operations Minimum and Maximum, which return the minimum and maximum element stored in the tree respectively.[3] These both run in
O(1)
Let
log2m=k
k
M=2m
\{0,\ldots,M-1\}
\sqrt{M}
\{i\sqrt{M},\ldots,(i+1)\sqrt{M}-1\}
Data is stored in a vEB tree as follows: The smallest value currently in the tree is stored in and largest value is stored in . Note that is not stored anywhere else in the vEB tree, while is. If T is empty then we use the convention that and . Any other value
x
i=\lfloorx/\sqrt{M}\rfloor
j
The operation that searches for the successor of an element x in a vEB tree proceeds as follows: If then the search is complete, and the answer is . If then the next element does not exist, return M. Otherwise, let
i=\lfloorx/\sqrt{M}\rfloor
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/
\sqrt{M}
\sqrt{M}
\sqrt{M}
\sqrt{M}
Note that, in any case, the algorithm performs
O(1)
M1/2
m/2
T(m)=T(m/2)+O(1)
O(logm)=O(loglogM)
The call that inserts a value into a vEB tree operates as follows:
In code: function Insert(T, x) if T.min
x then // x is already inserted return 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 /
\sqrt{M}
\sqrt{M}
The key to the efficiency of this procedure is that inserting an element into an empty vEB tree takes 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 as before.
Deletion from vEB trees is the trickiest of the operations. The call that deletes a value x from a vEB tree T operates as follows:
In code: function Delete(T, x) if T.min
x then T.min = M T.max = −1 return if x
\sqrt{M}
\sqrt{M}
\sqrt{M}
\sqrt{M}
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 second Delete call only executes if x was the only element in prior to the deletion.
The assumption that is an integer is unnecessary. The operations
x\sqrt{M}
x\bmod\sqrt{M}
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 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 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 new trees containing about pointers all together. As the tree grows, more and more subtrees are reused, especially the larger ones. In a full tree of elements, only 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.
The implementation described above uses pointers and occupies a total space of, proportional to the size of the key universe. This can be seen as follows. The recurrence is
S(M)=O(\sqrt{M})+(\sqrt{M}+1) ⋅ S(\sqrt{M})
S(M)\in(1+\sqrt{M})log+loglogM ⋅ O(\sqrt{M})
The space usage of vEB trees is an enormous overhead unless a large fraction of the universe of keys is being stored. This is one reason why vEB trees are not popular in practice. This limitation can be addressed by changing the array used to store children to another data structure. One possibility is to use only a fixed number of bits per level, which results in a trie. Alternatively, each array may be replaced by a hash table, reducing the space to (where is the number of elements stored in the data structure) at the expense of making the data structure randomized.
x-fast tries and the more complicated y-fast tries have comparable update and query times to vEB trees and use randomized hash tables to reduce the space used. x-fast tries use space while y-fast tries use space.
Fusion trees are another type of tree data structure that implements an associative array on w-bit integers on a finite universe. They use word-level parallelism and bit manipulation techniques to achieve time for predecessor/successor queries and updates, where is the word size.[5] Fusion trees use space and can be made dynamic with hashing or exponential trees.
There is a verified implementation in Isabelle (proof assistant).[6] Both functional correctness and time bounds are proved.Efficient imperative Standard ML code can be generated.