This repository has been archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl_tree.c
319 lines (270 loc) · 6.21 KB
/
avl_tree.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
#include "avl_tree.h"
#include "generic_vector.h"
#include "stdlib.h"
#include "my_string.h"
typedef struct node Node;
struct node {
GENERIC_VECTOR data;
MY_STRING key;
Node *left;
Node *right;
int BF;
};
int avl_tree_balance(Node *pNode);
Status left_rotation(Node *pNode);
Status right_rotation(Node *pNode);
Node *make_node(MY_STRING new_key, MY_STRING word)
{
Node *pNode = (Node *)malloc(sizeof(Node));
if (pNode == NULL)
{
printf("ERROR: Failed to allocate space\n");
return NULL;
}
pNode->data = generic_vector_init_default(my_string_assignment, my_string_destroy);
if (pNode->data == NULL)
{
free(pNode);
return NULL;
}
if (generic_vector_push_back(pNode->data, word) == FAILURE)
{
free(pNode);
return NULL;
}
// if you want to be awful: pNode->left = pNode->right = NULL;
pNode->left = NULL;
pNode->right = NULL;
pNode->key = new_key;
pNode->BF = 0;
return pNode;
}
AVL_TREE avl_tree_init_default()
{
return NULL;
}
Status avl_tree_insert(AVL_TREE *phTree, MY_STRING new_key, MY_STRING word, int output)
{
Node **ppNode = (Node **)phTree;
Node *pNode = *ppNode;
if (pNode == NULL)
{
*ppNode = make_node(new_key, word);
if(output)
{
printf("%s\n", my_string_c_str(new_key));
}
return *ppNode == NULL ? FAILURE : SUCCESS;
}
int const compare = my_string_compare(new_key, pNode->key);
if (compare < 0)
{
return avl_tree_insert((AVL_TREE *)(&pNode->left), new_key, word, output);
//avl_tree_balance(pNode);
}
else if (compare > 0)
{
return avl_tree_insert((AVL_TREE *)(&pNode->right), new_key, word, output);
//avl_tree_balance(pNode);
}
else
{
//node already exists, add word to the node's values
if (pNode->data == NULL)
{
pNode->data = generic_vector_init_default(my_string_assignment, my_string_destroy);
if (pNode->data == NULL)
{
return FAILURE;
}
}
generic_vector_push_back(pNode->data, word);
//no new node was created, so destroy the key
my_string_destroy(&new_key);
return SUCCESS;
}
}
static void get_largest_node(Node *pNode, int *max_size, Node **max_node)
{
if (pNode == NULL) {
return;
}
int const size = generic_vector_get_size(pNode->data);
if (*max_node == NULL || *max_size < size)
{
*max_size = size;
*max_node = pNode;
}
get_largest_node(pNode->left, max_size, max_node);
get_largest_node(pNode->right, max_size, max_node);
}
AVL_TREE avl_get_largest_node(AVL_TREE hTree) {
Node *pNode = (Node *)hTree;
Node *max_node = NULL;
int max_size;
get_largest_node(pNode, &max_size, &max_node);
return max_node;
}
int my_max(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
int tree_report_height(Node *root)
{
if (root == NULL)
{
return 0;
}
else
{
return 1 + my_max(tree_report_height(root->left), tree_report_height(root->right));
}
}
//needs to find bottom node then travel back up it unsureing everything is balanced
//so count must start at the bottom of recursion
int avl_tree_balance(Node *pRoot)
{
int heightLeft = tree_report_height(pRoot->left);
int heightRight = tree_report_height(pRoot->right);
pRoot->BF = heightRight - heightLeft;
if (pRoot->BF < -1)
{
right_rotation(pRoot);
}
else if (pRoot->BF > 1)
{
left_rotation(pRoot);
}
return 0;
}
Status left_rotation(Node *pNode)
{
if (pNode->right != NULL)
{
if (pNode->right->BF < 0)
{
right_rotation(pNode->right);
}
}
Status status = FAILURE;
Node *temp1, *temp2, *orphan = NULL;
temp1 = (Node *)malloc(sizeof(Node));
if (temp1 != NULL)
{
//make a temp
status = SUCCESS;
temp1->BF = 0;
temp1->data = pNode->data;
temp1->left = pNode->left;
temp1->right = pNode->right;
temp1->key = pNode->key;
//assign orphan if it exsits & cut it from tree
if (pNode->right != NULL && pNode->right->left != NULL)
{
orphan = pNode->right->left;
pNode->right->left = NULL;
}
//set right child as new parent
temp2 = pNode->right;
if (temp2 != NULL)
{
pNode->data = pNode->right->data;
pNode->key = pNode->right->key;
pNode->right = pNode->right->right;
free(temp2);
}
pNode->left = temp1;
//re-assign orphan to the former parent
temp1->right = orphan;
//get new balance factor
pNode->BF = 0;//fix
}
return status;
}
Status right_rotation(Node *pNode)
{
if (pNode->left != NULL)
{
if (pNode->left->BF > 0)
{
left_rotation(pNode->left);
}
}
Status status = FAILURE;
Node *temp1, *temp2, *orphan = NULL;
temp1 = (Node *)malloc(sizeof(Node));
if (temp1 != NULL)
{
//make a temp
status = SUCCESS;
temp1->BF = 0;
temp1->data = pNode->data;
temp1->left = pNode->left;
temp1->right = pNode->right;
temp1->key = pNode->key;
//assign orphan if it exsits & cut it from tree
if (pNode->left != NULL && pNode->left->right != NULL)
{
orphan = pNode->left->right;
pNode->left->right = NULL;
}
//set left child as new parent
temp2 = pNode->left;
if (temp2 != NULL)
{
pNode->data = pNode->left->data;
pNode->key = pNode->left->key;
pNode->left = pNode->left->left;
pNode->right = temp1;
free(temp2);
}
//re-assign orphan to the former parent
temp1->left = orphan;
//get new balance factor
pNode->BF = 0;//fix
}
return status;
}
GENERIC_VECTOR avl_tree_swap_destroy(MY_STRING *current_word_family, AVL_TREE key_closet, AVL_TREE next_vector)
{
int i;
Node *pTree = (Node *)next_vector;
MY_STRING temp;
GENERIC_VECTOR newVector = generic_vector_init_default(my_string_assignment, my_string_destroy);
MY_STRING new_key = my_string_init_default();
my_string_assignment(current_word_family, pTree->key);
for (i = 0; i < generic_vector_get_size(pTree->data); i++)
{
generic_vector_push_back(newVector, *(generic_vector_at(pTree->data, i)));
}
avl_tree_destroy(key_closet);
return newVector;
}
void avl_get_key_take_value(AVL_TREE hTree, MY_STRING *phKey, GENERIC_VECTOR *phVec)
{
Node *pNode = (Node *)hTree;
my_string_assignment(phKey, pNode->key);
*phVec = pNode->data;
pNode->data = NULL;
}
static void destroy_nodes(Node *pNode) {
if (pNode == NULL)
{
return;
}
destroy_nodes(pNode->left);
destroy_nodes(pNode->right);
generic_vector_destroy(&(pNode->data));
my_string_destroy(&(pNode->key));
free(pNode);
}
void avl_tree_destroy(AVL_TREE *phTree)
{
Node *pNode = (Node *)*phTree;
destroy_nodes(pNode);
*phTree = NULL;
}