# Proto Van Emde Boas Tree | Set 2 | Construction

Van Emde Boas Tree supports search, minimum, maximum, successor, predecessor, insert and delete operations in O(lglgN) time which is faster than any of related data structures like priority queue, binary search tree, etc. Proto Van Emde Boas tree is similar prototype type data structure but it fails to achieve the complexity of O(lglgN), We will learn Proto Van Emde Boas tree first to get a basic understanding of working of Van Emde Boas tree. Here N is the size of the universe over which tree is defined.

**Note:** Proto Van Emde Boas Data Structure’s keys must be defined over a range of 0 to n(n is positive integer of the form 2^{2k}) and it works when duplicate keys are not allowed.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.

**Abbreviations:**

- Proto-VEB is an abbreviation of Proto-Van Emde Boas tree.
- Proto-VEB() is an abbreviation for Proto-VEB containing u number of keys.

**Basic Understanding of the structure of Proto-VEB Tree:**

Proto Van Emde boas tree are recursively defined data structure and it shrinks to sqrt size as we go up in the tree. Please refer this article to understand the basics of it.

In Proto-VEB we use a bit array to represents whether a key is present or not, we put 1 if it is present and 0 else.

Here, the summary of a particular cluster contains whether there is any key present in a cluster if at least one key present then the summary is 1 or 0 elsewhere. Clusters are segments of a bit array. A summary is also a bit array. See the image below:

**Construction of Proto VEB Tree:**

Below image represents the basic Proto-VEB structure:

The recursively defined structure has two main part:

**summary**: it is a pointer to Proto-VEB structure which has size.**cluster:**it is an array of pointers to Proto-VEB structures with size.

First of all, we have to understand some function and keywords:

**universe size (u)**: Number of keys in Proto-VEB structure.**High(x):**From the first image, we can see that if we want to reach at the cluster of the key then we can divide it with .__For example__, We want to know the cluster of the key 12 then can divide it with which is 3, so key 12 is in 3^{rd}cluster.

High(x) = floor( x / )

**low(x):**From the first image, we can see that if we want the position of the key in the cluster we can apply modulus operation x % .__For example__, If you want to find a position of 7 in the cluster you can apply 7 % = 3 which is a position of 7 in 2^{nd}cluster.

low(x) = x % )

**Recursive Procedure of construction: **

- Base case : If universe size is 2 then it is a base size so there will be no more summary array, which means it is null and we will store only bit array for 2 keys.
- We will recursively assign summary as sized Proto-VEB tree and -sized Proto-VEB to all clusters.

See u=4 Proto-VEB structure in the image below:

Here is the code representing the Algorithm:

## C++

`#include <bits/stdc++.h>` `using` `namespace` `std;` `class` `Proto_Van_Emde_Boas {` `public` `:` ` ` `// Total number of keys` ` ` `int` `universe_size;` ` ` `// Summary` ` ` `Proto_Van_Emde_Boas* summary;` ` ` `// Clusters array of Proto-VEB pointers` ` ` `vector<Proto_Van_Emde_Boas*> clusters;` ` ` `int` `root(` `int` `u)` ` ` `{` ` ` `return` `(` `int` `)` `sqrt` `(u);` ` ` `}` ` ` `// Function to return cluster numbers` ` ` `// in which key is present` ` ` `int` `high(` `int` `x)` ` ` `{` ` ` `return` `x / root(universe_size);` ` ` `}` ` ` `// Function to return the position` ` ` `// of x in cluster` ` ` `int` `low(` `int` `x)` ` ` `{` ` ` `return` `x % root(universe_size);` ` ` `}` ` ` `// Function to return index form` ` ` `// cluster number and position` ` ` `int` `generate_index(` `int` `cluster, ` `int` `position)` ` ` `{` ` ` `return` `cluster * root(universe_size) + position;` ` ` `}` ` ` `// Constructor` ` ` `Proto_Van_Emde_Boas(` `int` `size)` ` ` `{` ` ` `universe_size = size;` ` ` `// Base case` ` ` `if` `(size <= 2) {` ` ` `// Set summary to nullptr as there is no` ` ` `// more summary for size 2` ` ` `summary = nullptr;` ` ` `// Vector of two pointers` ` ` `// nullptr in starting` ` ` `clusters = vector<Proto_Van_Emde_Boas*>(size, nullptr);` ` ` `}` ` ` `else` `{` ` ` `// Assigning Proto-VEB(sqrt(u)) to summary` ` ` `summary = ` `new` `Proto_Van_Emde_Boas(root(size));` ` ` `// Creating array of Proto-VEB Tree pointers of size sqrt(u)` ` ` `// first all nullptrs are going to assign` ` ` `clusters = vector<Proto_Van_Emde_Boas*>(root(size), nullptr);` ` ` `// Assigning Proto-VEB(sqrt(u)) to all its clusters` ` ` `for` `(` `int` `i = 0; i < root(size); i++) {` ` ` `clusters[i] = ` `new` `Proto_Van_Emde_Boas(root(size));` ` ` `}` ` ` `}` ` ` `}` `};` `// Driver code` `int` `main()` `{` ` ` `Proto_Van_Emde_Boas pveb(4);` `}` |

**Time Complexity : **O(sqrt(n))