-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvan_emde_boas.cpp
335 lines (285 loc) · 6.86 KB
/
van_emde_boas.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <bits/stdc++.h>
#define NIL -1
using namespace std;
class VEB_tree{
int u; // universal set size
int min; // minimum in cluster
int max; // maximum in cluster
VEB_tree* summary; // summary of cluster
vector<VEB_tree*> cluster;
int high(int x);
int low(int x);
int index(int x, int y);
int lowerRoot(int n);
int upperRoot(int n);
public:
// recursively creating the tree
VEB_tree(int size){
u = size;
min = NIL;
max = NIL;
// Base case
if (size <= 2) {
summary = nullptr;
cluster = vector<VEB_tree*>(0, nullptr);
}
else {
int upperSize= upperRoot(u);
summary = new VEB_tree(upperSize);
cluster = vector<VEB_tree*>(upperSize, nullptr);
for (int i = 0; i < upperSize; i++) {
cluster[i] = new VEB_tree(lowerRoot(u));
}
}
}
int getMin();
int getMax();
int extractMin();
void insert(int k);
void init_tree(int *arr, int size);
bool find(int n);
int successor(int n);
void del_element(int key);
void printTree();
};
// used to find upper root
int VEB_tree::upperRoot(int x){
return (int) pow( 2,ceil(log2(x)/2) );
}
//used to find lower root
int VEB_tree::lowerRoot(int x){
return (int) pow( 2,floor(log2(x)/2) );
}
// used to find the high bits of x
int VEB_tree:: high(int x){
return x / lowerRoot(u);
}
// used to find the low bits of x
int VEB_tree:: low(int x){
return x % lowerRoot(u);
}
// used to create an index A such that
// high(A) =x, low(A)=y
int VEB_tree:: index(int x, int y){
return x * lowerRoot(u) + y;
}
// gets the minimum element in the node
int VEB_tree:: getMin(){
return min != NIL ? min : NIL;
}
// gets the maximum element in the node
int VEB_tree:: getMax(){
return (max == NIL ? NIL : max);
}
// inserts elements into the tree
void VEB_tree:: insert(int key){
// check if node is empty
if (min == NIL) {
min = key;
max = key;
}
else {
// inserts element if base case, else just update min
if (key < min) {
swap(min, key);
}
// for non base case
if (u > 2)
{
// If no key is present in the cluster
if (cluster[high(key)]->getMin() == NIL) {
summary->insert(high(key));
// Sets the min and max of current empty cluster
cluster[ high(key)]->min = low(key);
cluster[ high(key)]->max = low(key);
}
else {
// since other elements present in cluster
// move deeper and repeat
cluster[ high(key)]->insert(low(key));
}
}
// if key is new max
if (key > max) {
max = key;
}
}
}
//initialises the tree with values given in array
void VEB_tree:: init_tree( int *arr, int size ){
for(int i=0; i<size; i++) {
insert(arr[i]);
}
}
//searches for an element, returns true if found
bool VEB_tree:: find(int key){
// for elements exceeding u or not existing
if (u < key)
return false;
if ( min == key || max == key)
return true;
else {
if (u == 2)
return false;
else
return cluster[high(key)]->find(low(key));
}
}
// finds the next element in sequence,
// used for printing the keys in the tree
int VEB_tree:: successor(int key){
// base case: if key is stored in 0 index
if (u == 2) {
if (key == 0 && max == 1)
return 1;
else
return NIL;
}
// when key less than current min
else if (min != NIL && key < min){
return min;
}
else
{
// finding successor in cluster
int maxClusterK = cluster[high(key)]->getMax();
int offset, sucessorClusterK;
// when maxclusterk is greater than key, return
if (maxClusterK != NIL && low(key) < maxClusterK) {
offset = cluster[high(key)]->successor(low(key));
return index(high(key), offset);
}
else // searching for cluster with any key
{
sucessorClusterK = summary->successor(high(key));
// when no element is in cluster
// according to to summary
if (sucessorClusterK == NIL) {
return NIL;
}
// pick min element in successor cluster
else {
offset = cluster[sucessorClusterK]->getMin();
return index(sucessorClusterK, offset);
}
}
}
}
// deletes elements
void VEB_tree:: del_element(int key){
// only one element in node
if (max == min) {
min = NIL;
max = NIL;
}
// base case if min!=max
else if (u == 2) {
if (key == 0)
min = 1;
else
min = 0;
max = min;
}
else {
// find next big element and assign
// it as min
if (key == min)
{
int first_cluster = summary->getMin();
key = index(first_cluster, cluster[first_cluster]->getMin() );
min = key;
}
// Now delete the key
cluster[high(key)]->del_element(low(key));
// if cluster is empty then we update the summary
if (cluster[high(key)]->getMin() == NIL){
summary->del_element(high(key));
if (key == max) {
int max_insummary = summary->getMax();
// max isn't assigned a value yet, only one
// key is present, which is min. update max to min
if (max_insummary == NIL)
max = min;
else {
// else set max to the max of the child clusters
max = index(max_insummary, cluster[max_insummary]->getMax());
}
}
}
//find the next max key in the same cluster as key
// set max to new value of max
else if (key == max) {
max= index(high(key), cluster[high(key)]->getMax());
}
}
}
// finds the element and then deletes it
int VEB_tree:: extractMin(){
int x = getMin();
del_element(x);
return x;
}
//prints the elements in the tree
void VEB_tree:: printTree(){
cout << "\nThe elements in the tree are:";
if(find(0)){
cout << "0, ";
}
int x=0;
while(true){
x=successor(x);
if(x!=NIL)
cout << x << ", ";
else
break;
}
cout << "\b\b ";
}
int main(){
VEB_tree V(1024); // u = 2^10
srand(0);
int init_size;
cout << "\nEnter the number of elements to insert:";
cin >> init_size;
// Inserting Keys
int *arr= new int[init_size];
cout << "\nEnter the keys to be inserted: (range is 0-1023)";
for(int i=0; i<init_size; i++){
int x;
cin >> x;
if(!V.find(x)){
V.insert(x);
}
else{
cout << x << " was already inserted.\n";
}
}
// prints the elements in the tree
V.printTree();
// deletion
int temp;
cout << "\nEnter the number of elements to delete:";
cin >>temp;
cout <<"\nEnter the elements to be deleted:";
for(int i=0; i<temp; i++){
int x;
cin >> x;
if(V.find(x)){
V.del_element(x);
cout << x << " is deleted.\n";
}
}
V.printTree();
cout << "\nEnter element to be searched for:";
cin>> temp;
if(V.find(temp)){
cout << "\nElement "<< temp<< " found!";
}
else
cout << "Element " << temp <<" not found!";
cout << "\n\nThe minimum element in tree is " << V.getMin();
cout << "\nMinimum element shall be extracted.";
V.extractMin();
V.printTree();
return 0;
}