-
Notifications
You must be signed in to change notification settings - Fork 2
/
RedBlackTree.cpp
439 lines (393 loc) · 10.1 KB
/
RedBlackTree.cpp
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
#include "RedBlackTree.h"
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::string;
// public
RedBlackTree::RedBlackTree() {
nil = new rbNode("NIL", "NIL", "NIL", 0, 0, "NIL", 0);
nil->isRed = false;
nil->left = nil;
nil->right = nil;
root = nil;
}
RedBlackTree::~RedBlackTree() {
deleteAll(root);
delete nil;
}
void RedBlackTree::printTree() {
printTree(root);
return;
}
void RedBlackTree::addDataNode(string in_commonName, string in_sciName, string in_phenophase, int in_elevation, int in_siteID, string in_date, int count) {
rbNode *newNode = new rbNode (in_commonName, in_sciName, in_phenophase, in_elevation, in_siteID, in_date, count);
newNode->left = nil;
newNode->right = nil;
rbNode *x = root;
rbNode *y = NULL;
if (root == nil) {
root = newNode;
newNode->parent = NULL;
newNode->count++;
} else {
while (x != nil) {
y = x;
if (newNode->commonName.compare(x->commonName) < 0) {
x = x->left;
} else {
x = x->right;
}
}
if (y != nil && y != NULL) {
if (newNode->commonName != y->commonName) {
newNode->parent = y;
newNode->count = 1;
if (newNode->commonName.compare(y->commonName) < 0) {
y->left = newNode;
} else {
y->right = newNode;
}
rbAddFixup(newNode); // tree is already balanced if animal/plant has been added before
} else { // sighting of same animal/plant
y->count++;
y->phenophase.push_back(in_phenophase);
y->elevation.push_back(in_elevation);
y->siteID.push_back(in_siteID);
y->date.push_back(in_date);
}
}
}
return;
}
void RedBlackTree::findDataNode(string commonName) {
rbNode* x = searchRBTree(root, commonName);
if (x == nil) {
cout << "data not found" << endl;
} else {
cout << "data information: " << endl;
printNode(x, true);
}
return;
}
void RedBlackTree::deleteDataNode(string commonName) { // fix seg faults here
rbNode *z = searchRBTree(root, commonName);
if (z != nil) {
rbNode *y = z;
rbNode *x = nil;
bool yOrigColor = y->isRed;
if (z->left == nil) {
x = z->right;
rbTransplant(z, z->right);
} else if (z->right == nil) {
x = z->left;
rbTransplant(z, z->right);
} else {
y = z->right;
while (y->left != nil) {
y = y->left;
}
yOrigColor = y->isRed;
x = y->right;
if (y->parent == z) {
x->parent = y;
} else {
rbTransplant(y, y->right);
y->right = z->right;
y->right->parent = y;
}
rbTransplant(z,y);
y->left = z->left;
y->left->parent = y;
y->isRed = z->isRed;
}
delete z;
if (!yOrigColor) {
rbDeleteFixup(x);
}
}
}
bool RedBlackTree::isValid() {
if (rbValid(root) == 0) {
return false;
} else {
cout << "Black height: " << rbValid(root) << endl;
return true;
}
}
// private
void RedBlackTree::deleteAll(rbNode *node) {
if (node == nil) {
return;
} else {
if (node->left != nil) {
deleteAll(node->left);
}
if (node->right != nil) {
deleteAll(node->right);
}
delete node;
}
return;
}
void RedBlackTree::printTree(rbNode *node) {
cout << "print" << endl;
if (node == nil) {
cout << "no nodes in tree" << endl;
return;
} else {
if (node->left != nil) {
printTree(node->left);
}
printNode(node, false);
if (node->right != nil) {
printTree(node->right);
}
return;
}
}
void RedBlackTree::printNode(rbNode *node, bool lookUp) {
cout << "Common name: " << node->commonName << endl;
cout << "Scientific name: " << node->sciName << endl;
cout << "Number of records: " << node->count << endl;
cout << "Site ID(s): ";
for (unsigned int i = 0; i < (node->siteID).size()-1; i++) {
cout << node->siteID[i]<<",";
} cout << node->siteID[node->siteID.size()-1] << endl;
if(lookUp){//only an option when the user has searched for a specimen by common name (not when printing entire tree)
std::string input = "blah";
while(input != "y" && input != "n"){//loop until user provides valid input
cout<<"Would you like more information on a site ID? (y)es or (n)o" <<endl;
std::getline(std::cin, input);
}
if(input =="y"){
std::string ID = " ";
while(ID.compare(" ") == 0){
cout<<"What is the site ID?"<<endl;
getline(std::cin, ID);
}
bool found = false;//indicates whether at least one with the same index has been found
for(unsigned int i=0; i < (node->siteID).size(); i++){
if(std::to_string(node->siteID[i]).compare(ID)== 0){//change to string to more easily handle invalid user input(e.g. string instead of int)
if(!found){//first instance in vector
cout<<"Site ID:" << node->siteID[i]<<endl;
cout<<"Elevation:" <<node->elevation[i]<<endl;
cout<<"----------"<<endl;
found = true;
}
if(node->date[i].length() ==8){
cout<<node->date[i].substr(0,2)<<"/"<<node->date[i].substr(2,2)<<"/"<<node->date[i].substr(4,4)<< ": " <<node->phenophase[i]<<endl;
}else{
cout<<node->date[i].substr(0,2)<<"/"<<node->date[i].substr(2,1)<<"/"<<node->date[i].substr(3,4)<<": " <<node->phenophase[i]<<endl;
}
}
}
if(!found){
cout<<"Sorry, that ID could not be found" <<endl;
}
}
}
}
rbNode* RedBlackTree::searchRBTree(rbNode *node, std::string in_commonName) {
if (node == nil) {
return nil;
} else if (node->commonName == in_commonName) {
return node;
} else {
if (in_commonName.compare(node->commonName) < 0) {
return searchRBTree(node->left, in_commonName);
} else {
return searchRBTree(node->right, in_commonName);
}
}
}
void RedBlackTree::rbAddFixup(rbNode *node) {
rbNode *uncle = NULL;
node->left = nil;
node->right = nil;
while (node != root && node->parent->isRed) {
cout << "check" << endl;
if (node->parent->parent->left == node->parent) {
uncle = node->parent->parent->right;
if (uncle->isRed) {
node->parent->isRed = false;
uncle->isRed = false;
node->parent->parent->isRed = true;
node = node->parent->parent;
} else {
if (node->parent->right == node) {
node = node->parent;
cout << "working before rotate left" << endl;
leftRotate(node);
}
node->parent->isRed = false;
node->parent->parent->isRed = true;
cout << "working before rotate right" << endl;
rightRotate(node->parent->parent);
}
} else {
uncle = node->parent->parent->left;
if (uncle->isRed) {
node->parent->isRed = false;
uncle->isRed = false;
node->parent->parent->isRed = true;
node = node->parent->parent;
} else {
if (node->parent->left == node) {
node = node->parent;
cout << "working before rotate right 2" << endl;
rightRotate(node);
}
node->parent->isRed = false;
node->parent->parent->isRed = true;
cout << "working before left rotate 2" << endl;
leftRotate(node->parent->parent);
}
}
}
root->isRed = false;
}
void RedBlackTree::rbDeleteFixup(rbNode *node) {
rbNode *w = nil;
while (node != root && node->isRed == false) {
if (node->parent->left == node) {
w = node->parent->right;
if (w->isRed) {
w->isRed = false;
node->parent->isRed = true;
leftRotate(node->parent);
w = node->parent->right;
}
if (!w->left->isRed && !w->right->isRed) {
w->isRed = true;
node = node->parent;
} else {
if (!w->right->isRed) {
w->left->isRed = false;
w->isRed = true;
rightRotate(w);
w = node->parent->right;
}
w->isRed = node->parent->isRed;
node->parent->isRed = false;
w->right->isRed = false;
leftRotate(node->parent);
node = root;
}
}
else {
w = node->parent->right;
if (w->isRed) {
w->isRed = false;
node->parent->isRed = true;
rightRotate(node->parent);
w = node->parent->left;
}
if (!w->left->isRed && !w->right->isRed) {
w->isRed = true;
node = node->parent;
} else {
if (!w->left->isRed) {
w->right->isRed = false;
w->isRed = true;
leftRotate(w);
w = node->parent->left;
}
w->isRed = node->parent->isRed;
node->parent->isRed = false;
w->left->isRed = false;
rightRotate(node->parent);
node = root;
}
}
}
node->isRed = false;
return;
}
void RedBlackTree::rbTransplant(rbNode *u, rbNode *v) {
if (u->parent == nil) {
root = v;
} else if (u->parent->left == u) {
u->parent->left = v;
} else {
u->parent->right = v;
}
v->parent = u->parent;
return;
}
void RedBlackTree::leftRotate(rbNode *x) {
rbNode *y = x->right;
x->right = y->left;
if (y->left != nil) {
y->left->parent = x;
}
y->parent = x->parent;
if (x->parent == nil) {
root = y;
} else {
if (x->parent->left == x) {
x->parent->left = y;
} else {
x->parent->right = y;
}
}
y->left = x;
x->parent = y;
return;
}
void RedBlackTree::rightRotate(rbNode *x) {
rbNode *y = x->left;
x->left = y->right;
if (y->right != nil) {
y->right->parent = x;
}
y->parent = x->parent;
if (x->parent == nil) {
root = y;
} else {
if (x->parent->left == x) {
x->parent->left = y;
} else {
x->parent->right = y;
}
}
y->right = x;
x->parent = y;
}
int RedBlackTree::rbValid(rbNode *node) {
int lh = 0;
int rh = 0;
// If we are at a nil node just return 1
if (node == nil)
return 1;
else {
rbNode * ln = node->left;
rbNode * rn = node->right;
// consecutive red links
if (node->isRed)
{
if(ln->isRed || rn->isRed)
{
cout << "red violation\n";
return 0;
}
}
lh = rbValid(ln);
rh = rbValid(rn);
// black height mismatch
if (lh != 0 && rh != 0 && lh != rh)
{
cout << "Black height violation\n";
return 0;
}
if (lh != 0 && rh != 0)
{
return node->isRed ? lh : lh + 1;
}
else
{
return 0;
}
}
}