-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrb_binary_tree.hh
More file actions
350 lines (296 loc) · 9.64 KB
/
Copy pathrb_binary_tree.hh
File metadata and controls
350 lines (296 loc) · 9.64 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
#ifndef RB_BINARY_TREE_HH
#define RB_BINARY_TREE_HH
#include "binary_tree.hh"
#include "rb_tree_node.hh"
#define RB_PARENT(z) ((RedBlackTreeNode<T> *)z->get_parent())
#define RB_GRANDPARENT(z) ((RedBlackTreeNode<T> *)(z->get_parent()->get_parent()))
template <class T>
class RBBinarySearchTree : public BinarySearchTree<T>
{
// rotatie spre stanga in jurul nodului x;
void left_rotate(TreeNode<T> *x);
// rotatie la dreapta in jurul nodului x;
void right_rotate(TreeNode<T> *x);
// restabileste culorile in arbore dupa inserarea unui nou nod;
void insert_fixup(RedBlackTreeNode<T> *z);
//restabileste culorile in arbore dupa stergerea unui nod
void remove_fixup(RedBlackTreeNode<T> *z);
protected:
void remove_node(const TreeNode<T> *z);
void transplant(TreeNode<T> *u, TreeNode<T> *v);
public:
RBBinarySearchTree();
~RBBinarySearchTree();
void insert(T x);
// void remove(T x);
};
template <class T>
RBBinarySearchTree<T>::RBBinarySearchTree() : BinarySearchTree<T>()
{
this->nil = new RedBlackTreeNode<T>(0, BLACK);
this->root = this->nil;
}
template <class T>
RBBinarySearchTree<T>::~RBBinarySearchTree()
{
delete this->nil;
}
template <class T>
void RBBinarySearchTree<T>::left_rotate(TreeNode<T> *x)
{
// se presupune ca dreapta lui x este nenula;
TreeNode<T> *y = (TreeNode<T> *)x->get_right();
// dreapta lui x devine stanga lui y;
x->set_right(y->get_left());
// daca stanga lui y este nenula
if (y->get_left() != this->nil)
{
// atunci stanga lui y primeste ca parinte pe x;
TreeNode<T> *y_left = (TreeNode<T> *)y->get_left();
y_left->set_parent(x);
}
// parintele lui x devine acum parintele lui y;
y->set_parent(x->get_parent());
TreeNode<T> *x_parent = (TreeNode<T> *)x->get_parent();
// daca parintele lui x este nul (i.e. x este radacina)
if (x_parent == this->nil)
{
// atunci y devine radacina
this->root = y;
}
// daca x a fost copil stang
else if (x == x_parent->get_left())
{
// atunci y devine si el copil stang;
x_parent->set_left(y);
}
else // altfel devine copil drept;
{
x_parent->set_right(y);
}
// x este acum stanga lui y;
y->set_left(x);
// dupa care y devine parintele lui x;
x->set_parent(y);
}
template <class T>
void RBBinarySearchTree<T>::right_rotate(TreeNode<T> *x)
{
// codul este perfect simetric cu cazul "left_rotate"
// doar se interschimba "left" cu "right";
TreeNode<T> *y = (TreeNode<T> *)x->get_left();
x->set_left(y->get_right());
if (y->get_right() != this->nil)
{
TreeNode<T> *y_right = (TreeNode<T> *)y->get_right();
y_right->set_parent(x);
}
y->set_parent(x->get_parent());
TreeNode<T> *x_parent = (TreeNode<T> *)x->get_parent();
if (x_parent == this->nil)
{
this->root = y;
}
else if (x == x_parent->get_left())
{
x_parent->set_left(y);
}
else
{
x_parent->set_right(y);
}
y->set_right(x);
x->set_parent(y);
}
template <class T>
void RBBinarySearchTree<T>::insert_fixup(RedBlackTreeNode<T> *z)
{
RedBlackTreeNode<T> *y;
while (RB_PARENT(z)->get_color() == RED)
{
if (z->get_parent() == RB_GRANDPARENT(z)->get_left())
{
y = (RedBlackTreeNode<T> *)z->get_parent()->get_parent()->get_right();
if (y->get_color() == RED)
{
RB_PARENT(z)->set_color(BLACK);
y->set_color(BLACK);
RB_GRANDPARENT(z)->set_color(RED);
z = RB_GRANDPARENT(z);
}
else
{
if (z == z->get_parent()->get_right())
{
z = RB_PARENT(z);
this->left_rotate(z);
}
RB_PARENT(z)->set_color(BLACK);
RB_GRANDPARENT(z)->set_color(RED);
this->right_rotate(RB_GRANDPARENT(z));
}
}
else
{
y = (RedBlackTreeNode<T> *)z->get_parent()->get_parent()->get_left();
if (y->get_color() == RED)
{
RB_PARENT(z)->set_color(BLACK);
y->set_color(BLACK);
RB_GRANDPARENT(z)->set_color(RED);
z = RB_GRANDPARENT(z);
}
else
{
if (z == z->get_parent()->get_left())
{
z = RB_PARENT(z);
this->right_rotate(z);
}
RB_PARENT(z)->set_color(BLACK);
RB_GRANDPARENT(z)->set_color(RED);
this->left_rotate(RB_GRANDPARENT(z));
}
}
}
((RedBlackTreeNode<T> *)this->root)->set_color(BLACK);
}
template <class T>
void RBBinarySearchTree<T>::transplant(TreeNode<T> *u, TreeNode<T> *v)
{
this->tree_transplant(u, v);
v->set_parent(u->get_parent());
}
template <class T>
void RBBinarySearchTree<T>::insert(T x)
{
// copiaza cheia lui n intr-un RedBlackTreeNode;
RedBlackTreeNode<T> *z = new RedBlackTreeNode<T>(x);
// inserarea propriu-zisa nu tine cont de culoare
// deci se poate apela metoda insert ca intr-un arbore simplu;
if (BinarySearchTree<T>::insert(z) != this->nil)
{
// seteaza copiii lui z la nil;
z->set_left(this->nil);
z->set_right(this->nil);
// insert_fixup este specifica unui RedBlackSearchTree
// si restabileste regulile unui RB-Tree;
this->insert_fixup(z);
}
}
template <class T>
void RBBinarySearchTree<T>::remove_node(const TreeNode<T> *z)
{
// copiaza cheia lui n intr-un RedBlackTreeNode;
RedBlackTreeNode<T> *y;
RedBlackTreeNode<T> *x;
y = (RedBlackTreeNode<T> *)z;
NodeColor y_original_color = y->get_color();
if (z->get_left() == this->nil)
{
x = (RedBlackTreeNode<T> *)z->get_right();
this->transplant((TreeNode<T> *)z, (TreeNode<T> *)z->get_right());
}
else if (z->get_right() == this->nil)
{
x = (RedBlackTreeNode<T> *)z->get_left();
this->transplant((TreeNode<T> *)z, (TreeNode<T> *)z->get_left());
}
else
{
y = (RedBlackTreeNode<T> *)this->minimum(z->get_right()); // <- treb apelata functia din binary_tree
y_original_color = y->get_color();
x = (RedBlackTreeNode<T> *)y->get_right();
if (y->get_parent() == z)
{
x->set_parent(y);
}
else
{
this->transplant(y, (TreeNode<T> *)y->get_right());
y->set_right(z->get_right());
((TreeNode<T> *)y->get_right())->set_parent(y);
}
this->transplant((TreeNode<T> *)z, y);
y->set_left(z->get_left());
((TreeNode<T> *)y->get_left())->set_parent(y);
y->set_color(((RedBlackTreeNode<T> *)z)->get_color());
}
delete z;
if (y_original_color == BLACK)
{
remove_fixup(x);
}
}
template <class T>
void RBBinarySearchTree<T>::remove_fixup(RedBlackTreeNode<T> *x)
{
RedBlackTreeNode<T> *w;
while (x != this->root && x->get_color() == BLACK)
{
if (x == x->get_parent()->get_left())
{
w = (RedBlackTreeNode<T> *)x->get_parent()->get_right();
if (w->get_color() == RED)
{
w->set_color(BLACK);
RB_PARENT(x)->set_color(RED);
this->left_rotate(PARENT(x)); // <------ aici
w = (RedBlackTreeNode<T> *)x->get_parent()->get_right();
}
if (w->get_left()->get_color() == BLACK && w->get_right()->get_color() == BLACK)
{
w->set_color(RED);
x = RB_PARENT(x);
}
else
{
if (w->get_right()->get_color() == BLACK)
{
((RedBlackTreeNode<T> *)w->get_left())->set_color(BLACK);
w->set_color(RED);
this->right_rotate(w); // <------- si aici
w = (RedBlackTreeNode<T> *)x->get_parent()->get_right();
}
w->set_color(x->get_parent()->get_color());
RB_PARENT(x)->set_color(BLACK);
((RedBlackTreeNode<T> *)w->get_right())->set_color(BLACK);
this->left_rotate(RB_PARENT(x)); // <---------- si aici
x = (RedBlackTreeNode<T> *)this->root;
}
}
else
{
w = (RedBlackTreeNode<T> *)x->get_parent()->get_left();
if (w->get_color() == RED)
{
w->set_color(BLACK);
RB_PARENT(x)->set_color(RED);
this->right_rotate(PARENT(x));
w = (RedBlackTreeNode<T> *)x->get_parent()->get_left();
}
if (w->get_right()->get_color() == BLACK && w->get_left()->get_color() == BLACK)
{
w->set_color(RED);
x = RB_PARENT(x);
}
else
{
if (w->get_left()->get_color() == BLACK)
{
((RedBlackTreeNode<T> *)w->get_right())->set_color(BLACK);
w->set_color(RED);
this->left_rotate(w); // aiici
w = (RedBlackTreeNode<T> *)x->get_parent()->get_left();
}
w->set_color(x->get_parent()->get_color());
RB_PARENT(x)->set_color(BLACK);
((RedBlackTreeNode<T> *)w->get_left())->set_color(BLACK);
this->right_rotate(RB_PARENT(x)); // aici
x = (RedBlackTreeNode<T> *)this->root;
}
}
}
x->set_color(BLACK);
}
#endif