-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl_any.c
377 lines (316 loc) · 7.93 KB
/
avl_any.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
//===========================================================================
// FILE: avl_any.c
//===========================================================================
// Implementation file for AVL Tree struct
// Original Author: David Smallwood
// Modifications:
// - Added avl_any_find() function.
// - Added avl_any_inorder_map() function.
// - Use SAFE_MALLOC(), SAFE_MALLOC_EXISTING() & SAFE_FREE()
// - Ensured max line length of 80 to suit assignment criteria :p
//---------------------------------------------------------------------------
//#define TRACE_ON
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "avl_any.h"
#include "trace.h"
#include "smalloc.h"
typedef struct node
{
char height;
any item;
struct node * left;
struct node * right;
} node_t;
struct avl_any_implementation
{
struct node * root;
int size;
rel_func lt;
};
avl_any * new_avl_any(rel_func lt)
{
SAFE_MALLOC(avl_any,t);
t->root = NULL;
t->size = 0;
t->lt = lt;
return t;
}
#define LT(A,B) t->lt(A,B)
#define ABS(X) ((X<0) ? -(X) : X)
#define MAX(X,Y) ((X>Y) ? X : Y)
#define HEIGHT(P) ((P == NULL) ? 0 : P->height)
int avl_any_isempty(avl_any *t)
{
assert(t!=NULL);
return (t->size == 0);
}
int avl_any_isfull(avl_any *t)
{
return 0;
}
int avl_any_size(avl_any *t)
{
assert(t!=NULL);
return t->size;
}
int get_depth(struct node *p)
{
int l,r;
if (p==NULL)
return 0;
else {
l = 1 + get_depth(p->left);
r = 1 + get_depth(p->right);
return (l>r) ? l : r;
}
}
int avl_any_depth(avl_any *t)
{
assert(t!=NULL);
return get_depth(t->root);
}
struct node* rotate_left(struct node *p)
{
struct node *q;
assert(p!=NULL);
assert(p->right!=NULL);
q = p->right;
p->right = q->left;
q->left = p;
p->height = 1 + MAX(HEIGHT(p->left),HEIGHT(p->right));
q->height = 1 + MAX(HEIGHT(q->left),HEIGHT(q->right));
return q;
}
struct node* rotate_right(struct node *p)
{
struct node *q;
assert(p!=NULL);
assert(p->left!=NULL);
q = p->left;
p->left = q->right;
q->right = p;
p->height = 1 + MAX(HEIGHT(p->left),HEIGHT(p->right));
q->height = 1 + MAX(HEIGHT(q->left),HEIGHT(q->right));
return q;
}
struct node* ensure_balance(struct node *p)
{
// trivially balanced - return now
if (p==NULL)
return p;
p->height = 1 + MAX(HEIGHT(p->left),HEIGHT(p->right));
// left-heavy - rebalance needed
if ( HEIGHT(p->left) - HEIGHT(p->right) > 1)
{
if (HEIGHT(p->left->left) > HEIGHT(p->left->right))
p = rotate_right(p);
else {
p->left = rotate_left(p->left);
p = rotate_right(p);
}
}
// right-heavy - rebalance needed
else if ( HEIGHT(p->right) - HEIGHT(p->left) > 1)
{
if (HEIGHT(p->right->right) > HEIGHT(p->right->left))
p = rotate_left(p);
else {
p->right = rotate_right(p->right);
p = rotate_left(p);
}
}
else// balanced - no rebalance needed
;
return p;
}
struct node * ins(avl_any *t, any x, struct node *p)
{
if (p==NULL)
{
SAFE_MALLOC_EXISTING(node_t,p);
p->left = p->right = NULL;
p->item = x;
(t->size)++;
}
// inserting into left sub-tree
else if (LT(x,p->item)) {
p->left = ins(t, x, p->left);
}
// inserting into right sub-tree
else if (LT(p->item,x)) {
p->right = ins(t, x, p->right);
}
return ensure_balance(p);
}
void avl_any_insert(avl_any *t, any x)
{
assert(t!=NULL);
t->root = ins(t, x, t->root);
}
struct node * findmin(struct node *p)
{
if (p==NULL)
printf("error: (avl_any) findmin: empty tree\n");
else if (p->left == NULL)
return p;
else
return findmin(p->left);
}
struct node * del(avl_any *t, any x, struct node *p)
{
struct node * n;
// do nothing - data not in tree
if (p == NULL)
;
else if (LT(x,p->item))
p->left = del(t, x, p->left);
else if (LT(p->item,x))
p->right = del(t, x, p->right);
// found and has two children
// get smallest in right tree
// overwrite the item to be deleted
// del the duplicate item in right tree
else if (p->left!=NULL && p->right!=NULL)
{
n = findmin(p->right);
p->item = n->item;
p->right = del(t, p->item, p->right);
}
// found and has at most one child
else {
n = p;
if(p->left == NULL)
// promote the right tree
p = p->right;
else if (p->right == NULL)
// promote the left tree
p = p->left;
(t->size)--; // reduce the count
SAFE_FREE(n); // reclaim memory
}
return ensure_balance(p);
}
void avl_any_delete(avl_any *t, any x)
{
assert(t!=NULL);
t->root = del(t, x, t->root);
}
int isin(avl_any *t, any x, struct node *p)
{
if (p==NULL)
return 0;
else if (LT(x,p->item))
return isin(t, x, p->left);
else if (LT(p->item,x))
return isin(t, x, p->right);
else
return 1;
}
int avl_any_contains(avl_any *t, any x)
{
assert(t!=NULL);
return isin(t,x,t->root);
}
void preorder(struct node *p, void (* item_print)(any item))
{
if (p!=NULL) {
item_print(p->item);
preorder(p->left,item_print);
preorder(p->right,item_print);
}
}
void inorder(struct node *p, void (* item_print)(any item))
{
if (p!=NULL) {
inorder(p->left,item_print);
item_print(p->item);
inorder(p->right,item_print);
}
}
void postorder(struct node *p, void (* item_print)(any item))
{
if (p!=NULL) {
postorder(p->left,item_print);
postorder(p->right,item_print);
item_print(p->item);
}
}
void avl_any_preorder_print(avl_any *t, void (* item_print)(any item))
{
assert(t!=NULL);
preorder(t->root, item_print);
}
void avl_any_inorder_print(avl_any *t, void (* item_print)(any item))
{
assert(t!=NULL);
inorder(t->root, item_print);
}
void avl_any_postorder_print(avl_any *t, void (* item_print)(any item))
{
assert(t!=NULL);
postorder(t->root, item_print);
}
void simple_print(struct node *p, int ind, void (* item_print)(any item))
{
int i;
if (p!=NULL) {
simple_print(p->right,ind+4,item_print);
for(i=0;i<ind;i++) putchar(' ');
item_print(p->item);
printf(" (h=%i).... %p", ((int)(p->height)), p);
putchar('\n');
simple_print(p->left,ind+4,item_print);
}
else {
for(i=0;i<ind;i++) putchar(' ');
printf("%p\n", p);
}
}
void avl_any_simple_print(avl_any *t, void (* item_print)(any item))
{
assert(t!=NULL);
simple_print(t->root,0,item_print);
}
any findin (avl_any *t, any x, struct node *p)
{
if (p==NULL)
return 0;
else if (LT(x,p->item))
return findin(t, x, p->left);
else if (LT(p->item,x))
return findin(t, x, p->right);
else
return p->item;
}
any avl_any_find (avl_any *t, any x)
{
assert(t!=NULL);
return findin (t,x,t->root);
}
void inorder_map(struct node *p, void (* item_process)(any item, any context), any context)
{
// do not assert p, will be null for leaf nodes
assert(item_process != NULL);
// do not assert context - NULL may be valid
if (p != NULL)
{
inorder_map(p->left, item_process, context);
item_process (p->item, context);
inorder_map(p->right, item_process, context);
}
}
void avl_any_inorder_map(avl_any *t, void (* item_process)(any item, any context), any context)
{
assert(t!=NULL);
assert(item_process != NULL);
// do not assert context - NULL may be valid
inorder_map(t->root, item_process, context);
}
void avl_any_release(avl_any *t)
{
assert(t != NULL);
SAFE_FREE(t);
}