/
RedBlackTree.h
490 lines (427 loc) · 12.2 KB
/
RedBlackTree.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#pragma once
#include <vector>
using std::vector;
template <class T>
class NodeT {
public:
T data;
NodeT<T>* parent;
NodeT<T>* left;
NodeT<T>* right;
bool isBlack;
NodeT(T value)
: data(value), parent(nullptr), left(nullptr), right(nullptr), isBlack(false)
{ }
};
template <class T>
class RedBlackTree {
public:
// constructors, operators and destructors
RedBlackTree(); // empty tree, root is null
RedBlackTree(const RedBlackTree &); // deep copy
RedBlackTree & operator=(const RedBlackTree &); // deep copy, overloaded assignment operator
~RedBlackTree(); // deallocate nodes
// mutators
bool insert(T); // insert data if tree does not contain parameter, no duplicates
bool remove(T); // remove parameter and return true, if parameter is not in tree, return false
// accessors
bool search(T) const; // return true if parameter is found
vector<T> search(T, T) const; // return vector containing elements in tree from a to b, ascending order
vector<T> dump() const; // return vector with all contents of tree
int size() const; // return number of nodes in tree
NodeT<T>* getRoot() const; // return pointer to tree's root
private:
NodeT<T>* root;
int currentSize;
// helpers
// copy data into tree
void deepCopy(const RedBlackTree & r)
{
if (r.root != nullptr) {
clearTree(root); // empty original tree
root = copyHelper(r.root);
} else {
root = nullptr;
}
currentSize = r.currentSize;
}
// pre order traversal and copy nodes
NodeT<T>* copyHelper(NodeT<T>* node)
{
if (node == nullptr) return nullptr;
NodeT<T>* newNode = new NodeT<T>(node->data);
newNode->left = copyHelper(node->left);
if (newNode->left != nullptr) newNode->left->parent = newNode;
newNode->right = copyHelper(node->right);
if (newNode->right != nullptr) newNode->right->parent = newNode;
return newNode;
}
// post order traversal and delete nodes
// POST: deallocate memory in tree
void clearTree(NodeT<T>* node)
{
if (node != nullptr) {
clearTree(node->left);
clearTree(node->right);
delete node;
}
}
// PARAM: value to insert in tree
// POST: return pointer to inserted node
NodeT<T>* btInsert(T value)
{
NodeT<T>* pos = root;
NodeT<T>* newNode = new NodeT<T>(value);
NodeT<T>* tempParent = root;
bool isLeft = true;
if (root == nullptr) {
root = newNode; // set root on empty tree
return root;
} else {
while (pos != nullptr) { // tree traversal
tempParent = pos;
if (value < tempParent->data) {
pos = tempParent->left;
isLeft = true;
} else {
pos = tempParent->right;
isLeft = false;
}
}
// leaf found and insertion
if (isLeft) {
tempParent->left = newNode;
newNode->parent = tempParent;
} else {
tempParent->right = newNode;
newNode->parent = tempParent;
}
return newNode;
}
}
// PARAM: node to be rotated left
void leftRotate(NodeT<T>* node)
{
NodeT<T>* temp = node->right;
node->right = temp->left;
// node's parent references temp's left child
if (temp->left != nullptr) {
temp->left->parent = node;
}
temp->parent = node->parent;
// set child of node's parent
if (node->parent == nullptr) { // node is root
root = temp;
} else if (node == node->parent->left) { // left child
node->parent->left = temp;
} else { // right child
node->parent->right = temp;
}
// node become temp's left child
temp->left = node;
node->parent = temp;
}
// symmetry of leftRotate()
// PARAM: node to be rotated right
void rightRotate(NodeT<T>* node)
{
NodeT<T>* temp = node->left;
node->left = temp->right;
if (temp->right != nullptr) {
temp->right->parent = node;
}
temp->parent = node->parent;
if (node->parent == nullptr) {
root = temp;
} else if (node == node->parent->right) {
node->parent->right = temp;
} else {
node->parent->left = temp;
}
temp->right = node;
node->parent = temp;
}
// PRE: target contains successor
// PARAM: target - node to find successor of
// sParent - parent of successor
// POST: return successor of target, set parent
NodeT<T>* getSuccessor(NodeT<T>* target, NodeT<T>* & sParent) const
{
// successor is the left-most node of right subtree
NodeT<T>* temp = target->right;
sParent = target;
while (temp->left != nullptr) {
sParent = temp;
temp = temp->left;
}
return temp;
}
// PRE: value is in red black tree
// PARAM: value - value to find in tree
// node - node to start searching on, usually root
// POST: return node containing desired value
NodeT<T>* findNode(T value, NodeT<T>* node) const
{
NodeT<T>* temp = node;
// find node based on value
while (temp != nullptr) {
if (value == temp->data) {
return temp;
} else if (value > temp->data) { // access right child
temp = temp->right;
} else { // left child
temp = temp->left;
}
}
}
// PARAM: node to fix
// POST: fixes tree to be complete, correct black height of tree
void fix(NodeT<T>* node)
{
NodeT<T>* sibling = nullptr;
while (node != root && node->isBlack) {
if (node == node->parent->left) { // node is left child
NodeT<T>* sibling = node->parent->right;
if (sibling != nullptr && !sibling->isBlack) { // set node's sibling to black
sibling->isBlack = true;
node->parent->isBlack = false;
leftRotate(node->parent);
sibling = node->parent->right;
}
// make sibling's subtree the same black height, sibling is now red
if (sibling->left->isBlack && sibling->right->isBlack) {
sibling->isBlack = false;
node = node->parent;
} else {
// make sibling's right child red
if (sibling->right->isBlack) {
sibling->left->isBlack = true;
sibling->isBlack = false;
rightRotate(sibling);
sibling = node->parent->right;
}
sibling->isBlack = node->parent->isBlack;
node->parent->isBlack = true;
sibling->right->isBlack = true;
leftRotate(node->parent);
node = root;
// fixed
}
} else { // symmetric to if, node is right child
NodeT<T>* sibling = node->parent->left;
if (sibling != nullptr && !sibling->isBlack) {
sibling->isBlack = true;
node->parent->isBlack = false;
rightRotate(node->parent);
sibling = node->parent->left;
}
if (sibling->right->isBlack && sibling->left->isBlack) {
sibling->isBlack = false;
node = node->parent;
} else {
if (sibling->left->isBlack) {
sibling->right->isBlack = true;
sibling->isBlack = false;
leftRotate(sibling);
sibling = node->parent->left;
}
sibling->isBlack = node->parent->isBlack;
node->parent->isBlack = true;
sibling->left->isBlack = true;
rightRotate(node->parent);
node = root;
}
}
}
// root is black
node->isBlack = true;
}
// PRE: start < end
// PRAM: start - begining of interval to check
// end - end of interval to check
// node - node to check
// result - vector containing correct values
// POST: add values between start and end (inclusive) to result
void searchVectorHelper(int start, int end, NodeT<T>* node, vector<T> & result) const
{
if (node != nullptr) {
searchVectorHelper(start, end, node->left, result);
if (node->data >= start && node->data <= end) {
result.push_back(node->data);
}
searchVectorHelper(start, end, node->right, result);
}
}
// PARAM: result is vector contianing data in tree
// POST: add all data into result
void dumpHelper(NodeT<T>* node, vector<T> & result) const
{
// inorder traversal
if (node != nullptr) {
dumpHelper(node->left, result);
result.push_back(node->data);
dumpHelper(node->right, result);
}
}
};
// method implementation
// default constructor
template <class T>
RedBlackTree<T>::RedBlackTree()
: root(nullptr), currentSize(0)
{ }
// copy constructor
template <class T>
RedBlackTree<T>::RedBlackTree(const RedBlackTree & r)
: root(nullptr), currentSize(0)
{
deepCopy(r);
}
template <class T>
RedBlackTree<T> & RedBlackTree<T>::operator=(const RedBlackTree & r)
{
if (this != &r) { // don't copy self
deepCopy(r);
}
return *this;
}
// destructor
template <class T>
RedBlackTree<T>::~RedBlackTree()
{
clearTree(root);
}
// PARAM: check if parameter is already in array
// return true of parameter is not in array and insert parameter
template <class T>
bool RedBlackTree<T>::insert(T value)
{
if (!search(value)) {
NodeT<T>* newNode = btInsert(value); // regular binary tree insertion
currentSize++;
// redblack balancing
while (newNode->parent != root && newNode != root && !newNode->parent->isBlack) {
if (newNode->parent == newNode->parent->parent->left) { // parent of newNode is a left child
NodeT<T>* uncle = newNode->parent->parent->right;
if (uncle != nullptr && !uncle->isBlack) { // same as parent
newNode->parent->isBlack = true;
uncle->isBlack = true;
newNode->parent->parent->isBlack = false;
newNode = newNode->parent->parent;
} else { // uncle colour is black
if (newNode == newNode->parent->right) {
newNode = newNode->parent;
leftRotate(newNode);
}
newNode->parent->isBlack = true;
newNode->parent->parent->isBlack = false;
rightRotate(newNode->parent->parent);
}
} else { // symmetric to if
NodeT<T>* uncle = newNode->parent->parent->left;
if (uncle != nullptr && !uncle->isBlack) {
newNode->parent->isBlack = true;
uncle->isBlack = true;
newNode->parent->parent->isBlack = false;
newNode = newNode->parent->parent;
} else {
if (newNode == newNode->parent->left) {
newNode = newNode->parent;
rightRotate(newNode);
}
newNode->parent->isBlack = true;
newNode->parent->parent->isBlack = false;
leftRotate(newNode->parent->parent);
}
}
}
root->isBlack = true;
return true;
}
return false;
}
// PARAM: value to remove
// POST: return true if value is removed, otherwise false
template <class T>
bool RedBlackTree<T>::remove(T value)
{
if (search(value)) {
NodeT<T>* target = findNode(value, root); // get pointer to node of interest
NodeT<T>* sParent = nullptr;
NodeT<T>* toRemove = nullptr;
if (target->left == nullptr || target->right == nullptr) { // no children
toRemove = target; // will delete toRemove pointer
} else { // 2 children
// find successor (left-most node of right subtree)
toRemove = getSuccessor(target, sParent);
}
// find if toRemove's only child is right or left
NodeT<T>* child = nullptr;
if (toRemove->left != nullptr) {
child = toRemove->left;
} else {
child = toRemove->right;
}
if (child != nullptr) child->parent = toRemove->parent; // detach child from toRemove
if (toRemove->parent == nullptr) { // child is new root
root = child;
} else {
if (toRemove == toRemove->parent->left) {
toRemove->parent->left = child;
} else {
toRemove->parent->right = child;
}
}
if (toRemove != target) { // toRemove has been moved up
target->data = toRemove->data;
}
if (toRemove->isBlack && child != nullptr)
fix(child);
delete toRemove;
currentSize--;
return true;
}
return false;
}
// PARAM: value to search for
// POST: return true if value is found
template <class T>
bool RedBlackTree<T>::search(T value) const
{
NodeT<T>* temp = root;
while (temp != nullptr) {
if (value == temp->data) {
return true; // match found
} else if (value > temp->data) { // access right child
temp = temp->right;
} else { // left child
temp = temp->left;
}
}
return false;
}
// PRE: start <= end
// PARAM: values to return inbetween two parameters (inclusive)
// POST: return vector containing values between parameters in ascending order, empty if none are found
template <class T>
vector<T> RedBlackTree<T>::search(T start, T end) const
{
vector<T> result;
searchVectorHelper(start, end, root, result);
return result;
}
// POST: return vector containing all elements of tree in ascending order
// vector is empty if tree is empty
template <class T>
vector<T> RedBlackTree<T>::dump() const
{
vector<T> result;
dumpHelper(root, result);
return result;
}
// POST: return current number of nodes
template <class T>
int RedBlackTree<T>::size() const { return currentSize; }
// POST: return root node of tree
template <class T>
NodeT<T>* RedBlackTree<T>::getRoot() const { return root; }