-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRBTree.cpp
More file actions
369 lines (351 loc) · 9.88 KB
/
Copy pathRBTree.cpp
File metadata and controls
369 lines (351 loc) · 9.88 KB
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
//
// Red Black Tree Implementation
//
//#include <bits/stdc++.h>
#include <string>
#include "RBTree.h"
#include <iostream>
using namespace std;
RBTree::RBTree() {
root = nullptr;
}
int RBTree::getColor(Node *&node) {
if (node == nullptr)
return BLACK;
return node->color;
}
void RBTree::setColor(Node *&node, int color) {
if (node == nullptr)
return;
node->color = color;
}
Node* RBTree::insertBST(Node *&root, Node *&ptr) {
if (root == nullptr)
return ptr;
if (ptr->name < root->name) {
root->left = insertBST(root->left, ptr);
root->left->parent = root;
}
else if (ptr->name > root->name) {
root->right = insertBST(root->right, ptr);
root->right->parent = root;
}
return root;
}
void RBTree::insertValue(string name, int size) {
Node *node = new Node(name, size);
root = insertBST(root, node);
fixInsertRBTree(node);
}
void RBTree::rotateLeft(Node *&ptr) {
Node *right_child = ptr->right;
ptr->right = right_child->left;
if (ptr->right != nullptr)
ptr->right->parent = ptr;
right_child->parent = ptr->parent;
if (ptr->parent == nullptr)
root = right_child;
else if (ptr == ptr->parent->left)
ptr->parent->left = right_child;
else
ptr->parent->right = right_child;
right_child->left = ptr;
ptr->parent = right_child;
}
void RBTree::rotateRight(Node *&ptr) {
Node *left_child = ptr->left;
ptr->left = left_child->right;
if (ptr->left != nullptr)
ptr->left->parent = ptr;
left_child->parent = ptr->parent;
if (ptr->parent == nullptr)
root = left_child;
else if (ptr == ptr->parent->left)
ptr->parent->left = left_child;
else
ptr->parent->right = left_child;
left_child->right = ptr;
ptr->parent = left_child;
}
void RBTree::fixInsertRBTree(Node *&ptr) {
Node *parent = nullptr;
Node *grandparent = nullptr;
while (ptr != root && getColor(ptr) == RED && getColor(ptr->parent) == RED) {
parent = ptr->parent;
grandparent = parent->parent;
if (parent == grandparent->left) {
Node *uncle = grandparent->right;
if (getColor(uncle) == RED) {
setColor(uncle, BLACK);
setColor(parent, BLACK);
setColor(grandparent, RED);
ptr = grandparent;
}
else {
if (ptr == parent->right) {
rotateLeft(parent);
ptr = parent;
parent = ptr->parent;
}
rotateRight(grandparent);
swap(parent->color, grandparent->color);
ptr = parent;
}
}
else {
Node *uncle = grandparent->left;
if (getColor(uncle) == RED) {
setColor(uncle, BLACK);
setColor(parent, BLACK);
setColor(grandparent, RED);
ptr = grandparent;
}
else {
if (ptr == parent->left) {
rotateRight(parent);
ptr = parent;
parent = ptr->parent;
}
rotateLeft(grandparent);
swap(parent->color, grandparent->color);
ptr = parent;
}
}
}
setColor(root, BLACK);
}
// deletes the given node, based off of code from https://gist.github.com/nandor/9249431
void RBTree::deleteBST(string name) {
Node *node = root;
//traverses down the tree
while (node)
{
if (node->name > name)
{
node = node->left;
}
else if (node->name< name)
{
node = node->right;
}
else
{
break;
}
}
//checks if node is NULL or has the wrong name
if (!node || node->name != name)
{
return;
}
Node *sub, *old;
//Had to static cast since old->color was an int
Color original = static_cast<Color>(old->color);
//switches the nodes to prepare for deletion
if (!node->left)
{
Transplant(node, sub = node->right);
}
else if (!node->right)
{
Transplant(node, sub = node->left);
}
else
{
old = minValueNode(node->right);
;
sub = old->right;
if (old->parent == node && sub != NULL)
{
sub->parent = node;
}
else
{
Transplant(old, old->right);
old->right = node->right;
old->right->parent = old;
}
Transplant(node, old);
old->left = node->left;
old->left->parent = old;
old->color = node->color;
}
delete node;
//the part that fixes the tree after deleting the node
if (original == BLACK)
{
bool side;
Node *sibling;
while (old != root && old->color == BLACK)
{
if ((side = (old == old->parent->left)))
{
sibling = old->parent->right;
}
else
{
sibling = old->parent->left;
}
if (sibling->color == RED)
{
sibling->color = BLACK;
old->parent->color = RED;
side ? rotateLeft(old->parent) : rotateRight(old->parent);
sibling = side ? old->parent->right : old->parent->left;
}
if (sibling->left->color == BLACK && sibling->right->color == BLACK)
{
sibling->color = RED;
old = old->parent;
}
else
{
if (BLACK == side ? sibling->right->color : sibling->left->color)
{
sibling->color = RED;
if (side)
{
sibling->left->color = BLACK;
rotateRight(sibling);
sibling = old->parent->right;
}
else
{
sibling->right->color = BLACK;
rotateLeft(sibling);
sibling = old->parent->left;
}
}
sibling->color = old->parent->color;
old->parent->color = BLACK;
if (side)
{
sibling->left->color = BLACK;
rotateLeft(old->parent);
}
else
{
sibling->right->color = BLACK;
rotateRight(old->parent);
}
old = root;
}
}
}
}
//public function that calls the protected delete function
void RBTree::deleteValue(string name) {
deleteBST(name);
}
//in order traversal
void RBTree::inorderBST(Node *&ptr) {
if (ptr == nullptr)
return;
inorderBST(ptr->left);
cout << ptr->name << " " << ptr->color << endl;
inorderBST(ptr->right);
}
void RBTree::inorder() {
inorderBST(root);
}
// pre order traversal
void RBTree::preorderBST(Node *&ptr) {
if (ptr == nullptr)
return;
cout << ptr->name << " " << ptr->color << endl;
preorderBST(ptr->left);
preorderBST(ptr->right);
}
void RBTree::preorder() {
preorderBST(root);
cout << "-------" << endl;
}
//finds the bottom left/minimum node
Node *RBTree::minValueNode(Node *&node) {
Node *ptr = node;
while (ptr->left != nullptr)
ptr = ptr->left;
return ptr;
}
//finds the bottom right/maximum node
Node* RBTree::maxValueNode(Node *&node) {
Node *ptr = node;
while (ptr->right != nullptr)
ptr = ptr->right;
return ptr;
}
//finds the height of the black nodes
int RBTree::getBlackHeight(Node *node) {
int blackheight = 0;
while (node != nullptr) {
if (getColor(node) == BLACK)
blackheight++;
node = node->left;
}
return blackheight;
}
//functions to get the files
Node* file;
Node* getFile_helper(Node* top, string name) {
if (top == nullptr)
throw ("Empty Tree");
if (name < top->name) {
getFile_helper(top->left, name);
//top->left->parent = top;
}
else if (name > top->name) {
getFile_helper(top->right, name);
//top->right->parent = top;
} else if (name == top->name) {
file = top;
}
return file;
}
Node RBTree::getFile(string name) {
return *getFile_helper(root, name);
}
void getFiles_helper(Node* top, std::list<Node>& files) {
files.push_front(*top);
if (top->left != nullptr) {
getFiles_helper(top->left, files);
}
if (top->right != nullptr) {
getFiles_helper(top->right, files);
}
}
std::list<Node> RBTree::getFiles() {
std::list<Node> files;
if (root == nullptr)
throw ("Empty Tree");
getFiles_helper(root, files);
return files;
}
//deletes the tree
void clearTree_helper(Node* top) {
if (top == nullptr) return;
clearTree_helper(top->left);
clearTree_helper(top->right);
delete top;
}
void RBTree::clearTree() {
clearTree_helper(root);
}
//switches the nodes around for deletion
void RBTree::Transplant(Node *dest, Node *src) {
if (dest->parent == NULL)
{
root = src;
}
else if (dest == dest->parent->left)
{
dest->parent->left = src;
}
else
{
dest->parent->right = src;
}
if (src)
{
src->parent = dest->parent;
}
}