-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedBlackTree.h
285 lines (251 loc) · 6.16 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
#pragma once
#include <iostream>
#include <queue>
#include <fstream>
#include <string>
#include <sstream>
enum Color :bool { Black = 0, Red = 1 };
template<typename T>
struct Node {
T data;
Node* parent;
Node* left;
Node* right;
bool is_nil;
bool color;
Node(T elm, Node* p = nullptr, Node* l = nullptr, Node* r = nullptr)
:data(elm), parent(p), left(l), right(r), is_nil(0), color(Red) {}
};
template<typename T>
class RedBlackTree {
private:
int _count;
Node<T>* _root;
void binarySearchTreeInsert(Node<T>*,Node<T>*);
void leftRotate(Node<T>*, Node<T>*);
void rightRotate(Node<T>*, Node<T>*);
void fixRbtInsert(Node<T>*);
void fixRbtDelete(Node<T>*);
void DeleteTree(Node<T>*);
public:
RedBlackTree();
void insert(T);
void remove(T);
const Node<T>* Find(T);
std::string print();
const Node<T>* inOrderTraversal(const Node<T>*);
const Node<T>* GetRoot() { return _root; }
~RedBlackTree() {
DeleteTree(_root);
}
};
template<typename T>
void RedBlackTree<T>::DeleteTree(Node<T>* t) {
//post order traversal
if (t == nullptr)return;
DeleteTree(t->left);
DeleteTree(t->right);
delete t;
}
template<typename T>
std::string RedBlackTree<T>::print() {
std::stringstream ss;
std::queue<Node<T>*> q;
q.push(_root);
// ss<<root->data<<"("<<root->height<<")("<<root->balance<<")"<<' ';
ss << _root->data << "(" << _root->color << ")" << ' ';
while (!q.empty()) {
Node<T>* t = q.front();
Node<T>* left = t->left;
Node<T>* right = t->right;
if (left != NULL) {
q.push(left);
// ss<<left->data<<"("<<left->height<<")("<<left->balance<<")"<<' ';
ss << left->data <<"("<<left->color<<")"<< ' ';
}
else {
ss << -1 << ' ';
}
if (right != NULL) {
q.push(right);
//ss<<right->data<<"("<<right->height<<")("<<right->balance<<")"<<' ';
ss << right->data << "(" << right->color << ")" << ' ';
}
else {
ss << -1 << ' ';
}
q.pop();
}
return ss.str();
}
template<typename T>
RedBlackTree<T>::RedBlackTree() {
_count = 0;
_root = nullptr;
}
template<typename T>
inline void RedBlackTree<T>::binarySearchTreeInsert(Node<T>* node, Node<T>* element) {
//utility function used to perform binary search tree insert
//empty tree
if (node == nullptr) {
element->color = Black;
_root = element;
return;
}
if (element->data < node->data && node->left != nullptr)
binarySearchTreeInsert(node->left, element);
else if (element->data < node->data && node->left == nullptr)
{
node->left = element;
element->parent = node;
}
else if (element->data > node->data && node->right != nullptr)
binarySearchTreeInsert(node->right, element);
else if (element->data > node->data && node->right == nullptr)
{
node->right = element;
element->parent = node;
}
}
template<typename T>
inline void RedBlackTree<T>::leftRotate(Node<T>*x, Node<T>*y)
{
auto temp = y->right;
y->right = temp->left;
if(y->right != nullptr)
temp->left->parent = y;
temp->parent = y->parent;
if (y->parent == nullptr)
x->parent = temp;
else if (y == y->parent->left)
y->parent->left = temp;
else
y->parent->right = temp;
temp->left = y;
y->parent = temp;
//why???? not working if rotating the root
if (x == y)
_root = temp;
}
template<typename T>
inline void RedBlackTree<T>::rightRotate(Node<T>*x, Node<T>*y)
{
auto temp = y->left;
y->left = temp->right;
if (y->left != nullptr)
temp->right->parent = y;
temp->parent = y->parent;
if (y->parent == nullptr)
x->parent = temp;
else if (y == y->parent->right)
y->parent->right = temp;
else
y->parent->left = temp;
temp->right = y;
y->parent = temp;
//why???? not working if sending root root
if (x == y)
_root = temp;
}
template<typename T>
inline void RedBlackTree<T>::fixRbtInsert(Node<T>*node)
{
while ( node->parent != nullptr && node->parent->color == Red) {
if (node->parent == node->parent->parent->left)
{
auto uncle = node->parent->parent->right;
if (uncle !=nullptr && uncle->color == Red){
//case1
node->parent->color = Black;
uncle->color = Black;
node->parent->parent->color = Red;
node = node->parent->parent;
}
else if (node == node->parent->right) {
//case2
node = node->parent;
leftRotate(_root, node);
}
else {
node->parent->color = Black;
node->parent->parent->color = Red;
rightRotate(_root,node->parent->parent);
}
}
else {
auto uncle = node->parent->parent->left;
if (uncle != nullptr && uncle->color == Red) {
//case1
node->parent->color = Black;
uncle->color = Black;
node->parent->parent->color = Red;
node = node->parent->parent;
}
else if (node == node->parent->left) {
//case2
node = node->parent;
rightRotate(_root,node);
}
else {
node->parent->color = Black;
node->parent->parent->color = Red;
leftRotate(_root, node->parent->parent);
}
}
}
_root->color = Black;
}
template<typename T>
inline void RedBlackTree<T>::insert(const T element) {
Node<T>* temporal_node = new Node<T>(element);
binarySearchTreeInsert(_root,temporal_node);
_count++;
//check red black tree for violation
//do not fix if we insert root
if(_count >1)
fixRbtInsert(temporal_node);
}
template<typename T>
inline const Node<T>* RedBlackTree<T>::Find(T element) {
auto node = _root;
if (_root == nullptr) return nullptr;
while (node != nullptr) {
if (element < node->data) {
std::cout << "At node " << node->data << " going left\n";
node = node->left;
}
else if (element > node->data) {
std::cout << "At node " << node->data << " going right\n";
node = node->right;
}
else {
std::cout << "Found node " << node->data << " terminated\n";
return node;
}
}
std::cout << "The tree does not contain node with key:" << element << "\n";
return nullptr;
}
template<typename T>
inline void RedBlackTree<T>::fixRbtDelete(Node<T>*)
{
}
template<typename T>
inline void RedBlackTree<T>::remove(T element)
{
if (_root == nullptr)return;
auto temp=Find(element);
if (temp == nullptr)return;
else {
_count--;
}
}
template<typename T>
const Node<T>* RedBlackTree<T>::inOrderTraversal(const Node<T>* node)
{
if (node == NULL)
return nullptr;
inOrderTraversal(node->left);
std::cout << node->data << " ";
inOrderTraversal(node->right);
}