-
Notifications
You must be signed in to change notification settings - Fork 2
/
RBtree.c
239 lines (216 loc) · 5.09 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
#include "RBtree.h"
//tool function
int isRed(Node *T)
{
if(!T)
return 0;
return (T->color==RED) ? 1:0;
}
Node *rotateLeft(Node *T)
{
Node *x = T->rchild;
T->rchild = x->lchild;
x->lchild = T;
x->color = T->color;
T->color = RED;
return x;
}
Node *rotateRight(Node *T)
{
Node *x = T->lchild;
T->lchild = x->rchild;
x->rchild = T;
x->color = T->color;
T->color = RED;
return x;
}
void flipColors(Node *T)
{
T->color = RED;
T->lchild->color = BLACK;
T->rchild->color = BLACK;
}
//-------------------------------------------------------------------
//insert function
void InsertNode(Node **T, char *key, char *data)
{
put(T, key, data);
(*T)->color = BLACK;
}
Node *put(Node **T, char *key, char *data)
{
if(*T == NULL)
{
*T = calloc(1, sizeof(Node));
if(*T == NULL)
return NULL;
else
{
(*T)->key = key;
(*T)->data = data;
(*T)->lchild = NULL;
(*T)->rchild = NULL;
(*T)->color = RED;
}
}
else if(strcmp(key, (*T)->key)<0)
(*T)->lchild = put(&((*T)->lchild), key, data);
else if(strcmp(key, (*T)->key)>0)
(*T)->rchild = put(&((*T)->rchild), key, data);
else if(!strcmp(key, (*T)->key))
(*T)->data = data;
if(isRed((*T)->rchild) && !isRed((*T)->lchild))
*T = rotateLeft(*T);
if(isRed((*T)->lchild) && isRed((*T)->lchild->lchild))
*T = rotateRight(*T);
if(isRed((*T)->lchild) && isRed((*T)->rchild))
flipColors(*T);
return *T;
}
//-------------------------------------------------------------------
//search function
Node *SearchNode(Node *T, char *key, Node *f)
{
if(!T)
return NULL;
else if(!strcmp(key, T->key))
return T;
else if(strcmp(key, T->key)<0)
return SearchNode(T->lchild, key, T);
else
return SearchNode(T->rchild, key, T);
}
//------------------------------------------------------------------
//delete function
Node *moveRedLeft(Node *T)
{
flipColors(T);
if(isRed(T->rchild->lchild))
{
T->rchild = rotateRight(T->rchild);
T = rotateLeft(T);
}
return T;
}
Node *deleteMin(Node *T)
{
if(!T->lchild)
{
if(T->key)
free(T->key);
if(T->data)
free(T->data);
free(T);
return NULL;
}
if(!isRed(T->lchild) && !isRed(T->lchild->lchild))
T = moveRedLeft(T);
T->lchild = deleteMin(T->lchild);
return balance(T);
}
Node *moveRedRight(Node *T)
{
flipColors(T);
if(!isRed(T->lchild->lchild))
T = rotateRight(T);
return T;
}
Node *balance(Node *T)
{
if(isRed(T->rchild))
T = rotateLeft(T);
if(isRed(T->rchild) && !isRed(T->lchild))
T = rotateLeft(T);
if(isRed(T->lchild) && isRed(T->lchild->lchild))
T = rotateRight(T);
if(isRed(T->lchild) && isRed(T->rchild))
flipColors(T);
return T;
}
Node *min(Node *T)
{
if(!T->lchild)
return T;
return min(T->lchild);
}
Node *DeleteNode(Node *T, char *key)
{
if(!isRed(T->lchild) && !isRed(T->rchild))
T->color = RED;
T = p_delete(T, key);
return T;
}
Node *p_delete(Node *T, char *key)
{
if(strcmp(key, T->key)<0)
{
if(!isRed(T->lchild) && !isRed(T->lchild->lchild))
T = moveRedLeft(T);
T->lchild = p_delete(T->lchild, key);
}
else
{
if(isRed(T->lchild))
T = rotateRight(T);
if(!strcmp(key, T->key) && !T->rchild)
return NULL;
if(!isRed(T->rchild) && !isRed(T->rchild->lchild))
T = moveRedRight(T);
if(!strcmp(key, T->key))
{
T->data = SearchNode(T->rchild,
min(T->rchild)->data,
NULL)->data;
T->key = min(T->rchild)->key;
T->rchild = deleteMin(T->rchild);
}
else
T->rchild = p_delete(T->rchild, key);
}
return balance(T);
}
//------------------------------------------------------------------
//test case
/*
int main()
{
char *key1 = "S";
char *key2 = "E";
char *key3 = "A";
char *key4 = "R";
char *key5 = "C";
char *key6 = "H";
char *key7 = "X";
char *key8 = "M";
char *key9 = "P";
char *key10 = "L";
char *data1 = "S";
char *data2 = "E";
char *data3 = "A";
char *data4 = "R";
char *data5 = "C";
char *data6 = "H";
char *data7 = "X";
char *data8 = "M";
char *data9 = "P";
char *data10 = "L";
Node *p = NULL;
Node *s = NULL;
InsertNode(&p, key1, data1);
InsertNode(&p, key2, data2);
InsertNode(&p, key3, data3);
InsertNode(&p, key4, data4);
InsertNode(&p, key5, data5);
InsertNode(&p, key6, data6);
InsertNode(&p, key7, data7);
InsertNode(&p, key8, data8);
InsertNode(&p, key9, data9);
InsertNode(&p, key10, data10);
s = SearchNode(p, "R", NULL);
if(s)
printf("%s12\n", s->data);
DeleteNode(p, "R");
s = SearchNode(p, "L", NULL);
if(s)
printf("%s\n", s->data);
}*/