-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbTree.cpp
352 lines (303 loc) · 7.8 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
342
343
344
345
346
347
348
349
350
351
352
#include <iostream>
#include <queue>
#include <ctime>
using namespace std;
enum color_t { BLACK, RED };
template<class T>
class RBTree;
template <class T>
T& max(T& left, T& right) {
if (left > right)
return left;
else
return right;
}
template <class T>
T max(const T& left, const T& right) {
if (left > right)
return left;
else
return right;
}
template<class T>
class RBNode {
RBNode<T>* parent, * left, * right;
T data;
color_t color;
public:
friend class RBTree<T>;
RBNode(const T& newdata = T(), RBNode<T>* newParent = nullptr, RBNode<T>* newleft = nullptr,
RBNode<T>* newright = nullptr) : data(newdata), parent(newParent), left(newleft), right(newright),
color(RED) {}
void printInOrder() const {
if (left != nullptr)
left->printInOrder();
cout << data << "\t" << color << endl;
if (right != nullptr)
right->printInOrder();
}
void printPostOrder() const {
if (left != nullptr)
left->printPostOrder();
if (right != nullptr)
right->printPostOrder();
cout << data << "\t" << color << endl;
}
int size()const {
int leftSize = 0;
int rightSize = 0;
if (left != nullptr)
leftSize = left->size();
if (right != nullptr)
rightSize = right->size();
return 1 + leftSize + rightSize;
}
int depth() const {
int parentDepth = -1;
if (parent != nullptr)
parentDepth = parent->depth();
return 1 + parentDepth;
}
RBNode<T>* getSibling() {
if (parent == nullptr)
return nullptr;
if (this == parent->left)
return parent->right;
else
return parent->left;
}
RBNode<T>* getUncle() {
if (parent == nullptr)
return nullptr;
return parent->getSibling();
}
};
template<class T>
class RBTree {
RBNode<T>* root;
int size;
RBNode<T>* recursiveCopy(RBNode<T>* toCopy);
void singleRightRotate(RBNode<T>*& point);
void doubleRightRotate(RBNode<T>*& point);
void singleLeftRotate(RBNode<T>*& point);
void doubleLeftRotate(RBNode<T>*& point);
void removeTree(RBNode<T>* point);
void insertHelper(const T& toInsert, RBNode<T>*& point);
void balanceTree(RBNode<T>* point);
void insertCase1(RBNode<T>* point);
void insertCase2(RBNode<T>* point);
void insertCase3(RBNode<T>* point);
void insertCase4(RBNode<T>* point);
public:
RBTree() : size(0), root(nullptr) {}
RBTree(const RBTree<T>& rhs) : root(nullptr) {
clear();
*this = rhs;
}
~RBTree() { clear(); }
RBTree& operator =(const RBTree<T>& rhs);
bool isInTree(const T& toFind) { return find(toFind) != nullptr; }
bool isEmpty() const { return root == nullptr; }
int getSize() const { return size; }
void insert(const T&);
void insert(const T&, RBNode<T>*& point);
RBNode<T>* find(const T& toFind) const;
void clear();
void printInOrder()const { root->printInOrder(); }
void printPostOrder()const { root->printPostOrder(); }
void printLevelOrder()const;
};
template <class T>
void RBTree<T>::doubleLeftRotate(RBNode<T>*& point) {
singleRightRotate(point->left);
singleLeftRotate(point);
}
template <class T>
void RBTree<T>::doubleRightRotate(RBNode<T>*& point) {
singleLeftRotate(point->left);
singleRightRotate(point);
}
template <class T>
void RBTree<T>::singleLeftRotate(RBNode<T>*& point) {
RBNode<T>* parent = point;
RBNode<T>* child = point->right;
child->parent = parent->parent;
parent->parent = child;
parent->right = child->left;
child->left = parent;
if (parent->right != nullptr)
parent->right->parent = parent;
if (child->parent == nullptr)
root = child;
else if (child->parent->left = parent)
child->parent->left = child;
else
child->parent->right = child;
}
template <class T>
void RBTree<T>::singleRightRotate(RBNode<T>*& point) {
RBNode<T>* parent = point;
RBNode<T>* child = point->left;
child->parent = parent->parent;
parent->parent = child;
parent->left = child->right;
child->right = parent;
if (parent->left != nullptr)
parent->left->parent = parent;
if (child->parent == nullptr)
root = child;
else if (child->parent->left = parent)
child->parent->left = child;
else
child->parent->right = child;
}
template <class T>
void RBTree<T>::insert(const T& toInsert, RBNode<T>*& point) {
insertHelper(toInsert, point);
balanceTree(point);
root = point;
while (root->parent != nullptr) {
root = root->parent;
}
}
template <class T>
void RBTree<T>::balanceTree(RBNode<T>* point) {
if (point->parent == nullptr)
insertCase1(point);
else if (point->parent->color == BLACK)
insertCase2(point);
else if (point->getUncle() != nullptr && point->getUncle()->color == RED)
insertCase3(point);
else
insertCase4(point);
}
template <class T>
void RBTree<T>::insertCase4(RBNode<T>* point) {
RBNode<T>* p = point->parent;
RBNode<T>* g = p->parent;
if (g->left == p) {
if (p->right == point)
doubleRightRotate(g);
else
singleRightRotate(g);
}
else {
if (p->left == point)
doubleLeftRotate(g);
else
singleLeftRotate(g);
}
p->color = BLACK;
g->color = RED;
}
template <class T>
void RBTree<T>::insertCase3(RBNode<T>* point) {
point->parent->color = BLACK;
point->getUncle()->color = BLACK;
point->parent->parent->color = RED;
balanceTree(point->parent->parent);
}
template <class T>
void RBTree<T>::insertCase2(RBNode<T>* point) {
return;
}
template <class T>
void RBTree<T>::insertCase1(RBNode<T>* point) {
point->color = BLACK;
}
template <class T>
void RBTree<T>::insertHelper(const T& toInsert, RBNode<T>*& point) {
if (point == nullptr)
point = new RBNode<T>(toInsert);
else if (toInsert < point->data) {
insertHelper(toInsert, point->left);
point->left->parent = point;
}
else {
insertHelper(toInsert, point->right);
point->right->parent = point;
}
}
template <class T>
void RBTree<T>::insert(const T& toInsert) {
insert(toInsert, root);
}
template <class T>
RBNode<T>* RBTree<T>::find(const T& toFind) const {
RBNode<T>* target = root;
while (target != nullptr) {
if (toFind < target->data)
target = target->left;
else if (toFind > target->data)
target = target->right;
else
return target;
}
return nullptr;
}
template <class T>
void RBTree<T>::clear() {
if (!isEmpty())
removeTree(root);
root = nullptr;
}
template <class T>
void RBTree<T>::removeTree(RBNode<T>* point) {
if (point->parent != nullptr) {
if (point->parent->left == point)
point->parent->left = nullptr;
else
point->parent->right = nullptr;
}
if (point->left != nullptr)
removeTree(point->left);
if (point->right != nullptr)
removeTree(point->right);
delete point;
}
template <class T>
RBNode<T>* RBTree<T>::recursiveCopy(RBNode<T>* toCopy) {
if (toCopy == nullptr)
return nullptr;
RBTree<T>* temp = new RBTree<T>(toCopy->data, nullptr, recursiveCopy(toCopy->left), recursiveCopy(toCopy->right));
if (temp->left != nullptr)
temp->left->parent = temp;
if (temp->right != nullptr)
temp->right->parent = temp;
return temp;
}
template <class T>
RBTree<T>& RBTree<T>::operator =(const RBTree<T>& rhs) {
if (this == &rhs)
return *this;
clear();
root = recursiveCopy(rhs.root);
return *this;
}
template <class T>
void RBTree<T>::printLevelOrder() const {
queue<RBNode<T>*> q;
q.push(root);
while (!q.empty()) {
RBNode<T>* front = q.front();
cout << front->data << "\t" << front->color << endl;
if (front->left != nullptr)
q.push(front->left);
if (front->right)
q.push(front->right);
q.pop();
}
}
int main() {
//RBNode<int>* one = new RBNode<int>(1);
RBTree<int> b;
//b.insert(0);
//b.insert(1);
srand((unsigned int)time(NULL));
for (int i = 0; i < 10; i++) {
int val = rand() % 1000;
b.insert(i);
}
cout << "finished inserting" << endl;
b.printLevelOrder();
}