-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.h
290 lines (266 loc) · 9.02 KB
/
tree.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
286
287
288
289
290
const std::string red("\033[0;31m");
const std::string green("\033[1;32m");
const std::string yellow("\033[1;33m");
const std::string cyan("\033[0;36m");
const std::string magenta("\033[0;35m");
const std::string reset("\033[0m");
template<typename T>
struct Node
{
Node(T key, Color color = RED) : key(key), color(color) {}
T key;
Node *parent = nullptr;
Node *left = nullptr;
Node *right = nullptr;
Color color;
};
template<typename T>
class BinarySearchTree
{
public:
BinarySearchTree() : root(nullptr) {}
~BinarySearchTree() {
free_tree(root);
}
void tree_insert(Node<T> *z) {
Node<T> *x = root;
Node<T> *y = nullptr;
while (x != nullptr) {
y = x;
if (z->key < x->key) {
x = x->left;
} else {
x = x->right;
}
}
z->parent = y;
if (y == nullptr) {
root = z;
} else if (z->key < y->key) {
y->left = z;
} else {
y->right = z;
}
}
void inorder_tree_walk(Node<T> *x) {
if (x != nullptr) {
inorder_tree_walk(x->left);
std::cout << x->key << " ";
inorder_tree_walk(x->right);
}
}
Node<T> *tree_search(Node<T> *x, T key) {
if (x == nullptr or x->key == key) {
return x;
}
if (key < x->key) {
return tree_search(x->left, key);
} else {
return tree_search(x->right, key);
}
}
Node<T> *iterative_tree_search(Node<T> *x, T key) {
while (x != nullptr and x->key != key) {
if (key < x->key) {
x = x->left;
} else {
x = x->right;
}
}
return x;
}
Node<T> *tree_minimum(Node<T> *x) {
while (x->left != nullptr) {
x = x->left;
}
return x;
}
Node<T> *tree_maximum(Node<T> *x) {
while (x->right != nullptr) {
x = x->right;
}
return x;
}
Node<T> *tree_successor(Node<T> *x) {
if (x->right != nullptr) {
return tree_minimum(x->right);
}
Node<T> *y = x->parent;
while (y != nullptr and x == y->right) {
x = y;
y = y->parent;
}
return y;
}
Node<T> *tree_predecessor(Node<T> *x) {
if (x->left != nullptr) {
return tree_maximum(x->left);
}
Node<T> *y = x->parent;
while (y != nullptr and x == y->left) {
x = y;
y = y->parent;
}
return y;
}
void tree_delete(Node<T> *z) {
if (z->left == nullptr) {
transplant(root, z, z->right);
} else if (z->right == nullptr) {
transplant(root, z, z->left);
} else {
Node<T> *y = tree_minimum(z->right);
if (y->parent != z) {
transplant(root, y, y->right);
y->right = z->right;
y->right->parent = y;
}
transplant(root, z, y);
y->left = z->left;
y->left->parent = y;
}
}
void print_tree() {
print_tree(root);
}
protected:
Node<T> *root;
void free_tree(Node<T> *&root) {
if (root != nullptr) {
free_tree(root->left);
free_tree(root->right);
delete root;
root = nullptr;
}
}
void print_tree(Node<T> *root, int level = 0) {
if (root != nullptr) {
print_tree(root->right, level + 1);
for (int i = 0; i < level; ++i) {
std::cout << " ";
}
if (root->color == RED) {
std::cout << red << root->key << reset << std::endl;
} else {
std::cout << root->key << std::endl;
}
print_tree(root->left, level + 1);
}
}
void transplant(Node<T> *&root, Node<T> *u, Node<T> *v) { // replace u with v
if (u->parent == nullptr) { // u is the root
root = v;
} else if (u == u->parent->left) { // u is a left child
u->parent->left = v;
} else { // u is a right child
u->parent->right = v;
}
if (v != nullptr) {
v->parent = u->parent;
}
}
};
template<typename T>
class RedBlackTree : public BinarySearchTree<T>
{
public:
RedBlackTree() : BinarySearchTree<T>() {}
void rb_insert(Node<T> *z) {
// start by inserting z as if it were an ordinary binary search tree
this->tree_insert(z);
z->left = nullptr;
z->right = nullptr;
z->color = RED;
rb_insert_fixup(this->root, z);
}
private:
// assume x->right is not nullptr
// result: indorder tree walk of the input tree and the modified tree produce the same listing of key values
void left_rotate(Node<T> *&root, Node<T> *x) {
Node<T> *y = x->right;
x->right = y->left; // turn y's left subtree into x's right subtree
if (y->left != nullptr) { // if y's left subtree is not nullptr
y->left->parent = x;
}
y->parent = x->parent; // x's parent becomes y's parent
if (x->parent == nullptr) { // if x was the root
root = y; // ...then y becomes the root
} else if (x == x->parent->left) { // otherwise, if x was a left child
x->parent->left = y; // ...then y becomes a left child
} else {
x->parent->right = y; // otherwise, y becomes a right child
}
y->left = x;
x->parent = y;
}
void right_rotate(Node<T> *&root, Node<T> *y) {
Node<T> *x = y->left;
y->left = x->right;
if (x->right != nullptr) {
x->right->parent = y;
}
x->parent = y->parent;
if (y->parent == nullptr) {
root = x;
} else if (y == y->parent->left) {
y->parent->left = x;
} else {
y->parent->right = x;
}
x->right = y;
y->parent = x;
}
void rb_insert_fixup(Node<T> *&root, Node<T> *z) {
while (z->parent && z->parent->color == RED) {
if (z->parent == z->parent->parent->left) { // is z's parent a left child? here `z->parent->parent` must exist, since z->parent is red, so z->parent is not the root
Node<T> *y = z->parent->parent->right; // y is z's uncle on the right of z's grandparent
if (y->color == RED) { // case 1: z's parent and uncle are both red
z->parent->color = BLACK;
y->color = BLACK;
z->parent->parent->color = RED;
z = z->parent->parent; // z move up 2 levels
} else { // z's uncle is black
if (z == z->parent->right) { // case 2: z's uncle is black and z is a right child
z = z->parent;
left_rotate(root, z);
}
z->parent->color = BLACK; // case 3: z's uncle is black and z is a left child
z->parent->parent->color = RED;
right_rotate(root, z->parent->parent);
}
} else { // z's parent is a right child
Node<T> *y = z->parent->parent->left; // y is z's uncle on the left
Color y_color = BLACK; // leaf nodes are black
if (y != nullptr) { // y might be nullptr
y_color = y->color;
}
if (y_color == RED) { // case 1: z's parent and uncle are both red
z->parent->color = BLACK;
y->color = BLACK;
z->parent->parent->color = RED;
z = z->parent->parent;
} else { // z's uncle is black
if (z == z->parent->left) { // case 2: z's uncle is black and z is a left child
z = z->parent;
right_rotate(root, z);
} // case 3: z's uncle is black and z is a right child
z->parent->color = BLACK;
z->parent->parent->color = RED;
left_rotate(root, z->parent->parent);
}
}
}
root->color = BLACK;
}
void rb_transplant(Node<T> *&root, Node<T> *u, Node<T> *v) {
if (u->parent == nullptr) {
root = v;
} else if (u == u->parent->left) {
u->parent->left = v;
} else {
u->parent->right = v;
}
// could be a problem if v is nullptr. the text book take advantage of this
v->parent = u->parent;
}
};