-
Notifications
You must be signed in to change notification settings - Fork 1
/
avlbst.c
370 lines (318 loc) · 7.81 KB
/
avlbst.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include"avlbst.h"
#include<stdlib.h>
#include<string.h>
#include<errno.h>
// Create a new node which has the specified `key' and `userdata'
// Return NULL if failed
static avlbst_p avlbst_new_node(const size_t key, void *userdata)
{
avlbst_p n = calloc(1, sizeof *n);
if(!n) return n;
n->key = key;
n->userdata = userdata;
return n;
}
// Get the maximum value of two integers
static int avlbst_max(const int a, const int b)
{
return (a > b) ? a : b;
}
// Get the stored height value of the node
static int avlbst_height(avlbst_p n)
{
if(n) return n->height;
else return 0;
}
// Perform left rotation to a node
static avlbst_p avlbst_rol(avlbst_p x)
{
avlbst_p y = x->r_child;
avlbst_p T2 = y->l_child;
y->l_child = x;
x->r_child = T2;
x->height = avlbst_max(avlbst_height(x->l_child), avlbst_height(x->r_child)) + 1;
y->height = avlbst_max(avlbst_height(y->l_child), avlbst_height(y->r_child)) + 1;
return y;
}
// Perform right rotation to a node.
static avlbst_p avlbst_ror(avlbst_p y)
{
avlbst_p x = y->l_child;
avlbst_p T2 = x->r_child;
x->r_child = y;
y->l_child = T2;
y->height = avlbst_max(avlbst_height(y->l_child), avlbst_height(y->r_child)) + 1;
x->height = avlbst_max(avlbst_height(x->l_child), avlbst_height(x->r_child)) + 1;
return x;
}
// Get the balance factor of the node
static int avlbst_get_balance(avlbst_p n)
{
if(n) return avlbst_height(n->l_child) - avlbst_height(n->r_child);
else return 0;
}
// Call this function on insert to keep the tree balanced
static avlbst_p avlbst_keep_balance_ins(avlbst_p n, size_t key)
{
int balance = avlbst_get_balance(n);
// If this node becomes unbalanced, then
// there are 4 cases
// Left Left Case
if (balance > 1 && key < n->l_child->key)
return avlbst_ror(n);
// Right Right Case
if (balance < -1 && key > n->r_child->key)
return avlbst_rol(n);
// Left Right Case
if (balance > 1 && key > n->l_child->key)
{
n->l_child = avlbst_rol(n->l_child);
return avlbst_ror(n);
}
// Right Left Case
if (balance < -1 && key < n->r_child->key)
{
n->r_child = avlbst_ror(n->r_child);
return avlbst_rol(n);
}
return n;
}
// Insert data to the tree
static avlbst_p avlbst_insert_recursive(avlbst_p n, size_t key, void *userdata)
{
if(!n)
{
avlbst_p nn = avlbst_new_node(key, userdata);
if(!nn) goto GenErrExit;
return nn;
}
if(key < n->key)
n->l_child = avlbst_insert_recursive(n->l_child, key, userdata);
else if(key > n->key)
n->r_child = avlbst_insert_recursive(n->r_child, key, userdata);
else
return n;
n->height = avlbst_max(avlbst_height(n->l_child), avlbst_height(n->r_child)) + 1;
return avlbst_keep_balance_ins(n, key);
GenErrExit:
goto FailExit;
FailExit:
return NULL;
}
// The API of avlbst insertion
int avlbst_insert(avlbst_p *ppavlbst, size_t key, void *userdata)
{
avlbst_p n;
if(!ppavlbst) goto InvalidParamExit;
n = *ppavlbst;
n = avlbst_insert_recursive(n, key, userdata);
if(!n) goto GenErrExit;
*ppavlbst = n;
return 1;
InvalidParamExit:
errno = EINVAL;
goto FailExit;
GenErrExit:
goto FailExit;
FailExit:
return 0;
}
// The API of avlbst search
int avlbst_search(avlbst_p pavlbst, size_t key, avlbst_p *ppmatch)
{
avlbst_p n;
if(!pavlbst) goto NotFound;
n = pavlbst;
do
{
if(n->key == key)
{
if(ppmatch) *ppmatch = n;
return 1;
}
else if(key < n->key) n = n->l_child;
else n = n->r_child;
}while(n);
NotFound:
if(ppmatch) *ppmatch = NULL;
return 0;
}
// Find a node in a tree that has the maximum key value
size_t avlbst_find_max_key(avlbst_p pavlbst, avlbst_p *ppn)
{
avlbst_p n = pavlbst;
if(!n)
{
if(ppn) *ppn = NULL;
return 0;
}
while(n->r_child) n = n->r_child;
if(ppn) *ppn = n;
return n->key;
}
// Find a node in a tree that has the minimum key value
size_t avlbst_find_min_key(avlbst_p pavlbst, avlbst_p *ppn)
{
avlbst_p n = pavlbst;
if(!n)
{
if(ppn) *ppn = NULL;
return 0;
}
while(n->l_child) n = n->l_child;
if(ppn) *ppn = n;
return n->key;
}
// Call this function on remove to keep the tree balanced
static avlbst_p avlbst_keep_balance_rem(avlbst_p n)
{
int balance = avlbst_get_balance(n);
// If this node becomes unbalanced, then
// there are 4 cases
// Left Left Case
if (balance > 1 && avlbst_get_balance(n->l_child) >= 0)
return avlbst_ror(n);
// Left Right Case
if (balance > 1 && avlbst_get_balance(n->l_child) < 0)
{
n->l_child = avlbst_rol(n->l_child);
return avlbst_ror(n);
}
// Right Right Case
if (balance < -1 && avlbst_get_balance(n->r_child) <= 0)
return avlbst_rol(n);
// Right Left Case
if (balance < -1 && avlbst_get_balance(n->r_child) > 0)
{
n->r_child = avlbst_ror(n->r_child);
return avlbst_rol(n);
}
return n;
}
// Remove data from the tree
static avlbst_p avlbst_remove_recursive(avlbst_p r, size_t key, void(*on_free)(void *userdata))
{
if(!r) return r;
if(key < r->key) r->l_child = avlbst_remove_recursive(r->l_child, key, on_free);
else if(key > r->key) r->r_child = avlbst_remove_recursive(r->r_child, key, on_free);
else
{
avlbst_p temp;
if((!r->l_child) || (!r->r_child))
{
temp = r->l_child ? r->l_child : r->r_child;
if(!temp)
{
temp = r;
r = NULL;
if(on_free) on_free(temp->userdata);
}
else
{
if(on_free) on_free(r->userdata);
*r = *temp;
}
free(temp);
}
else
{
avlbst_find_min_key(r->r_child, &temp);
r->key = temp->key;
r->userdata = temp->userdata;
r->r_child = avlbst_remove_recursive(r->r_child, temp->key, on_free);
}
}
if(!r) return r;
r->height = avlbst_max(avlbst_height(r->l_child), avlbst_height(r->r_child)) + 1;
return avlbst_keep_balance_rem(r);
}
// The API of avlbst remove
int avlbst_remove(avlbst_p *ppavlbst, size_t key, void(*on_free)(void *userdata))
{
if(!ppavlbst) goto InvalidParamExit;
*ppavlbst = avlbst_remove_recursive(*ppavlbst, key, on_free);
return 1;
InvalidParamExit:
errno = EINVAL;
goto FailExit;
FailExit:
return 0;
}
// Free a node and its child nodes
static void avlbst_free_recursive(avlbst_p n, void(*on_free)(void *userdata))
{
if(!n) return;
avlbst_free_recursive(n->l_child, on_free);
avlbst_free_recursive(n->r_child, on_free);
if(on_free) on_free(n->userdata);
free(n);
}
// The API to free an avlbst
void avlbst_free(avlbst_p *ppavlbst, void(*on_free)(void *userdata))
{
if(ppavlbst)
{
avlbst_free_recursive(*ppavlbst, on_free);
*ppavlbst = NULL;
}
}
// The API to clone an avlbst
avlbst_p avlbst_clone(avlbst_p pavlbst)
{
avlbst_p n;
if(!pavlbst) return pavlbst;
// Clone the node itself
n = avlbst_new_node(pavlbst->key, pavlbst->userdata);
if(!n) return n;
n->height = pavlbst->height;
// Then clone its childs recursively
// If any of the childs could not be cloned, the whole recursived function fails as documented behavior
if(pavlbst->l_child)
{
n->l_child = avlbst_clone(pavlbst->l_child);
if(!n->l_child)
{
free(n);
return NULL;
}
}
if(pavlbst->r_child)
{
n->r_child = avlbst_clone(pavlbst->r_child);
if(!n->r_child)
{
avlbst_free(&n, NULL);
return NULL;
}
}
return n;
}
// Move the store location in the memory of the nodes to perform defragment
// Move them to a lower address by calling malloc() to get a new location,
// and check if the location is at a lower address, then copy the contents of the node.
//
size_t avlbst_defrag(avlbst_p *ppavlbst)
{
size_t sum = 0;
avlbst_p oldptr, newptr;
if(!ppavlbst) return 0;
oldptr = *ppavlbst;
if(!oldptr) return 0;
newptr = malloc(sizeof *newptr);
if (!newptr) return 0; // No more free memory, do nothing at this point.
if((size_t)newptr < (size_t)oldptr)
{
memcpy(newptr, oldptr, sizeof *newptr);
*ppavlbst = newptr;
free(oldptr);
sum = 1;
}
else
{
free(newptr);
newptr = oldptr;
}
sum += avlbst_defrag(&newptr->l_child);
sum += avlbst_defrag(&newptr->r_child);
return sum;
}