-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbtree.c
309 lines (287 loc) · 12.9 KB
/
rbtree.c
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
#include "rbtree.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct rb_node rb_node;
static inline void msg(const char* s) {
printf("%s\n", s);
}
// sentinel node: all leaf nodes and the parent of the root, color is black
struct rb_node __sentinel_nil = {NIL, RB_BLACK, NULL, NULL};
#define RED "\x1B[31m"
#define GRN "\x1B[32m"
#define YEL "\x1B[33m"
#define BLU "\x1B[34m"
#define MAG "\x1B[35m"
#define CYN "\x1B[36m"
#define WHT "\x1B[37m"
#define RESET "\x1B[0m"
struct rb_node* get_node() {
struct rb_node* node = malloc(sizeof(struct rb_node));
assert(node);
node->rb_parent = node->rb_left = node->rb_right = NIL;
node->rb_color = RB_BLACK;
node->key = -1;
node->s = NULL;
return node;
}
// check [here](http://www.cse.yorku.ca/~oz/hash.html)
static unsigned long hash(unsigned char* str, int *len) {
unsigned long hash = 5381;
int c;
*len = 0;
while ((c = *str++)) {
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
(*len)++;
}
return hash;
}
struct rb_node* get_node_str(unsigned char* ps) {
struct rb_node *node = get_node();
if (ps) {
int len = 0;
unsigned long k = hash(ps, &len);
node->key = k;
node->s = malloc((len + 1) * sizeof(unsigned char));
node->s[len] = '\0';
while (len--) node->s[len] = ps[len]; // copy the string
//printf("node_str: %s\n", node->s);
}
return node;
}
/*
| |
y <-----left_rotate(T, x)---- x
/ \ / \
x c ------right_rotate(T, y)---> a y
/ \ / \
a b b c
*/
// root and x both are not NULL
static rb_node* left_rotate(rb_node* root, rb_node* x) {
rb_node* y = x->rb_right;
x->rb_right = y->rb_left;
if (y->rb_left != NIL) { // b is not NIL
y->rb_left->rb_parent = x;
}
y->rb_parent = x->rb_parent;
if (x->rb_parent == NIL) { // x is root
root = y;
} else if (x == x->rb_parent->rb_left) { // x is its parent's left child
x->rb_parent->rb_left = y;
} else { // x is its patent's right child
x->rb_parent->rb_right = y;
}
y->rb_left = x;
x->rb_parent = y;
return root;
}
// root and y both are not NULL
static rb_node* right_rotate(rb_node* root, rb_node* y) {
rb_node *x = y->rb_left;
y->rb_left = x->rb_right;
if (x->rb_right != NIL) { // b is not NIL
x->rb_right->rb_parent = y;
}
x->rb_parent = y->rb_parent;
if (y->rb_parent == NIL) { // y is root
root = x;
} else if (y == y->rb_parent->rb_left) {
y->rb_parent->rb_left = x;
} else {
y->rb_parent->rb_right = x;
}
x->rb_right = y;
y->rb_parent = x;
return root;
}
static struct rb_node* rb_insert_fixup(rb_node* root, rb_node* z) {
while (z->rb_parent->rb_color == RB_RED) { // violation: both 'z' and 'z->rb_parent' are red
if (z->rb_parent == z->rb_parent->rb_parent->rb_left) { // z's parent is a left child
rb_node* y = z->rb_parent->rb_parent->rb_right; // let 'y' be z's uncle
if (y->rb_color == RB_RED) { // uncle is red
z->rb_parent->rb_color = RB_BLACK; // set parent BLACK
y->rb_color = RB_BLACK; // set uncle BLACK
z->rb_parent->rb_parent->rb_color = RB_RED; // set grandparent RED
z = z->rb_parent->rb_parent; // let 'z' be its grandparent, now 'z' is still RED
} else {
if (z == z->rb_parent->rb_right) { // 'z' is a right child
z = z->rb_parent; root = left_rotate(root, z); // z's height is kept, now uncle is black
}
// we are in case 3
z->rb_parent->rb_color = RB_BLACK;
z->rb_parent->rb_parent->rb_color = RB_RED;
root = right_rotate(root, z->rb_parent->rb_parent);
}
} else { // z's parent is a right child
rb_node* y = z->rb_parent->rb_parent->rb_left; // let 'y' be z's uncle
if (y->rb_color == RB_RED) { // uncle is red
z->rb_parent->rb_color = RB_BLACK; // set parent BLACK
y->rb_color = RB_BLACK; // set uncle BLACK
z->rb_parent->rb_parent->rb_color = RB_RED; // set grandparent RED
z = z->rb_parent->rb_parent; // let 'z' be its grandparent, now 'z' is still RED
} else {
if (z == z->rb_parent->rb_left) { // 'z' is a right child
z = z->rb_parent; root = right_rotate(root, z); // z's height is kept, now uncle is black
}
// we are in case 3
z->rb_parent->rb_color = RB_BLACK;
z->rb_parent->rb_parent->rb_color = RB_RED;
root = left_rotate(root, z->rb_parent->rb_parent);
}
}
}
root->rb_color = RB_BLACK;
return root;
}
// 'z' can not be NULL, however 'root' can
struct rb_node* rb_insert(rb_node *root, rb_node *z) {
struct rb_node *y = NIL, *x = root; // y is the parent of x
while (x != NIL) {
y = x; // keep y to be the parent of x
if (z->key < x->key) {
x = x->rb_left; // go left
} else {
x = x->rb_right; // go right
}
}
z->rb_parent = y; // x is NIL now, and y is the parent of x
if (y == NIL) {
root = z; // empty tree, we make 'z' root node
} else if (z->key < y->key) {
y->rb_left = z;
} else {
y->rb_right = z;
}
z->rb_left = z->rb_right = NIL; // if we get 'z' by 'get_node', there's no need to assign 'left' and 'right'
z->rb_color = RB_RED; // colorize it
root = rb_insert_fixup(root, z);
return root;
}
static rb_node* rb_transplant(rb_node *root, rb_node *u, rb_node *v) {
if (u->rb_parent == NIL) { // u is root
root = v;
} else if (u == u->rb_parent->rb_left) { // u is a left child
u->rb_parent->rb_left = v;
} else { // u is a right child
u->rb_parent->rb_right = v;
}
v->rb_parent = u->rb_parent;
return root;
}
struct rb_node* rb_minimum(rb_node *root) {
while (root->rb_left != NIL)
root = root->rb_left;
return root;
}
struct rb_node *rb_delete_fixup(rb_node* root, rb_node* x) {
while (x != root && x->rb_color == RB_BLACK) {
if (x == x->rb_parent->rb_left) {
// x's brother: w, and w can never be NIL
rb_node *w = x->rb_parent->rb_right;
if (w->rb_color == RB_RED) {
w->rb_color = RB_BLACK;
x->rb_parent->rb_color = RB_RED;
root = left_rotate(root, x->rb_parent);
w = x->rb_parent->rb_right; // w is still x's brother, with black color
}
if (w->rb_left->rb_color == RB_BLACK && w->rb_right->rb_color == RB_BLACK) {
w->rb_color = RB_RED; // remove the extra black color of x and w
x = x->rb_parent; // add the extra black color to x->rb_parent, keep looping
} else {
if (w->rb_right->rb_color == RB_BLACK) {
w->rb_left->rb_color = RB_BLACK;
w->rb_color = RB_RED;
root = right_rotate(root, w);
w = x->rb_parent->rb_right;
}
w->rb_color = x->rb_parent->rb_color;
x->rb_parent->rb_color = RB_BLACK;
w->rb_right->rb_color = RB_BLACK;
root = left_rotate(root, x->rb_parent);
x = root;
}
} else {
// x's brother: w, and w can never be NIL
rb_node *w = x->rb_parent->rb_left;
if (w->rb_color == RB_RED) {
w->rb_color = RB_BLACK;
x->rb_parent->rb_color = RB_RED;
root = right_rotate(root, x->rb_parent);
w = x->rb_parent->rb_left; // w is still x's brother, with black color
}
if (w->rb_right->rb_color == RB_BLACK && w->rb_left->rb_color == RB_BLACK) {
w->rb_color = RB_RED; // remove the extra black color of x and w
x = x->rb_parent; // add the extra black color to x->rb_parent, keep looping
} else {
if (w->rb_left->rb_color == RB_BLACK) {
w->rb_right->rb_color = RB_BLACK;
w->rb_color = RB_RED;
root = left_rotate(root, w);
w = x->rb_parent->rb_left;
}
w->rb_color = x->rb_parent->rb_color;
x->rb_parent->rb_color = RB_BLACK;
w->rb_left->rb_color = RB_BLACK;
root = right_rotate(root, x->rb_parent);
x = root;
}
}
}
/*
* if x is root, we just remove the extra black
* if x is RB_RED, we just set it black
*/
x->rb_color = RB_BLACK;
return root;
}
struct rb_node* rb_delete(rb_node *root, rb_node *z) {
rb_node *y = z;
rb_node *x = NULL;
int y_original_color = y->rb_color;
if (z->rb_left == NIL) { // z only has right child
x = z->rb_right;
root = rb_transplant(root, z, x); // replace z with x
} else if (z->rb_right == NIL) { // z only has left child
x = z->rb_left;
root = rb_transplant(root, z, x);
} else { // z has both left and right children
// now we use z's minimum to replace z
y = rb_minimum(z->rb_right); // we know y has no left child
y_original_color = y->rb_color;
x = y->rb_right; // x may be NIL
if (y != z->rb_right) {
root = rb_transplant(root, y, x); // we kick 'y' out, let 'x' replace it, x's parent is y's parent
y->rb_right = z->rb_right;
y->rb_right->rb_parent = y;
} else {
x->rb_parent = y; // even x is NIL
}
root = rb_transplant(root, z, y); // we kick 'z' out, let 'y' replace it
y->rb_left = z->rb_left;
y->rb_left->rb_parent = y;
y->rb_color = z->rb_color;
}
if (y_original_color == RB_BLACK) root = rb_delete_fixup(root, x);
return root;
}
inline void free_node(rb_node* p) {
if (p && p != NIL) free(p);
}
struct rb_node* rb_search(rb_node* root, long long key) {
while (root != NIL) {
if (root->key == key) break;
if (root->key > key) root = root->rb_left;
if (root->key < key) root = root->rb_right;
}
return root;
}
void tranversal_inorder(struct rb_node *root) {
if (root == NIL) return;
tranversal_inorder(root->rb_left);
int flag = root->s ? 1 : 0;
printf("%s%lld %s%s", root->rb_color == RB_RED ? RED : RESET, root->key,
flag ? root->s : " ",
flag ? "\n" : " ");
tranversal_inorder(root->rb_right);
}