-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBTree.cpp
341 lines (323 loc) · 11.3 KB
/
RBTree.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
// CSCI2720
// Project 2
// Author: Justin Rector
// RBTree.cpp
// This class implements a red black tree with all apropriate methods
#include "RBNode.h"
#include "RBTree.h"
#include <string>
#include <queue>
using namespace std;
//An empty constructor. Creates tNIL and assigns the root to it for now.
RBTree::RBTree() {
RBNode * newTNIL = new RBNode(NULL, nullptr);
newTNIL->setColor(false);
tNIL = newTNIL;
root = tNIL;
}
//Creates a tree based on a RBNode. This is intended as making a subtree of an already established RBTree.
//tNIL is passed so the address doesn't get reassigned.
RBTree::RBTree(RBNode * newRoot, RBNode * newTNIL) {
root = newRoot;
tNIL = newTNIL;
}
//Returns the in order successor of a node
RBNode * RBTree::getSuccessor(RBNode * currentNode) {
currentNode = currentNode->getRightChild();
while (currentNode->getLeftChild() != tNIL) {
currentNode = currentNode->getLeftChild();
}
return currentNode;
}
//Inserts a RBNode into a RBTree
void RBTree::RBinsert(RBTree * tree, int zKey) {
RBNode * yPtr = tNIL;
RBNode * xPtr = root;
//traverse to find proper spot
while (xPtr != tNIL) {
yPtr = xPtr;
if (zKey < xPtr->getKey())
xPtr = xPtr->getLeftChild();
else
xPtr = xPtr->getRightChild();
}
//make new node and set parent
RBNode * zPtr = new RBNode(zKey, yPtr);
//special case, empty tree
if (yPtr == tNIL)
root = zPtr;
else if (zKey < yPtr->getKey())
yPtr->setLeftChild(zPtr);
else
yPtr->setRightChild(zPtr);
zPtr->setLeftChild(tNIL);
zPtr->setRightChild(tNIL);
zPtr->setColor(true);
RBinsertFixup(zPtr);
}
//Deletes a RBNode from a RBTree
bool RBTree::RBdelete(RBTree * tree, int zKey) {
RBNode * zPtr = root;
RBNode * xPtr;
bool result = true;
//traverse to match node with key
while (zPtr->getKey() != zKey) {
if (zKey < zPtr->getKey())
zPtr = zPtr->getLeftChild();
else
zPtr = zPtr->getRightChild();
if (zPtr == tNIL) {
result = false;
break;
}
}
RBNode * yPtr = zPtr;
bool yOrigColor = yPtr->getColor();
if (zPtr->getLeftChild() == tNIL) {
xPtr = zPtr->getRightChild();
RBtransplant(zPtr, zPtr->getRightChild());
}
else if (zPtr->getRightChild() == tNIL) {
xPtr = zPtr->getLeftChild();
RBtransplant(zPtr, zPtr->getLeftChild());
}
else {
yPtr = getSuccessor(zPtr);
yOrigColor = yPtr->getColor();
xPtr = yPtr->getRightChild();
if (yPtr->getParent() == zPtr)
xPtr->setParent(yPtr);
else {
RBtransplant(yPtr, yPtr->getRightChild());
yPtr->setRightChild(zPtr->getRightChild());
yPtr->getRightChild()->setParent(yPtr);
}
RBtransplant(zPtr, yPtr);
yPtr->setLeftChild(zPtr->getLeftChild());
yPtr->getLeftChild()->setParent(yPtr);
yPtr->setColor(zPtr->getColor());
}
if (yOrigColor == false)
RBdeleteFixup(xPtr);
return result;
}
//Prints the nodes of a RBTree through an inorder traversal
void RBTree::RBinorder(RBTree * tree) {
if (root != tNIL) {
if (root->getLeftChild() != tNIL) {
RBTree * newTree = new RBTree(root->getLeftChild(), tNIL);
newTree->RBinorder(newTree);
}
cout << root->getKey() << "(";
if (root->getColor() == true)
cout << "R";
else
cout << "B";
cout << ")" << endl;
if (root->getRightChild() != tNIL) {
RBTree * newTree = new RBTree(root->getRightChild(), tNIL);
newTree->RBinorder(newTree);
}
}
}
//Prints the nodes of a RBTree through a level order traversal
void RBTree::RBlevelorder(RBTree * tree) {
queue<RBNode *> nodeQueue;
RBNode * currentNode = root;
nodeQueue.push(currentNode);
while (currentNode != tNIL) {
if (currentNode->getLeftChild() != tNIL) {
RBNode * tempNode = currentNode->getLeftChild();
nodeQueue.push(tempNode);
}
if (currentNode->getRightChild() != tNIL) {
RBNode * tempNode = currentNode->getRightChild();
nodeQueue.push(tempNode);
}
cout << currentNode->getKey() << "(";
if (currentNode->getColor() == true)
cout << "R";
else
cout << "B";
cout << ")" << endl;
nodeQueue.pop();
if (nodeQueue.empty() != true)
currentNode = nodeQueue.front();
else
currentNode = tNIL;
}
}
//Rotates a subtree to the left. Node passed is the root of the subtree
void RBTree::leftRotate(RBNode * xPtr) {
RBNode * yPtr = xPtr->getRightChild();
//Make x's right subtree y's left subtree
xPtr->setRightChild(yPtr->getLeftChild());
//fix parent of changed subtree if it exists
if (yPtr->getLeftChild() != tNIL)
yPtr->getLeftChild()->setParent(xPtr);
//update parent of Y
yPtr->setParent(xPtr->getParent());
//special case of parent being tNIL
if (xPtr->getParent() == tNIL)
root = yPtr;
//update for if X was left child
else if (xPtr == xPtr->getParent()->getLeftChild())
xPtr->getParent()->setLeftChild(yPtr);
//update for if X was right child
else
xPtr->getParent()->setRightChild(yPtr);
yPtr->setLeftChild(xPtr);
xPtr->setParent(yPtr);
}
//Rotates a subtree to the right. Node passed is the root of the subtree
void RBTree::rightRotate(RBNode * yPtr) {
RBNode * xPtr = yPtr->getLeftChild();
//Make y's left subtree x's right subtree
yPtr->setLeftChild(xPtr->getRightChild());
//fix parent of changed subtree if it exists
if (xPtr->getRightChild() != tNIL)
xPtr->getRightChild()->setParent(yPtr);
//update parent of X
xPtr->setParent(yPtr->getParent());
//special case of parent being tNIL
if (yPtr->getParent() == tNIL)
root = xPtr;
//update for if Y was right child
else if (yPtr == yPtr->getParent()->getRightChild())
yPtr->getParent()->setRightChild(xPtr);
//update for if Y was left child
else
yPtr->getParent()->setLeftChild(xPtr);
xPtr->setRightChild(yPtr);
yPtr->setParent(xPtr);
}
//Fixes a RBTree after an insert has been performed. This is necessary when there are two RB nodes adjacent to each other
void RBTree::RBinsertFixup(RBNode * zPtr) {
while (zPtr->getParent()->getColor() == true) {
if (zPtr->getParent() == zPtr->getParent()->getParent()->getLeftChild()) {
RBNode * yPtr = zPtr->getParent()->getParent()->getRightChild();
//Case 1
if (yPtr->getColor() == true) {
zPtr->getParent()->setColor(false);
yPtr->setColor(false);
zPtr->getParent()->getParent()->setColor(true);
zPtr = zPtr->getParent()->getParent();
}
else {
//Case 2
if (zPtr == zPtr->getParent()->getRightChild()) {
zPtr = zPtr->getParent();
leftRotate(zPtr);
}
//Case 3
zPtr->getParent()->setColor(false);
zPtr->getParent()->getParent()->setColor(true);
rightRotate(zPtr->getParent()->getParent());
}
}
else {
RBNode * yPtr = zPtr->getParent()->getParent()->getLeftChild();
//Case 1
if (yPtr->getColor() == true) {
zPtr->getParent()->setColor(false);
yPtr->setColor(false);
zPtr->getParent()->getParent()->setColor(true);
zPtr = zPtr->getParent()->getParent();
}
else {
//Case 2
if (zPtr == zPtr->getParent()->getLeftChild()) {
zPtr = zPtr->getParent();
rightRotate(zPtr);
}
//Case 3
zPtr->getParent()->setColor(false);
zPtr->getParent()->getParent()->setColor(true);
leftRotate(zPtr->getParent()->getParent());
}
}
}
root->setColor(false);
}
//Transplants a leaf to a position in the RBTree.
void RBTree::RBtransplant(RBNode * uPtr, RBNode * vPtr) {
//special case of parent being tNIL
if (uPtr->getParent() == tNIL)
root = vPtr;
//u is a left child
else if (uPtr == uPtr->getParent()->getLeftChild())
uPtr->getParent()->setLeftChild(vPtr);
//u is a right child
else {
uPtr->getParent()->setRightChild(vPtr);
}
//fixing parent
vPtr->setParent(uPtr->getParent());
}
//Fixes up a RBTree after a delete has been performed. This accounts for any "doubly black" or "red-black" nodes in the three
void RBTree::RBdeleteFixup(RBNode * xPtr) {
while ((xPtr != root)&&(xPtr->getColor() == false)) {
if (xPtr == xPtr->getParent()->getLeftChild()) {
//set sibling
RBNode * wPtr = xPtr->getParent()->getRightChild();
//Case 1
if (wPtr->getColor() == true) {
wPtr->setColor(false);
xPtr->getParent()->setColor(true);
leftRotate(xPtr->getParent());
wPtr = xPtr->getParent()->getRightChild();
}
//Case 2
if ((wPtr->getLeftChild()->getColor() == false)&&(wPtr->getRightChild()->getColor() == false)) {
wPtr->setColor(true);
xPtr = xPtr->getParent();
}
else {
//Case 3
if (wPtr->getRightChild()->getColor() == false) {
wPtr->getLeftChild()->setColor(false);
wPtr->setColor(true);
rightRotate(wPtr);
wPtr = xPtr->getParent()->getRightChild();
}
//Case 4
wPtr->setColor(xPtr->getParent()->getColor());
xPtr->getParent()->setColor(false);
wPtr->getRightChild()->setColor(false);
leftRotate(xPtr->getParent());
xPtr = root;
}
}
else {
//set sibling
RBNode * wPtr = xPtr->getParent()->getLeftChild();
//Case 1
if (wPtr->getColor() == true) {
wPtr->setColor(false);
xPtr->getParent()->setColor(true);
rightRotate(xPtr->getParent());
wPtr = xPtr->getParent()->getLeftChild();
}
//Case 2
if ((wPtr->getRightChild()->getColor() == false)&&(wPtr->getLeftChild()->getColor() == false)) {
wPtr->setColor(true);
xPtr = xPtr->getParent();
}
else {
//Case 3
if (wPtr->getLeftChild()->getColor() == false) {
wPtr->getRightChild()->setColor(false);
wPtr->setColor(true);
leftRotate(wPtr);
wPtr = xPtr->getParent()->getLeftChild();
}
//Case 4
wPtr->setColor(xPtr->getParent()->getColor());
xPtr->getParent()->setColor(false);
wPtr->getLeftChild()->setColor(false);
rightRotate(xPtr->getParent());
xPtr = root;
}
}
}
}