-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl.c
393 lines (340 loc) · 11.7 KB
/
avl.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
// Created by hrl on 9/9/15.
//
// AVL function lib
#include <stddef.h>
#include <stdlib.h>
#include "avl_functions.h"
#include "avl_structs.h"
#include "avl_defines.h"
// NULL: empty tree
int _max(int a, int b){
return a>b?a:b;
}
int avl_init(Tree **self, void *data) {
/*
* init an empty AVL Tree
* *self must point to NULL
* (*self)->data need to be set immediately after call this function
*
* */
if(*self != NULL){
return TREE_INITED_ERROR;
}
*self = (Tree*)malloc(sizeof(Tree));
if(*self == NULL){
return TREE_INIT_FAIL_ERROR;
}
(*self)->data = data;
(*self)->height = 1;
(*self)->left = NULL;
(*self)->right = NULL;
return TREE_OP_SUCCESS;
}
int avl_del(Tree **self){
/*
* recursively delete(free) an AVL Tree
*
* */
if((*self) == NULL){
return TREE_OP_SUCCESS;
}
avl_del(&((*self)->left));
avl_del(&((*self)->right));
free(*self);
*self = NULL;
return TREE_OP_SUCCESS;
}
int avl_height_direct(Tree *self){
if(self == NULL){
return 0;
}
return self->height;
}
int avl_search(Tree *self, void *data, void **result_data, int *result_found, int (*compar)(const void *, const void *)){
/*
* Search data in AVL Tree `self`
* data in param and data in AVL struct will be compared in function compar
*
* if found, *result_data will be set to the target data
* result_found will be set to TRUE
* if not, result_found will be set to FALSE
*
* */
if(self == NULL){
return TREE_UNINIT_ERROR;
}
int search_result=(*compar)(data, self->data);
if(search_result == 0){
*result_data = self->data;
*result_found = 1;
} else if (search_result > 0 && self->right != NULL) {
return avl_search(self->right, data, result_data, result_found, compar);
} else if (search_result < 0 && self->left != NULL) {
return avl_search(self->left, data, result_data, result_found, compar);
} else {
*result_data = NULL;
*result_found = 0;
}
return TREE_OP_SUCCESS;
}
int _avl_single_rotate_with_left(Tree **self){
/*
* inner function to rotate AVL Tree when insert/delete member in AVL Tree
* */
Tree *tmp;
tmp = (*self)->left;
(*self)->left = tmp->right;
tmp->right = *self;
*self = tmp;
tmp = (*self)->right;
tmp->height = _max(avl_height_direct(tmp->left), avl_height_direct(tmp->right)) + 1;
(*self)->height = _max(avl_height_direct((*self)->left), avl_height_direct((*self)->right)) + 1;
return TREE_OP_SUCCESS;
}
int _avl_single_rotate_with_right(Tree **self){
Tree *tmp;
tmp = (*self)->right;
(*self)->right = tmp->left;
tmp->left = *self;
*self = tmp;
tmp = (*self)->left;
tmp->height = _max(avl_height_direct(tmp->left), avl_height_direct(tmp->right)) + 1;
(*self)->height = _max(avl_height_direct((*self)->left), avl_height_direct((*self)->right)) + 1;
return TREE_OP_SUCCESS;
}
int _avl_double_rotate_with_left(Tree **self){
int result=TREE_OP_SUCCESS;
result = _avl_single_rotate_with_right(&((*self)->left));
if(result != TREE_OP_SUCCESS)return result;
return _avl_single_rotate_with_left(self);
}
int _avl_double_rotate_with_right(Tree **self){
int result;
result = _avl_single_rotate_with_left(&((*self)->right));
if(result != TREE_OP_SUCCESS)return result;
return _avl_single_rotate_with_right(self);
}
int avl_insert(Tree **self, void *data, int (*compar)(const void *, const void *)){
/*
* Insert data into AVL Tree `self`
* data in param and data in AVL struct will be compared in function compar
*
*/
if(*self == NULL){
return avl_init(self, data);
}
int result=TREE_OP_SUCCESS;
int compar_result=(*compar)(data, (*self)->data);
if(compar_result == 0) {
return TREE_INSERT_SAME_VALUE_ERROR;
}
if(compar_result < 0){
if((*self)->left == NULL){
// left child is empty, just init an AVL Tree
result = avl_init(&((*self)->left), data);
if(result != TREE_OP_SUCCESS)return TREE_INSERT_FAIL_ERROR;
} else {
// recursively insert into left child
result = avl_insert(&((*self)->left), data, compar);
if(result != TREE_OP_SUCCESS)return result;
// rotate AVL Tree
if(avl_height_direct((*self)->left) - avl_height_direct((*self)->right) == 2){
if((*compar)(data, (*self)->left->data) < 0){
result = _avl_single_rotate_with_left(self);
} else {
result = _avl_double_rotate_with_left(self);
}
if(result != TREE_OP_SUCCESS)return result;
}
}
} else {
if((*self)->right == NULL){
result = avl_init(&((*self)->right), data);
if(result != TREE_OP_SUCCESS)return result;
} else {
result = avl_insert(&((*self)->right), data, compar);
if(result != TREE_OP_SUCCESS)return result;
if(avl_height_direct((*self)->right) - avl_height_direct((*self)->left) == 2){
if((*compar)(data, (*self)->right->data) > 0){
result = _avl_single_rotate_with_right(self);
} else {
result = _avl_double_rotate_with_right(self);
}
if(result != TREE_OP_SUCCESS)return result;
}
}
}
// update height according to sub tree
(*self)->height = _max(avl_height_direct((*self)->left), avl_height_direct((*self)->right)) + 1;
return TREE_OP_SUCCESS;
}
int _avl_rotate(Tree **self){
// choose rotate type according to AVL Tree's height
if(avl_height_direct((*self)->left) - avl_height_direct((*self)->right) == 2){
if(avl_height_direct((*self)->left->left) > avl_height_direct((*self)->left->right)){
return _avl_single_rotate_with_left(self);
}
return _avl_double_rotate_with_left(self);
}
if(avl_height_direct((*self)->right) - avl_height_direct((*self)->left) == 2){
if(avl_height_direct((*self)->right->right) > avl_height_direct((*self)->right->left)){
return _avl_single_rotate_with_right(self);
}
return _avl_double_rotate_with_right(self);
}
return TREE_OP_SUCCESS;
}
int avl_delete(Tree **self, void *data, int *deleted, int (*compar)(const void *, const void *)){
/*
* Delete data from AVL Tree `self`
* data in param and data in AVL struct will be compared in function compar
*
* if success found and deleted the target data, deleted will be set to TRUE
* if not, deleted will be set to FALSE
*
*/
if(*self == NULL){
return TREE_UNINIT_ERROR;
}
int result=TREE_OP_SUCCESS;
int compar_result=(*compar)(data, (*self)->data);
if(compar_result == 0){
// delete current Node
if((*self)->right == NULL){
Tree *need_free;
need_free = *self;
*self = (*self)->left;
free(need_free);
if(*self == NULL){
return TREE_OP_SUCCESS;
}
} else {
// find the minimum child in right tree
Tree *left_iterator;
left_iterator = (*self)->right;
while(left_iterator->left != NULL){
left_iterator = left_iterator->left;
}
// set current node's data to the minimum data in right tree
(*self)->data = left_iterator->data;
// then delete the minimum child in right tree
result = avl_delete(&((*self)->right), (*self)->data, deleted, compar);
if(result != TREE_OP_SUCCESS)return result;
(*self)->height = _max(avl_height_direct((*self)->left), avl_height_direct((*self)->right)) + 1;
}
} else if(compar_result < 0){
if((*self)->left == NULL){
*deleted = 0;
return TREE_OP_SUCCESS;
}
result = avl_delete(&((*self)->left), data, deleted, compar);
if(result != TREE_OP_SUCCESS)return result;
} else {
if((*self)->right == NULL){
*deleted = 0;
return TREE_OP_SUCCESS;
}
result = avl_delete(&((*self)->right), data, deleted, compar);
if(result != TREE_OP_SUCCESS)return result;
}
// re balance the AVL Tree
if((*self)->left != NULL){
result = _avl_rotate(&((*self)->left));
if(result != TREE_OP_SUCCESS)return result;
}
if((*self)->right != NULL){
result = _avl_rotate(&((*self)->right));
if(result != TREE_OP_SUCCESS)return result;
}
if(*self != NULL){
result = _avl_rotate(self);
if(result != TREE_OP_SUCCESS)return result;
}
// update AVL Tree's height
(*self)->height = _max(avl_height_direct((*self)->left), avl_height_direct((*self)->right)) + 1;
return TREE_OP_SUCCESS;
}
int avl_pre_order_traversal(Tree *self, void *pipe, int (*callback)(const void *, void *)){
/*
* traversal whole AVL Tree in pre order
* apply callback function to each Node's data and the input param pipe during traversal
*
* if the callback function's return value is not `TREE_OP_SUCCESS`, traversal will be aborted
*
* */
if(self == NULL){
return TREE_UNINIT_ERROR;
}
int result=TREE_OP_SUCCESS;
result = (*callback)(self->data, pipe);
if(result != TREE_OP_SUCCESS)return result;
if(self->left != NULL){
result = avl_pre_order_traversal(self->left, callback, pipe);
if(result != TREE_OP_SUCCESS)return result;
}
if(self->right != NULL){
result = avl_pre_order_traversal(self->right, callback, pipe);
if(result != TREE_OP_SUCCESS)return result;
}
return TREE_OP_SUCCESS;
}
int avl_in_order_traversal(Tree *self, void *pipe, int (*callback)(const void *, void *)){
if(self == NULL){
return TREE_UNINIT_ERROR;
}
int result=TREE_OP_SUCCESS;
if(self->left != NULL){
result = avl_in_order_traversal(self->left, callback, pipe);
if(result != TREE_OP_SUCCESS)return result;
}
result = (*callback)(self->data, pipe);
if(result != TREE_OP_SUCCESS)return result;
if(self->right != NULL){
result = avl_in_order_traversal(self->right, callback, pipe);
if(result != TREE_OP_SUCCESS)return result;
}
return TREE_OP_SUCCESS;
}
int avl_post_order_traversal(Tree *self, void *pipe, int (*callback)(const void *, void *)){
if(self == NULL){
return TREE_UNINIT_ERROR;
}
int result=TREE_OP_SUCCESS;
if(self->left != NULL){
result = avl_post_order_traversal(self->left, callback, pipe);
if(result != TREE_OP_SUCCESS)return result;
}
if(self->right != NULL){
result = avl_post_order_traversal(self->right, callback, pipe);
if(result != TREE_OP_SUCCESS)return result;
}
result = (*callback)(self->data, pipe);
if(result != TREE_OP_SUCCESS)return result;
return TREE_OP_SUCCESS;
}
int avl_level_order_traversal(Tree *self, void *pipe, int (*callback)(const void *, void *)){
if(self == NULL){
return TREE_OP_SUCCESS;
}
int result=TREE_OP_SUCCESS;
Tree* queue[TREE_MAX_SIZE];
int first=0;
int last=0;
queue[first] = self;
first++;
while(first != last){
result = (*callback)(queue[last]->data, pipe);
if(result != TREE_OP_SUCCESS)return result;
if(queue[last]->left != NULL){
queue[first] = queue[last]->left;
first++;
}
if(queue[last]->right != NULL){
queue[first] = queue[last]->right;
first++;
}
last++;
}
return TREE_OP_SUCCESS;
}