-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrbtree.hpp
341 lines (293 loc) · 9.54 KB
/
rbtree.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
#pragma once
#include <functional>
#include <utility>
template <class Value, class Compare = std::less<Value>>
struct RbTree {
enum RbColor {
RED,
BLACK
};
struct RbNode {
RbNode() noexcept
: left(nullptr),
right(nullptr),
parent(nullptr),
tree(nullptr),
color(RED) {}
RbNode(RbNode &&) = delete;
~RbNode() noexcept {
if (tree) {
tree->doErase(this);
}
}
friend struct RbTree;
private:
RbNode *left;
RbNode *right;
RbNode *parent;
RbTree *tree;
RbColor color;
};
private:
RbNode *root;
Compare comp;
bool compare(RbNode *left, RbNode *right) const noexcept {
return comp(static_cast<Value &>(*left), static_cast<Value &>(*right));
}
void rotateLeft(RbNode *node) noexcept {
RbNode *rightChild = node->right;
node->right = rightChild->left;
if (rightChild->left != nullptr) {
rightChild->left->parent = node;
}
rightChild->parent = node->parent;
if (node->parent == nullptr) {
root = rightChild;
} else if (node == node->parent->left) {
node->parent->left = rightChild;
} else {
node->parent->right = rightChild;
}
rightChild->left = node;
node->parent = rightChild;
}
void rotateRight(RbNode *node) noexcept {
RbNode *leftChild = node->left;
node->left = leftChild->right;
if (leftChild->right != nullptr) {
leftChild->right->parent = node;
}
leftChild->parent = node->parent;
if (node->parent == nullptr) {
root = leftChild;
} else if (node == node->parent->right) {
node->parent->right = leftChild;
} else {
node->parent->left = leftChild;
}
leftChild->right = node;
node->parent = leftChild;
}
void fixViolation(RbNode *node) noexcept {
RbNode *parent = nullptr;
RbNode *grandParent = nullptr;
while (node != root && node->color != BLACK &&
node->parent->color == RED) {
parent = node->parent;
grandParent = parent->parent;
if (parent == grandParent->left) {
RbNode *uncle = grandParent->right;
if (uncle != nullptr && uncle->color == RED) {
grandParent->color = RED;
parent->color = BLACK;
uncle->color = BLACK;
node = grandParent;
} else {
if (node == parent->right) {
rotateLeft(parent);
node = parent;
parent = node->parent;
}
rotateRight(grandParent);
std::swap(parent->color, grandParent->color);
node = parent;
}
} else {
RbNode *uncle = grandParent->left;
if (uncle != nullptr && uncle->color == RED) {
grandParent->color = RED;
parent->color = BLACK;
uncle->color = BLACK;
node = grandParent;
} else {
if (node == parent->left) {
rotateRight(parent);
node = parent;
parent = node->parent;
}
rotateLeft(grandParent);
std::swap(parent->color, grandParent->color);
node = parent;
}
}
}
root->color = BLACK;
}
void doInsert(RbNode *node) noexcept {
node->left = nullptr;
node->right = nullptr;
node->tree = this;
node->color = RED;
RbNode *parent = nullptr;
RbNode *current = root;
while (current != nullptr) {
parent = current;
if (compare(node, current)) {
current = current->left;
} else {
current = current->right;
}
}
node->parent = parent;
if (parent == nullptr) {
root = node;
} else if (compare(node, parent)) {
parent->left = node;
} else {
parent->right = node;
}
fixViolation(node);
}
/* template <class Key> */
/* RbNode* doFind(Key &&key) const { */
/* RbNode* current = root; */
/* // Find the node to delete */
/* while (current != nullptr) { */
/* if (key_compare(key, current)) { */
/* current = current->left; */
/* } else { */
/* current = current->right; */
/* } */
/* } */
/* return nullptr; */
/* } */
/* void doErase(RbNode* node) noexcept { */
/* RbNode* parent = nullptr; */
/* */
/* // Case 1: Node to delete has no children */
/* if (node->left == nullptr && node->right == nullptr) { */
/* if (node == root) { */
/* root = nullptr; */
/* } else { */
/* if (node == parent->left) { */
/* parent->left = nullptr; */
/* } else { */
/* parent->right = nullptr; */
/* } */
/* } */
/* delete node; */
/* } */
/* // Case 2: Node to delete has one child */
/* else if (node->left == nullptr || node->right == nullptr) { */
/* RbNode* child = (node->left != nullptr) ? node->left : node->right; */
/* */
/* if (node == root) { */
/* root = child; */
/* } else { */
/* if (node == parent->left) { */
/* parent->left = child; */
/* } else { */
/* parent->right = child; */
/* } */
/* } */
/* child->parent = parent; */
/* delete node; */
/* } */
/* // Case 3: Node to delete has two children */
/* else { */
/* RbNode* successor = node->right; */
/* while (successor->left != nullptr) { */
/* successor = successor->left; */
/* } */
/* doErase(successor); */
/* } */
/* } */
void doErase(RbNode *current) noexcept {
current->tree = nullptr;
RbNode *node = nullptr;
RbNode *child = nullptr;
RbColor color = RED;
if (current->left != nullptr && current->right != nullptr) {
RbNode *replace = current;
replace = replace->right;
while (replace->left != nullptr) {
replace = replace->left;
}
if (current != replace->parent) {
current->parent->left = replace->right;
replace->right = current->right;
current->right->parent = replace;
} else {
replace->parent = current;
}
if (current == root) {
root = replace;
} else if (current->parent->left == current) {
current->parent->left = replace;
} else {
current->parent->right = replace;
}
replace->left = current->left;
current->left->parent = replace;
node = replace;
color = node->color;
child = node->right;
} else {
node = current;
color = node->color;
child = (node->left != nullptr) ? node->left : node->right;
}
if (child != nullptr) {
child->parent = node->parent;
}
if (node == root) {
root = child;
} else if (node->parent->left == node) {
node->parent->left = child;
} else {
node->parent->right = child;
}
if (color == BLACK && root) {
fixViolation(child ? child : node->parent);
}
}
RbNode *getFront() const noexcept {
RbNode *current = root;
while (current->left != nullptr) {
current = current->left;
}
return current;
}
RbNode *getBack() const noexcept {
RbNode *current = root;
while (current->right != nullptr) {
current = current->right;
}
return current;
}
template <class Visitor>
void doTraversalInorder(RbNode *node, Visitor &&visitor) {
if (node == nullptr) {
return;
}
traversalInorder(node->left, visitor);
visitor(node);
doTraversalInorder(node->right, visitor);
}
public:
RbTree() noexcept : root(nullptr) {}
explicit RbTree(Compare comp) noexcept(noexcept(Compare(comp)))
: root(nullptr),
comp(comp) {}
RbTree(RbTree &&) = delete;
~RbTree() noexcept {}
void insert(Value &value) noexcept {
doInsert(&static_cast<RbNode &>(value));
}
void erase(Value &value) noexcept {
doErase(&static_cast<RbNode &>(value));
}
bool empty() const noexcept {
return root == nullptr;
}
Value &front() const noexcept {
return static_cast<Value &>(*getFront());
}
Value &back() const noexcept {
return static_cast<Value &>(*getBack());
}
template <class Visitor>
void traversalInorder(Visitor &&visitor) {
doTraversalInorder(root, std::forward<Visitor>(visitor));
}
};