forked from aed-i-2024-q1/student-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl.c
234 lines (184 loc) · 4.32 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
/**
* Binary Search Tree implementation with AVL balancing.
*/
#include "avl.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct Node {
Element key;
struct Node* left;
struct Node* right;
int height;
} Node;
struct AVL {
Node* root;
};
AVL* avl_create() {
AVL* bst = malloc(sizeof(AVL));
bst->root = NULL;
return bst;
}
void destroyRecur(Node* node) {
if (node == NULL) {
return;
}
if (node->left != NULL) {
destroyRecur(node->left);
}
if (node->right != NULL) {
destroyRecur(node->right);
}
free(node);
}
void avl_destroy(AVL* bst) {
destroyRecur(bst->root);
free(bst);
}
bool searchRecur(Node* node, Element key) {
if (node == NULL) {
return false;
}
if (node->key == key) {
return true;
}
if (key < node->key) {
return searchRecur(node->left, key);
}
return searchRecur(node->right, key);
}
bool avl_search(AVL* bst, Element key) {
return searchRecur(bst->root, key);
}
Node* createNode(Element key) {
Node* newNode = malloc(sizeof(Node));
newNode->key = key;
newNode->left = NULL;
newNode->right = NULL;
newNode->height = 1;
return newNode;
}
int height(Node* node) {
if (node == NULL) {
return 0;
}
return node->height;
}
int balanceFactor(Node* node) {
if (node == NULL) {
return 0;
}
return height(node->left) - height(node->right);
}
int maximum(int a, int b) {
return (a > b) ? a : b;
}
void updateHeight(Node* node) {
if (node != NULL) {
node->height = 1 + maximum(height(node->left), height(node->right));
}
}
Node* rotateRight(Node* node) {
Node* pivot = node->left;
node->left = pivot->right;
pivot->right = node;
updateHeight(node);
updateHeight(pivot);
return pivot;
}
Node* rotateLeft(Node* node) {
Node* pivot = node->right;
node->right = pivot->left;
pivot->left = node;
updateHeight(node);
updateHeight(pivot);
return pivot;
}
Node* rebalance(Node* node) {
updateHeight(node);
if (balanceFactor(node) < - 1) {
if (balanceFactor(node->right) > 0) {
node->right = rotateRight(node->right);
}
return rotateLeft(node);
} else if (balanceFactor(node) > 1) {
if (balanceFactor(node->left) < 0) {
node->left = rotateLeft(node->left);
}
return rotateRight(node);
}
return node;
}
Node* insertRecur(Node* node, Element key) {
if (node == NULL) {
return createNode(key);
}
if (key < node->key) {
node->left = insertRecur(node->left, key);
} else if (key > node->key) {
node->right = insertRecur(node->right, key);
}
return rebalance(node);
}
void avl_insert(AVL* bst, Element key) {
bst->root = insertRecur(bst->root, key);
}
Node* successor(Node* node) {
if (node == NULL) {
return NULL;
}
Node* cur = node->right;
while (cur->left != NULL) {
cur = cur->left;
}
return cur;
}
Node* removeRecur(Node* node, Element key) {
if (node == NULL) {
return node;
}
if (key < node->key) {
node->left = removeRecur(node->left, key);
} else if (key > node->key) {
node->right = removeRecur(node->right, key);
} else {
if (node->left == NULL) {
Node* trash = node;
node = node->right;
free(trash);
} else if (node->right == NULL) {
Node* trash = node;
node = node->left;
free(trash);
} else {
Node* temp = successor(node);
node->key = temp->key;
node->right = removeRecur(node->right, temp->key);
}
}
return rebalance(node);
}
void avl_remove(AVL* bst, Element key) {
bst->root = removeRecur(bst->root, key);
}
void fill_spaces(int size) {
for (int i = 0; i < size; i++) {
printf(" ");
}
}
void printDiagram(Node* node, int level) {
if (node == NULL) {
return;
}
for (int i = 0; i < level; i++) {
printf(" ");
}
element_print(node->key);
printf(",%d,%d ", height(node), balanceFactor(node));
printf("\n");
printDiagram(node->left, level + 1);
printDiagram(node->right, level + 1);
}
void avl_print(AVL* bst) {
printDiagram(bst->root, 0);
}