-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbt.c
280 lines (238 loc) · 6.83 KB
/
rbt.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rbt.h"
#include "mylib.h"
#define IS_BLACK(x) ((NULL == (x)) || (BLACK == (x)->colour))
#define IS_RED(x) ((NULL != (x)) && (RED == (x)->colour))
/* -------------------------------------------------------*/
/* rbt class */
/* -------------------------------------------------------*/
/*Enumerated type used to define a nodes color.
*/
typedef enum { RED, BLACK } rbt_colour;
/*A struct for an red-black tree node that has a
*character array key, associated colour and left
*or right nodes that may or may not be NULL.
*/
struct rbtnode {
char *key;
rbt_colour colour;
rbt left;
rbt right;
};
/*Method that creates a new RBT and sets the pointer to NULL.
*Returns NULL
*/
rbt rbt_new() {
return NULL;
}
/*Method that recursively searches for a specific word in the RBT.
*Param: b, the RBT
*Param: *str, the word to search for
*Returns 0 if the RBT is null, 1 if the words are the same
*/
int rbt_search(rbt b, char *str) {
if(b == NULL) {
return 0;
}
if(strcmp(b->key, str) == 0) {
return 1;
}
if(strcmp(b->key, str) > 0) {
return rbt_search(b->left, str);
}
return rbt_search(b->right, str);
}
/*Method that will do a right rotation in the current RBT.
*Param: b, the RBT
*/
static rbt rbt_right_rotate(rbt b) {
struct rbtnode *temp = b;
struct rbtnode *root = b;
root = root->left;
temp->left = root->right;
root->right = temp;
return root;
}
/*Method that will do a left rotation in the current RBT.
*Param: b, the RBT
*/
static rbt rbt_left_rotate(rbt b) {
struct rbtnode *temp = b;
struct rbtnode *root = b;
root = root->right;
temp->right = root->left;
root->left = temp;
return root;
}
/*Method that corrects an RBT if it breaks one of the RBT standard rules.
*Used when an insertion has resulted in two consecutive reds
*Param: rbt, the RBT
*Returns the corrected RBT
*/
rbt rbt_fix(rbt rbt) {
if(IS_RED(rbt->left) && IS_RED(rbt->left->left)) {
if(IS_RED(rbt->right)) {
rbt->colour = RED;
rbt->left->colour = BLACK;
rbt->right->colour = BLACK;
} else if(IS_BLACK(rbt->right)) {
rbt = rbt_right_rotate(rbt);
rbt->colour = BLACK;
rbt->right->colour = RED;
}
}else if(IS_RED(rbt->left) && IS_RED(rbt->left->right)) {
if(IS_RED(rbt->right)) {
rbt->colour = RED;
rbt->left->colour = BLACK;
rbt->right->colour = BLACK;
} else if(IS_BLACK(rbt->right)) {
rbt->left = rbt_left_rotate(rbt->left);
rbt = rbt_right_rotate(rbt);
rbt->colour = BLACK;
rbt->right->colour = RED;
}
}else if(IS_RED(rbt->right) && IS_RED(rbt->right->left)) {
if(IS_RED(rbt->left)) {
rbt->colour = RED;
rbt->left->colour = BLACK;
rbt->right->colour = BLACK;
} else if(IS_BLACK(rbt->left)) {
rbt->right = rbt_right_rotate(rbt->right);
rbt = rbt_left_rotate(rbt);
rbt->colour = BLACK;
rbt->left->colour = RED;
}
}else if(IS_RED(rbt->right) && IS_RED(rbt->right->right)) {
if(IS_RED(rbt->left)) {
rbt->colour = RED;
rbt->left->colour = BLACK;
rbt->right->colour = BLACK;
}else if(IS_BLACK(rbt->left)) {
rbt = rbt_left_rotate(rbt);
rbt->colour = BLACK;
rbt->left->colour = RED;
}
}
return rbt;
}
/*Method that will insert a particular word into the RBT.
*Parameters: the RBT and the word that we want to insert
*Returns the new RBT with the word in it
*/
rbt rbt_insert(rbt b, char *str) {
if(b == NULL) {
rbt b = emalloc(sizeof *b);
b->key = emalloc(strlen(str)+1);
strcpy(b->key, str);
b->colour = RED;
b->left = NULL;
b->right = NULL;;
return b;
}
if(strcmp(b->key, str) >= 0) {
b->left = rbt_insert(b->left, str);
}else if(strcmp(b->key, str) < 0) {
b->right = rbt_insert(b->right, str);
}
b = rbt_fix(b);
return b;
}
/*Method that fixes the RBT to ensure that the root of the tree is
always black.
*Param: b, the RBT to fix
*Param: *str, the string to insert into the RBT
*Returns the corrected RBT with a black root
*/
rbt rbt_insert_real(rbt b, char *str) {
b = rbt_insert(b, str);
if(IS_RED(b)) {
b->colour = BLACK;
}
return b;
}
/*Method that traverses the RBT by inorder.
*Param: b, RBT
*Param: f, the function that is passed into this method
*/
void rbt_inorder(rbt b, void f(char *str)) {
if(b == NULL) {
return;
}else{
rbt_inorder(b->left,f);
f(b->key);
rbt_inorder(b->right,f);
}
}
/*Method that traverses the RBT by preorder.
*Param: b, RBT
*Param: f, function that is passed into this method
*/
void rbt_preorder(rbt b, void f(char *s)) {
if(b == NULL) {
return;
}else{
f(b->key);
rbt_preorder(b->left, f);
rbt_preorder(b->right, f);
}
}
/*Method that frees the memory associated with the RBT.
*Param: b, the RBT
*Returns the RBT
*/
rbt rbt_free(rbt b) {
if(b == NULL) {
return b;
}else{
rbt_free(b->left);
free(b->key);
rbt_free(b->right);
free(b);
}
return NULL;
}
/*Method that deletes a specific word in the RBT.
*Param: b, the RBT
*Param: *str, the string to delete
*Returns the new RBT
*/
rbt rbt_delete(rbt b, char *str) {
if(b == NULL) {
return b;
}else if(strcmp(b->key, str) > 0) {
b->left = rbt_delete(b->left, str);
}else if(strcmp(b->key, str) < 0) {
b->right = rbt_delete(b->right, str);
}
if(strcmp(b->key, str) == 0) {
if(b->left == NULL && b->right == NULL) {
free(b->key);
free(b);
b = NULL;
}else if((b->left!= NULL && b->right == NULL) ||
(b->right!= NULL && b->left == NULL)) {
if(b->left != NULL) {
struct rbtnode *temp = b;
b = b->left;
free(temp->key);
free(temp);
}else if(b->right != NULL) {
struct rbtnode *temp = b;
b = b->right;
free(temp->key);
free(temp);
}
}else if(b->left!=NULL && b->right!=NULL) {
rbt tempRBT;
tempRBT = b->right;
while(tempRBT->left != NULL) {
tempRBT = tempRBT->left;
}
strcpy(b->key, tempRBT->key);
b->right = rbt_delete(b->right, tempRBT->key);
}
}
return b;
}