-
Notifications
You must be signed in to change notification settings - Fork 368
/
Van Emde Boas.cpp
200 lines (156 loc) · 5.55 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
#include <iostream>
#include <cmath>
#include <vector>
class VEBTree {
private:
int universeSize;
int minimum;
int maximum;
VEBTree* summary;
std::vector<VEBTree*> cluster;
public:
VEBTree(int size) : universeSize(size), minimum(-1), maximum(-1), summary(nullptr) {
if (size > 2) {
int upperSize = static_cast<int>(std::ceil(std::sqrt(size)));
int lowerSize = static_cast<int>(std::floor(std::sqrt(size)));
summary = new VEBTree(upperSize);
cluster.resize(upperSize, nullptr);
for (int i = 0; i < upperSize; i++)
cluster[i] = new VEBTree(lowerSize);
}
}
~VEBTree() {
if (summary != nullptr)
delete summary;
for (VEBTree* subTree : cluster) {
if (subTree != nullptr)
delete subTree;
}
}
void insert(int key) {
if (minimum == -1) {
minimum = key;
maximum = key;
return;
}
if (key < minimum) {
std::swap(key, minimum);
}
if (universeSize > 2) {
int high = key / static_cast<int>(std::ceil(std::sqrt(universeSize)));
int low = key % static_cast<int>(std::ceil(std::sqrt(universeSize)));
if (cluster[high] == nullptr) {
cluster[high] = new VEBTree(static_cast<int>(std::sqrt(universeSize)));
summary->insert(high);
}
cluster[high]->insert(low);
}
if (key > maximum)
maximum = key;
}
void remove(int key) {
if (minimum == maximum) {
minimum = -1;
maximum = -1;
return;
}
if (universeSize == 2) {
if (key == 0)
minimum = 1;
else
minimum = 0;
maximum = minimum;
return;
}
if (key == minimum) {
int firstCluster = summary->minimum;
key = firstCluster * static_cast<int>(std::ceil(std::sqrt(universeSize))) + cluster[firstCluster]->minimum;
minimum = key;
}
int high = key / static_cast<int>(std::ceil(std::sqrt(universeSize)));
int low = key % static_cast<int>(std::ceil(std::sqrt(universeSize)));
cluster[high]->remove(low);
if (cluster[high]->minimum == -1) {
summary->remove(high);
}
if (key == maximum) {
if (summary->minimum == -1)
maximum = minimum;
else {
int lastCluster = summary->maximum;
maximum = lastCluster * static_cast<int>(std::ceil(std::sqrt(universeSize))) + cluster[lastCluster]->maximum;
}
}
}
int successor(int key) {
if (universeSize == 2) {
if (key == 0 && maximum == 1)
return 1;
else
return -1;
}
if (minimum != -1 && key < minimum)
return minimum;
int high = key / static_cast<int>(std::ceil(std::sqrt(universeSize)));
int low = key % static_cast<int>(std::ceil(std::sqrt(universeSize)));
if (cluster[high] != nullptr && low < cluster[high]->maximum) {
int offset = cluster[high]->successor(low);
return high * static_cast<int>(std::ceil(std::sqrt(universeSize))) + offset;
}
int succCluster = summary->successor(high);
if (succCluster == -1)
return -1;
int offset = cluster[succCluster]->minimum;
return succCluster * static_cast<int>(std::ceil(std::sqrt(universeSize))) + offset;
}
int predecessor(int key) {
if (universeSize == 2) {
if (key == 1 && minimum == 0)
return 0;
else
return -1;
}
if (maximum != -1 && key > maximum)
return maximum;
int high = key / static_cast<int>(std::ceil(std::sqrt(universeSize)));
int low = key % static_cast<int>(std::ceil(std::sqrt(universeSize)));
if (cluster[high] != nullptr && low > cluster[high]->minimum) {
int offset = cluster[high]->predecessor(low);
return high * static_cast<int>(std::ceil(std::sqrt(universeSize))) + offset;
}
int predCluster = summary->predecessor(high);
if (predCluster == -1) {
if (minimum != -1 && key > minimum)
return minimum;
else
return -1;
}
int offset = cluster[predCluster]->maximum;
return predCluster * static_cast<int>(std::ceil(std::sqrt(universeSize))) + offset;
}
bool contains(int key) {
if (key == minimum || key == maximum)
return true;
if (universeSize == 2)
return false;
int high = key / static_cast<int>(std::ceil(std::sqrt(universeSize)));
int low = key % static_cast<int>(std::ceil(std::sqrt(universeSize)));
if (cluster[high] != nullptr)
return cluster[high]->contains(low);
return false;
}
};
int main() {
VEBTree vebTree(16);
vebTree.insert(4);
vebTree.insert(1);
vebTree.insert(8);
vebTree.insert(10);
std::cout << vebTree.contains(4) << std::endl; // Output: 1 (true)
std::cout << vebTree.contains(5) << std::endl; // Output: 0 (false)
std::cout << vebTree.successor(4) << std::endl; // Output: 8
std::cout << vebTree.predecessor(4) << std::endl; // Output: 1
vebTree.remove(4);
std::cout << vebTree.contains(4) << std::endl; // Output: 0 (false)
return 0;
}