-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl.c
213 lines (184 loc) · 4.09 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
#include "Tree.h"
#include <stdlib.h>
#include <stdio.h>
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define ABS(a) (((a) >= 0) ? (a) : (-(a)))
typedef struct avl
{
Item v;
struct avl *left;
struct avl *right;
int height;
} *AVL;
struct tree
{
AVL t;
int (*compare)(Item a, Item b);
};
int height(AVL t);
AVL AVLNew(Item x);
AVL AVLInsert(AVL t, Item x, int (*compare)(Item a, Item b));
AVL AVLDelete(AVL t, Item x, int (*compare)(Item a, Item b));
AVL rotateLeft(AVL t);
AVL rotateRight(AVL t);
void AVLFree(AVL t);
bool AVLMember(AVL t, Item v, int (*compare)(Item a, Item b));
void AVLFree(AVL t)
{
if (t)
{
AVLFree(t->left);
AVLFree(t->right);
free(t);
}
}
Tree TreeNew(int (*compare)(Item a, Item b))
{
Tree res = malloc(sizeof *res);
res->t = NULL;
res->compare = compare;
return res;
}
AVL AVLNew(Item x)
{
AVL res = malloc(sizeof *res);
res->v = x;
res->left = NULL;
res->right = NULL;
res->height = 0;
return res;
}
AVL rotateLeft(AVL t)
{
AVL res = t->right;
int ah = height(t->left);
int bh = height(res->left);
int zh = height(res->right);
t->right = res->left;
res->left = t;
t->height = MAX(ah, bh) + 1;
res->height = MAX(t->height, zh) + 1;
return res;
}
AVL rotateRight(AVL t)
{
AVL res = t->left;
int bh = height(res->right);
int zh = height(t->right);
int ah = height(res->left);
t->left = res->right;
res->right = t;
t->height = MAX(bh, zh) + 1;
res->height = MAX(ah, t->height) + 1;
return res;
}
int height(AVL t)
{
if (t)
return t->height;
else
return -1;
}
AVL AVLInsert(AVL t, Item x, int (*compare)(Item a, Item b))
{
if (t)
{
int cmp = compare(x, t->v);
if (cmp == 0)
{
return t;
}
else if (cmp < 0)
{
int cl = (t->left) ? (t->left->v) : 0;
t->left = AVLInsert(t->left, x, compare);
t->height = MAX(height(t->left), height(t->right)) + 1;
if (height(t->left) - height(t->right) > 1)
{
if (compare(cl, x) < 0)
t->left = rotateLeft(t->left);
return rotateRight(t);
}
return t;
}
else
{
int cl = (t->right) ? (t->right->v) : 0;
t->right = AVLInsert(t->right, x, compare);
t->height = MAX(height(t->left), height(t->right)) + 1;
if (height(t->right) - height(t->left) > 1)
{
if (compare(cl, x) > 0)
t->right = rotateRight(t->right);
return rotateLeft(t);
}
return t;
}
}
else
{
return AVLNew(x);
}
}
AVL AVLDelete(AVL t, Item x, int (*compare)(Item a, Item b))
{
return t;
}
void AVLFree(AVL t);
bool AVLMember(AVL t, Item v, int (*compare)(Item a, Item b));
void TreeFree(Tree t);
bool TreeMember(Tree t, Item v);
void TreeInsert(Tree t, Item v);
void TreeDelete(Tree t, Item v);
void printInOrder(AVL t)
{
if (t)
{
printInOrder(t->left);
printf(" %f", t->v);
printInOrder(t->right);
}
}
int cmpo(float a, float b)
{
if (a > b)
return 1;
else if (a == b)
return 0;
else
return -1;
}
void printLevel(AVL t, int l)
{
if (t)
{
printLevel(t->right, l + 1);
for (int i = 0; i < l; i++)
putchar('\t');
printf("%f\n", t->v);
printLevel(t->left, l + 1);
}
}
int main(void)
{
AVL x = AVLNew(6);
x = AVLInsert(x, 7, cmpo);
x = AVLInsert(x, 4, cmpo);
x = AVLInsert(x, 2, cmpo);
x = AVLInsert(x, 5, cmpo);
printLevel(x, 0);
printf("-------\n");
x = AVLInsert(x, 1, cmpo);
printLevel(x, 0);
printf("-------\n");
x = AVLInsert(x, 8, cmpo);
x = AVLInsert(x, 0, cmpo);
printLevel(x, 0);
printf("-------\n");
x = AVLInsert(x, 7.5, cmpo);
printLevel(x, 0);
printf("-------\n");
x = AVLInsert(x, 7, cmpo);
printLevel(x, 0);
printf("-------\n");
}