-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.hpp
390 lines (360 loc) · 13.1 KB
/
tree.hpp
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
#pragma once
#include "traits.hpp"
#include "pair.hpp"
namespace ft {
template <class Value, class Compare, class Alloc = std::allocator <Value> >
class Tree {
public:
typedef Alloc allocator_type;
typedef typename allocator_type::pointer node_ptr;
private:
allocator_type _alloc;
Compare _compare;
public:
node_ptr create_node(Value toAdd) {
node_ptr new_node = _alloc.allocate(1);
_alloc.construct(new_node, toAdd);
return new_node;
}
void delete_node(node_ptr some) {
if (some) {
_alloc.destroy(some);
_alloc.deallocate(some, 1);
}
}
void swap_color(node_ptr some) {
bool color = some->isBlack;
some->isBlack = some->right->isBlack;
some->right->isBlack = color;
some->left->isBlack = color;
}
node_ptr min_node(node_ptr some) const {
if (some) {
while(some->left)
some = some->left;
}
return some;
}
node_ptr max_node(node_ptr some) const {
if (some) {
while(some->right)
some = some->right;
}
return some;
}
node_ptr find_node(node_ptr tmp, Value key) const {
if (tmp) {
if (_compare(tmp->pair, key))
return find_node(tmp->right, key);
else if (_compare(key, tmp->pair)){
return find_node(tmp->left, key);
}
}
return tmp;
}
node_ptr lower(node_ptr root, Value key) const {
node_ptr tmp = 0;
while (root) {
if (_compare(root->pair, key))
root = root->right;
else
{
if (root->left) {
tmp = lower(root->left, key);
if (tmp)
root = tmp;
break;
}
else
break;
}
}
return root;
}
void clear(node_ptr* root) {
if (*root == 0)
return ;
if (*root)
{
if ((*root)->left)
clear(&((*root)->left));
if ((*root)->right)
clear(&((*root)->right));
delete_node((*root));
}
*root = 0;
}
size_t max_size() const {
return _alloc.max_size();
}
void insert_balance(node_ptr* root, node_ptr some) {
node_ptr parent;
node_ptr grand;
node_ptr tmp;
while (some) {
parent = some->parent;
grand = 0;
tmp = parent;
if (parent) {
grand = parent->parent;
if (some->isBlack == false) {
if (parent->isBlack == true)
some = parent;
else if (grand) {
if (grand->left && grand->left->isBlack == false && grand->right && grand->right->isBlack == false) {
swap_color(grand);
some = grand;
}
else if (grand->right == parent) {
if (parent->left == some) {
rotate_right(parent, root);
parent = some;
}
if (parent->right == some)
rotate_left(grand, root);
some = parent->parent;
}
else {
if (parent->right == some) {
rotate_left(parent, root);
parent = some;
}
if (parent->left == some)
rotate_right(grand, root);
some = parent->parent;
}
}
}
else
some = parent;
}
else {
some->isBlack = true;
some = some->parent;
}
}
}
bool insert(node_ptr* root, node_ptr new_node) {
if (*root == 0) {
*root = new_node;
new_node->isBlack = true;
}
else {
node_ptr tmp = *root;
while (tmp) {
if (!_compare(tmp->pair, new_node->pair) && !_compare(new_node->pair, tmp->pair)) {
if (tmp != new_node)
delete_node(new_node);
return false;
}
else if (_compare(new_node->pair, tmp->pair)) {
if (tmp->left)
tmp = tmp->left;
else {
tmp->left = new_node;
new_node->parent = tmp;
break;
}
}
else {
if (tmp->right)
tmp = tmp->right;
else {
tmp->right = new_node;
new_node->parent = tmp;
break;
}
}
}
}
insert_balance(root, new_node);
return true;
}
void swap_p(node_ptr remove, node_ptr replace) {
if (remove->parent) {
if (remove->parent->left == remove)
remove->parent->left = replace;
else
remove->parent->right = replace;
}
replace->parent = remove->parent;
if (remove->left) {
remove->left->parent = replace;
}
replace->left = remove->left;
if (remove->right) {
remove->right->parent = replace;
}
replace->right = remove->right;
replace->isBlack = remove->isBlack;
}
bool erase(node_ptr* root, Value key) {
node_ptr remove = find_node(*root, key);
if (remove) {
node_ptr replace = 0;
if (remove->left)
replace = erase_left(remove, root);
else if (remove->right)
replace = erase_right(remove, root);
else
erase_not_child(remove, root);
if (replace)
swap_p(remove, replace);
if (remove == *root) {
if (replace)
*root = replace;
else
*root = 0;
}
delete_node(remove);
return true;
}
return false;
}
node_ptr erase_left(node_ptr some, node_ptr* root) {
node_ptr replace = max_node(some->left);
if (replace) {
if (replace == some->left) {
some->left = replace->left;
if (replace->left)
replace->left->parent = some;
}
else {
replace->parent->right = replace->left;
if (replace->left)
replace->left->parent = replace->parent;
}
if (replace->isBlack == true) {
if (replace->left && replace->left->isBlack == false)
replace->left->isBlack = true;
erase_balance(root, replace->parent);
}
}
return replace;
}
node_ptr erase_right(node_ptr some, node_ptr* root) {
node_ptr replace = min_node(some->right);
if (replace) {
if (replace == some->right) {
some->right = replace->right;
if (replace->right)
replace->right->parent = some;
}
else {
replace->parent->left = replace->right;
if (replace->right)
replace->right->parent = replace->parent;
}
if (replace->isBlack == true) {
if (replace->right && replace->right->isBlack == false)
replace->right->isBlack = true;
erase_balance(root, replace->parent);
}
}
return replace;
}
node_ptr erase_not_child(node_ptr some, node_ptr* root) {
node_ptr replace = some;
if (replace && replace->parent) {
if (replace->parent->right == replace)
replace->parent->right = 0;
else
replace->parent->left = 0;
if (replace->isBlack == true) {
if (replace->right && replace->right->isBlack == false)
replace->right->isBlack = true;
erase_balance(root, replace->parent);
}
}
return replace;
}
void erase_balance(node_ptr* root, node_ptr some) {
node_ptr right;
node_ptr left;
node_ptr brother;
while (some != *root && some->isBlack) {
brother = some->right;
if (brother) {
right = brother->right;
left = brother->left;
if ((right || left) && (!right || right->isBlack == true) && (!left || left->isBlack == true)) {
if (brother->isBlack == true) {
brother->isBlack = false;
some->isBlack = true;
rotate_left(some, root);
}
else
some = some->parent;
}
else if (right && brother->isBlack == true) {
if (left && left->isBlack == false && right->isBlack == true) {
rotate_right(brother, root);
right = brother;
brother = left;
}
if (right->isBlack == false && (!left || left->isBlack == true || !some->left))
rotate_left(some, root);
else
some = some->parent;
}
else if (brother->isBlack == true && some->isBlack == false) {
some->isBlack = true;
brother->isBlack = false;
some = some->parent;
}
else
some = some->parent;
}
else
break;
}
}
void rotate_left(node_ptr node, node_ptr* root) {
node_ptr right = node->right;
right->parent = node->parent;
if (right->parent) {
if (right->parent->right == node)
right->parent->right = right;
else
right->parent->left = right;
}
node->parent = right;
node->right = right->left;
if (node->right) {
if (node->right->parent == right)
node->right->parent = node;
else
node->right->parent = node;
}
right->left = node;
bool color = node->isBlack;
node->isBlack = right->isBlack;
right->isBlack = color;
if (!right->parent)
*root = right;
}
void rotate_right(node_ptr some, node_ptr* root) {
node_ptr left = some->left;
left->parent = some->parent;
if (left->parent) {
if (left->parent->left == some)
left->parent->left = left;
else
left->parent->right = left;
}
some->parent = left;
some->left = left->right;
if (some->left) {
if (some->left->parent == left)
some->left->parent = some;
else
some->left->parent = some;
}
left->right = some;
some->isBlack = false;
left->isBlack = true;
if (!left->parent)
*root = left;
}
};
}