-
Notifications
You must be signed in to change notification settings - Fork 6
/
red_black.c
235 lines (202 loc) · 4.83 KB
/
red_black.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
#include <assert.h>
#include <stdlib.h>
#include "red_black_internal.h"
red_black_node_t null_node = {.color = COLOR_BLACK};
const struct map_vtable_struct red_black_vtable =
{
.get = (map_get_t) red_black_get,
.set = (map_set_t) red_black_set,
.free = (map_free_t) red_black_free
};
map_t *init_red_black()
{
red_black_t *tree = malloc(sizeof(red_black_t));
tree->base.vtable = &red_black_vtable;
tree->root = &null_node;
return (map_t*) tree;
}
void red_black_free_node(red_black_node_t *node)
// Depth first free the nodes
{
if(node != &null_node) {
red_black_free_node(node->left);
red_black_free_node(node->right);
free(node);
}
}
void red_black_free(red_black_t *tree)
{
red_black_free_node(tree->root);
free(tree);
}
void left_rotate(red_black_t *tree, red_black_node_t *x)
//
// ( x ) ( y )
// / \ -> / \
// (a) (y) (x) (c)
// / \ / \
// (b) (c) (a) (b)
{
red_black_node_t *y = x->right;
assert(y != &null_node);
x->right = y->left;
if(y->left != &null_node) {
y->left->parent = x;
}
y->parent = x->parent;
// Either x is the root of tree,
// or x is the left or right node of its
// parent. Update this to location to point to y.
if (x->parent == &null_node) {
tree->root = y;
}
else if ( x == x->parent->left) {
x->parent->left = y;
}
else {
assert(x->parent->right == x);
x->parent->right = y;
}
y->left = x;
x->parent = y;
}
void right_rotate(red_black_t *tree, red_black_node_t *y)
//
// ( x ) ( y )
// / \ <- / \
// (a) (y) (x) (c)
// / \ / \
// (b) (c) (a) (b)
{
red_black_node_t *x = y->left;
assert(x != &null_node);
y->left = x->right;
if(x->right != &null_node) {
x->right->parent = y;
}
x->parent = y->parent;
// Either y is the root of the tree or the left
// or right node of its parent. Update the pointer from the parent
// to point to x
if (y->parent == &null_node) {
tree->root = x;
}
else if (y->parent->left == y) {
y->parent->left = x;
}
else {
assert(y->parent->right == y);
y->parent->right = x;
}
x->right = y;
y->parent = x;
}
void red_black_insert_fixup(red_black_t *tree, red_black_node_t *z)
// Maintain the balance of the red-black tree (see Intro to Algorithms, pg 316)
{
while(z->parent->color == COLOR_RED) {
assert(z->color == COLOR_RED);
if(z->parent == z->parent->parent->left) {
red_black_node_t *y = z->parent->parent->right;
if(y->color == COLOR_RED) {
z->parent->color = COLOR_BLACK;
y->color = COLOR_BLACK;
z->parent->parent->color = COLOR_RED;
z = z->parent->parent;
}
else {
if(z == z->parent->right) {
z = z->parent;
left_rotate(tree, z);
}
z->parent->color = COLOR_BLACK;
z->parent->parent->color = COLOR_RED;
right_rotate(tree, z->parent->parent);
}
}
else {
red_black_node_t *y = z->parent->parent->left;
if(y->color == COLOR_RED) {
z->parent->color = COLOR_BLACK;
y->color = COLOR_BLACK;
z->parent->parent->color = COLOR_RED;
z = z->parent->parent;
}
else {
if(z == z->parent->left) {
z = z->parent;
right_rotate(tree, z);
}
z->parent->color = COLOR_BLACK;
z->parent->parent->color = COLOR_RED;
left_rotate(tree, z->parent->parent);
}
}
}
tree->root->color = COLOR_BLACK;
}
void red_black_set(red_black_t *tree, int key, void *value)
{
red_black_node_t *y = &null_node;
red_black_node_t *x = tree->root;
while(x != &null_node) {
y = x;
if(key < x->key) {
x = x->left;
}
else if(key > x->key) {
x = x->right;
}
else {
// Update rather than an insertion
assert(key == x->key);
x->value = value;
return;
}
}
// Insertion so create a new entry
red_black_node_t *z = malloc(sizeof(red_black_node_t));
z->parent = y;
z->key = key;
z->value = value;
z->left = &null_node;
z->right = &null_node;
z->color = COLOR_RED;
// y is either null (this is the root of the tree,
// or a leaf node and this new node should be linked to the left
// or right side of y (guaranteed to be empty due to the termination
// condition of the while above.
if(y == &null_node) {
tree->root = z;
}
else if (key < y->key) {
y->left = z;
}
else {
assert(key > y->key);
y->right = z;
}
red_black_insert_fixup(tree, z);
}
void* tree_search(red_black_node_t *node, int key, bool *found)
{
if(node == &null_node) {
*found = false;
return NULL;
}
if(key < node->key) {
return tree_search(node->left, key, found);
}
else if(key > node->key) {
return tree_search(node->right, key, found);
}
else {
assert(key == node->key);
*found = true;
return node->value;
}
}
void* red_black_get(red_black_t *tree, int key, bool *found)
{
return tree_search(tree->root, key, found);
}