-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set.h
390 lines (339 loc) · 10.5 KB
/
Set.h
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* Template analogue of std::set, based on AVL tree.
* Template type must have operator <.
* It supports standard set operations in guaranteed O(log(tree_size)):
* - insert
* - erase
* - find
* - lower_bound
*/
template<class T>
class Set {
public:
// Invariant of root!=null is needed for always having node, representing end() iterator.
Set() { root = new Node(); }
Set(const Set<T>& other) {
root = new Node();
for (const T& val : other) {
insert(val);
}
}
// Assignment operator
Set& operator=(const Set& other) {
if (&other == this) {
return *this;
}
destroy();
root->left = nullptr;
node_count = 0;
for (T val : other) {
insert(val);
}
return *this;
}
Set(const std::initializer_list<T>& elems) {
root = new Node();
for (const T& val : elems) {
insert(val);
}
}
template<typename Iterator>
Set(const Iterator first, const Iterator last) {
root = new Node();
for (Iterator it = first; it != last; ++it) {
insert(*it);
}
}
class iterator;
// If needed value exists, returns iterator on corresponding node, otherwise end().
iterator find(const T& val) const {
Node* cur = root->left;
while (cur != nullptr) {
if (val < cur->value) {
cur = cur->left;
} else if (cur->value < val) {
cur = cur->right;
} else {
return iterator(cur);
}
}
return end();
}
// Returns iterator on node with the lowest key >= val.
iterator lower_bound(const T& val) const {
Node* cur = root->left;
Node* ans = root;
while (cur) {
if (cur->value < val) {
cur = cur->right;
} else {
ans = cur;
cur = cur->left;
}
}
return iterator(ans);
}
iterator begin() const {
Node* ans = root;
while (ans->left) {
ans = ans->left;
}
return iterator(ans);
}
iterator end() const { return iterator(root); }
// Inserts element in tree. If such exists, does nothing.
void insert(const T& val) {
root->left = recursive_insert(root->left, val);
root->left->parent = root;
}
// Erases element from tree. If such doesn't exist, does nothing.
void erase(const T& val) {
root->left = recursive_erase(root->left, val);
if (root->left) root->left->parent = root;
}
size_t size() const { return node_count; }
bool empty() const { return node_count == 0; }
~Set() { destroy(root); }
private:
// Auxiliary class for storing node's information.
struct Node {
static constexpr int32_t UNDEFINED = -1;
static constexpr int32_t LEFT = 0;
static constexpr int32_t RIGHT = -1;
Node* parent = nullptr;
Node* left = nullptr;
Node* right = nullptr;
int32_t height = UNDEFINED;
T value;
Node() = default;
explicit Node(const T& val) {
value = val;
height = 0;
}
explicit Node(Node* other) {
parent = other->parent;
left = other->left;
right = other->right;
height = other->height;
value = other->value;
}
};
public:
/*
* Bidirectional iterator for AVL tree nodes
* Doesn't support random access
* Prefix/postfix increment/decrement works in amortized O(1)
* */
class iterator {
public:
iterator() = default;
explicit iterator(Node* node_) { node = node_; }
bool operator==(const iterator& it) const { return it.node == node; }
bool operator==(const iterator&& it) const { return it.node == node; }
bool operator!=(const iterator& it) const { return it.node != node; }
bool operator!=(const iterator&& it) const { return it.node != node; }
T operator*() const { return node->value; }
T* operator->() const { return &node->value; }
iterator& operator++() {
node = get_next_vertex(node);
return *this;
}
iterator operator++(int) {
iterator temp = iterator(node);
++(*this);
return temp;
}
iterator& operator--() {
node = get_prev_vertex(node);
return *this;
}
iterator operator--(int) {
iterator temp = iterator(node);
--(*this);
return temp;
}
private:
int get_parent_direction(Node* child) {
return child->parent->left == child ? Node::LEFT : Node::RIGHT;
}
Node* get_leftest_node(Node* node) {
while (node->left != nullptr) {
node = node->left;
}
return node;
}
Node* get_rightest_node(Node* node) {
while (node->right) {
node = node->right;
}
return node;
}
// Auxiliary method for finding next node in tree.
Node* get_next_vertex(Node* node) {
if (node->right != nullptr) {
return get_leftest_node(node->right);
}
while (node->parent) {
int32_t dir = get_parent_direction(node);
node = node->parent;
if (dir == Node::LEFT) {
return node;
}
}
return nullptr;
}
// Auxiliary method for finding previous node in tree.
Node* get_prev_vertex(Node* node) {
if (node->left != nullptr) {
return get_rightest_node(node->left);
}
while (node->parent) {
int32_t dir = get_parent_direction(node);
node = node->parent;
if (dir == Node::RIGHT) {
return node;
}
}
return nullptr;
}
Node* node = nullptr;
};
private:
Node* get_leftest_node(Node* node) {
while (node->left != nullptr) {
node = node->left;
}
return node;
}
int32_t get_height(Node* node) { return node == nullptr ? Node::UNDEFINED : node->height; }
int32_t height_difference(Node* node) {
return node ? get_height(node->left) - get_height(node->right) : 0;
}
// Auxiliary function for deleting tree and releasing memory.
void destroy(Node* node) {
if (node == nullptr) {
return;
}
destroy(node->left);
destroy(node->right);
delete node;
}
void destroy() { destroy(root->left); }
void update_height(Node* node) {
node->height = 1 + std::max(get_height(node->left), get_height(node->right));
}
Node* left_rotate(Node* node) {
Node* temp = node->right;
node->right = temp->left;
if (temp->left) {
temp->left->parent = node;
}
temp->left = node;
temp->parent = node->parent;
node->parent = temp;
update_height(node);
update_height(temp);
return temp;
}
Node* right_rotate(Node* node) {
Node* temp = node->left;
node->left = temp->right;
if (temp->right) {
temp->right->parent = node;
}
temp->right = node;
temp->parent = node->parent;
node->parent = temp;
update_height(node);
update_height(temp);
return temp;
}
Node* big_left_rotate(Node* node) {
node->right = right_rotate(node->right);
return left_rotate(node);
}
Node* big_right_rotate(Node* node) {
node->left = left_rotate(node->left);
return right_rotate(node);
}
// Standard AVL's rotate implementation.
Node* rotate(Node* node) {
int32_t n_diff = height_difference(node);
int32_t l_diff = height_difference(node->left);
int32_t r_diff = height_difference(node->right);
if (n_diff == -TWO) {
if (r_diff == ONE) {
node = big_left_rotate(node);
} else {
node = left_rotate(node);
}
} else if (n_diff == TWO) {
if (l_diff == -ONE) {
node = big_right_rotate(node);
} else {
node = right_rotate(node);
}
}
return node;
}
// Auxiliary function for erasing element from tree.
Node* recursive_erase(Node* node, const T& val) {
if (node == nullptr) {
return node;
}
if (val < node->value) {
node->left = recursive_erase(node->left, val);
if (node->left) {
node->left->parent = node;
}
} else if (node->value < val) {
node->right = recursive_erase(node->right, val);
if (node->right) {
node->right->parent = node;
}
} else {
if (node->left && node->right) {
Node* temp = get_leftest_node(node->right);
node->value = temp->value;
node->right = recursive_erase(node->right, temp->value);
if (node->right) {
node->right->parent = node;
}
} else {
Node* temp = node->left ? node->left : node->right;
delete node;
--node_count;
node = temp;
if (node) {
node->parent = nullptr;
}
}
}
if (node == nullptr) {
return node;
}
node = rotate(node);
update_height(node);
return node;
}
// Auxiliary function for inserting element from tree.
Node* recursive_insert(Node* node, const T& val) {
if (node == nullptr) {
node = new Node(val);
++node_count;
} else if (val < node->value) {
node->left = recursive_insert(node->left, val);
node->left->parent = node;
} else if (node->value < val) {
node->right = recursive_insert(node->right, val);
node->right->parent = node;
}
node = rotate(node);
update_height(node);
return node;
}
size_t node_count = 0;
Node* root;
static constexpr int32_t ONE = 1;
static constexpr int32_t TWO = 2;
};