-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyAVLTree.c
More file actions
516 lines (482 loc) · 14 KB
/
Copy pathMyAVLTree.c
File metadata and controls
516 lines (482 loc) · 14 KB
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
// data type for avl tree nodes
typedef struct AVLTreeNode {
int key; //key of this item
int value; //value (int) of this item
int height; //height of the subtree rooted at this node
struct AVLTreeNode *parent; //pointer to parent
struct AVLTreeNode *left; //pointer to left child
struct AVLTreeNode *right; //pointer to right child
} AVLTreeNode;
//data type for AVL trees
typedef struct AVLTree{
int size; // count of items in avl tree
AVLTreeNode *root; // root
} AVLTree;
// create a new AVLTreeNode
AVLTreeNode *newAVLTreeNode(int k, int v )
{
AVLTreeNode *new;
new = malloc(sizeof(AVLTreeNode));
assert(new != NULL);
new->key = k;
new->value = v;
new->height = 0; // height of this new node is set to 0
new->left = NULL; // this node has no child
new->right = NULL;
new->parent = NULL; // no parent
return new;
}
// create a new empty avl tree
AVLTree *newAVLTree()
{
AVLTree *T;
T = malloc(sizeof (AVLTree));
assert (T != NULL);
T->size = 0;
T->root = NULL;
return T;
}
typedef struct tTuple {
int key;
int value;
} tTuple;
tTuple *new_tuple(int key, int value){
tTuple *new;
new = malloc(sizeof(tTuple));
new -> key = key;
new -> value = value;
return new;
}
//do not remove
int InsertNode(AVLTree *T,int k,int v);
void PrintAVLTree(AVLTree *T);
// ********************Auxilliary Functions********************
// ******************** ********************
AVLTreeNode *search_tree(AVLTreeNode *root,int key,int value){
if(root == NULL){ //tree is empty
return NULL;
}
if ( root -> left == NULL && root -> right == NULL){ // Leaf node
if ( root -> value == value && root -> key == key){
return root;
} else return NULL;
}
if ( root->key > key){ // If key is smaller go left
search_tree(root->left,key,value);
} else if ( root->key < key ){ // If key is greater go right
search_tree(root->right,key,value);
} else if ( root->key == key ){ // If key is equal
if ( root->value > value ){ // If value is smaller go left
search_tree(root->left,key,value);
} else if( root->value < value ){ // If value is greater go right
search_tree(root->right,key,value);
} else if ( root->value == value ){ // If key and value are equal
return root;
}
}
}
void update_height(AVLTreeNode *node){ // Update Height after insertion
if ( node->left == NULL && node->right == NULL){
node->height = 0;
} else if (node->left == NULL){
node->height = node->right->height+1;
} else if (node->right == NULL){
node->height = node->left->height+1;
} else {
if(node->left->height > node->right->height) node->height = node->left->height+1;
else node->height = node->right->height+1;
}
if(node->parent == NULL) return; //break statement
update_height(node->parent);
}
//balance factor = height of right subtree - height of left subtree
int balance_factor(AVLTreeNode *node){ //returns the balance factor of a node
int lh,rh;
if(node->left == NULL) {
lh = 0;
} else lh = node->left->height + 1;
if(node->right == NULL) {
rh = 0;
} else rh = node->right->height + 1;
return rh - lh;
}
// Checks if tree is balanced, returns the unbalanced node else returns NULL if tree is balanced
AVLTreeNode *check_balance(AVLTreeNode *node){
int i = balance_factor(node);
//printf("i, key: %d %d\n",i,node->key);
if (i < -1 || i > 1) return node; //If node is unblanced return it
if (node->parent == NULL) return NULL; //break statement
else check_balance(node->parent); //check its ancestor
}
void print_inorder(AVLTreeNode *node){
if(node == NULL) return;
print_inorder(node->left);
printf("(%d, %d), %d\n",node->key,node->value,node->height);
print_inorder(node->right);
}
// void traverse_inorder(AVLTree *T,AVLTreeNode *node){
// if(node == NULL) return;
// traverse_inorder(node->left);
// int j = InsertNode(T,node->key,node->value);
// traverse_inorder(node->right);
// }
void rotate_right(AVLTreeNode *z,AVLTree *T){
AVLTreeNode *y,*parent;
y = z->left;
z->left = y->right;
y->right = z;
parent = z->parent;
z->parent = y;
y->parent = parent;
if(parent != NULL){
if(parent->left->key == z->key && parent->left->value == z->value){
parent->left = y;
} else parent->right = y;
} else T->root = y; //Make new root node (as unbalanced node was root itself)
update_height(z); // Check for errors
}
void rotate_left(AVLTreeNode *z,AVLTree *T){
AVLTreeNode *y,*parent;
parent = z->parent;
y = z->right;
z->right = y->left;
z->parent = y;
y->left = z;
y->parent = parent;
if (parent != NULL){
if(parent->left->key == z->key && parent->left->value == z->value){
parent->left = y;
} else parent->right = y;
} else T->root = y;
update_height(z);
}
void LL(AVLTreeNode *node,AVLTree *T){
rotate_right(node,T);
}
void LR(AVLTreeNode *node,AVLTree *T){
rotate_left(node->left,T);
rotate_right(node,T);
}
void RL(AVLTreeNode *node,AVLTree *T){
rotate_right(node->right,T);
rotate_left(node,T);
}
void RR(AVLTreeNode *node,AVLTree *T){
rotate_left(node,T);
}
void balance_tree(AVLTreeNode *node,AVLTree *T){
int balance = balance_factor(node);
if (balance > 1){ // Right Heavy
//Check RR
if (balance_factor(node->right) >= 0) RR(node,T);
//Check RL
else RL(node,T);
} else if ( balance < -1 ){ // Left Heavy
//Check LL
if (balance_factor(node->left) <= 0) LL(node,T);
//Check LR
else LR(node,T);
}
}
// put your time complexity analysis of CreateAVLTree() here
AVLTree *CreateAVLTree(const char *filename)
{
AVLTree *T;
T = newAVLTree();
FILE *fp;
if (filename == "stdin") {
char ch;
char t_num[5]; //temp number
int key,value;
int tnum_counter = 0, num1, num2;
int is_tuple = 0, is_num = 0;
// Get nodes from file or stdin
int first_char = 0;
while ( (ch = fgetc(stdin)) != EOF){
first_char++;
if( first_char == 1 && ch == '\n'){
break;
} else if( ch == '\n'){
first_char = 0;
continue;
}
else if ( ch == ' ') continue;
else if ( ch == ',' ){ //Get the first number
long l;
char *stopstring;
l = strtol(t_num,&stopstring,10);
num1 = (int) l;
//printf("num1:%d\n",num1);
is_tuple = 1;
strcpy(t_num,""); //reset temp num
tnum_counter = 0; // reset counter
continue ;
} else if ( ch == ')'){ // Get the second number and make tuple,store in array
long l;
char *stopstring;
l = strtol(t_num,&stopstring,10);
num2 = (int) l;
//AVLTreeNode *node;
//node = newAVLTreeNode(num1,num2);
int j = InsertNode(T,num1,num2); // Create a (key,value) tuple
is_tuple = 0; // Reset
is_num = 0; //Reset
for (int z = 0; z < 5;z++){
t_num[z] = '\0';
}
tnum_counter = 0;
} else if ( ch == '('){ // Number starts after opening brackets
is_num = 1;
continue;
}
if ( is_tuple == 0 && is_num == 1){ // First number
t_num[tnum_counter] = ch;
tnum_counter = tnum_counter + 1; //increment counter
} else if ( is_tuple == 1 && is_num == 1){ // Second number
t_num[tnum_counter] = ch;
tnum_counter = tnum_counter + 1; //increment counter
}
}
} else {
fp = fopen(filename,"r");
if ( fp == NULL){
printf("Cannot open file");
exit(-1);
}
char ch;
char t_num[5]; //temp number
int key,value;
//counter for the array
//tTuple *t; // tuple object
int tnum_counter = 0, num1, num2;
int is_tuple = 0, is_num = 0;
// Get nodes from file or stdin
while ( (ch = fgetc(fp)) != EOF){
if ( ch == ' ') continue;
else if ( ch == ',' ){ //Get the first number
long l;
char *stopstring;
l = strtol(t_num,&stopstring,10);
num1 = (int) l;
//printf("num1:%d\n",num1);
is_tuple = 1;
strcpy(t_num,""); //reset temp num
tnum_counter = 0; // reset counter
continue ;
} else if ( ch == ')'){ // Get the second number and make tuple,store in array
long l;
char *stopstring;
l = strtol(t_num,&stopstring,10);
num2 = (int) l;
//AVLTreeNode *node;
//node = newAVLTreeNode(num1,num2);
int j = InsertNode(T,num1,num2); // Create a (key,value) tuple
is_tuple = 0; // Reset
is_num = 0; //Reset
//strcpy(t_num,""); //reset temp num
for (int z = 0; z < 5;z++){
t_num[z] = '\0';
}
tnum_counter = 0;
} else if ( ch == '('){ // Number starts after opening brackets
is_num = 1;
continue;
}
if ( is_tuple == 0 && is_num == 1){ // First number
t_num[tnum_counter] = ch;
tnum_counter = tnum_counter + 1; //increment counter
} else if ( is_tuple == 1 && is_num == 1){ // Second number
t_num[tnum_counter] = ch;
tnum_counter = tnum_counter + 1; //increment counter
}
}
}
//int j;
// for ( j = 0; j < nodes_counter; j++){
// printf("Key : %d, Value : %d\n",nodes[j]->key,nodes[j]->value);
// }
// Sort the array (O(nlogn)
return T;
}
// put your time complexity analysis for CloneAVLTree() here
AVLTreeNode *CloneNode(AVLTreeNode *node,AVLTreeNode *parent_node){
if (node == NULL) return NULL;
AVLTreeNode *new_node;
new_node = newAVLTreeNode(node->key,node->value);
new_node->height = node->height;
new_node->parent = parent_node;
new_node->left = CloneNode(node->left,new_node);
new_node->right = CloneNode(node->right,new_node);
return new_node;
}
AVLTree *CloneAVLTree(AVLTree *T)
{
// put your code here
AVLTree *T2;
T2 = newAVLTree();
T2->root = CloneNode(T->root,NULL);
return T2;
}
void store_tree_in_array(AVLTreeNode *node,tTuple *arr[],int *i){
if(node == NULL) return;
store_tree_in_array(node->left,arr,i);
tTuple *t;
t = new_tuple(node->key,node->value);
arr[*i] = t;
i++;
store_tree_in_array(node->right,arr,i);
}
// put your time complexity for ALVTreesUNion() here
AVLTree *AVLTreesUnion(AVLTree *T1, AVLTree *T2)
{
//put your code here
tTuple *arr1[T1->size];
tTuple *arr2[T2->size];
int i = 0;
store_tree_in_array(T1->root,arr1,&i);
int j = 0;
store_tree_in_array(T2->root,arr2,&j);
}
// put the time complexity analysis for InsertNode() here O(log(n))
int InsertNode(AVLTree *T, int k, int v)
{
//printf("Insertion of node (%d,%d)\n",k,v);
AVLTreeNode *next,*last;
//put your code here
if(T->root == NULL){ // Tree is empty
AVLTreeNode *node;
node = newAVLTreeNode(k,v);
T->root = node;
return 1;
}
next = T->root;
while(next != NULL){
last = next;
if(next->key > k) {
next = next->left;
} else if(next->key < k){
next = next->right;
} else if(next->key == k){
if(next->value > v){
next = next->left;
} else if(next->value < v){
next = next->right;
} else return 0;
}
} // End while loop
AVLTreeNode *node;
node = newAVLTreeNode(k,v);
if(last->key > k) last->left = node;
if(last->key < k) last->right = node;
if(last->key == k){
if(last->value > v) last->left = node;
if(last->value < v) last->right = node;
}
node->parent = last;
node->height = 0;
update_height(node);
T->size++;
AVLTreeNode *unbalanced_node;
unbalanced_node = check_balance(last);
if ( unbalanced_node != NULL) {
//printf("Key %d and Value %d: \n",unbalanced_node->key,unbalanced_node->value);
balance_tree(unbalanced_node,T);
} //else printf("Good node Key %d and Value %d: \n",node->key,node->value);
return 1;
}
// put your time complexity analysis for Search() here
AVLTreeNode *Search(AVLTree *T, int k, int v)
{
AVLTreeNode *node;
node = search_tree(T->root,k,v);
return node;
}
// put your time complexity analysis for freeAVLTree() here
void freeNode(AVLTreeNode *node){
if (node == NULL) return;
if(node != NULL){
freeNode(node->left);
freeNode(node->right);
free(node);
}
}
void FreeAVLTree(AVLTree *T)
{
// put your code here
freeNode(T->root);
free(T);
}
// put your time complexity analysis for PrintAVLTree() here -- O(n)
void PrintAVLTree(AVLTree *T)
{
// put your code here
printf("\nPrinting Tree\n");
print_inorder(T->root);
printf("\nEnd of Tree\n");
}
int main() //sample main for testing
{ int i,j;
AVLTree *tree1, *tree2, *tree3, *tree4, *tree5, *tree6, *tree7, *tree8;
AVLTreeNode *node1;
tree1=CreateAVLTree("stdin");
PrintAVLTree(tree1);
FreeAVLTree(tree1);
//you need to create the text file file1.txt
// to store a set of items without duplicate items
tree2=CreateAVLTree("File1.txt");
PrintAVLTree(tree2);
tree3=CloneAVLTree(tree2);
printf("Tree3 \n");
PrintAVLTree(tree3);
FreeAVLTree(tree2);
FreeAVLTree(tree3);
//Create tree4
tree4=newAVLTree();
j=InsertNode(tree4, 10, 10);
for (i=0; i<15; i++)
{
j=InsertNode(tree4, i, i);
if (j==0) printf("(%d, %d) already exists\n", i, i);
}
PrintAVLTree(tree4);
node1=Search(tree4,20,20);
if (node1!=NULL)
printf("key= %d value= %d\n",node1->key,node1->value);
else
printf("Key 20 does not exist\n");
for (i=17; i>0; i--)
{
j=DeleteNode(tree4, i, i);
if (j==0)
printf("Key %d does not exist\n",i);
PrintAVLTree(tree4);
}
FreeAVLTree(tree4);
//Create tree5
tree5=newAVLTree();
j=InsertNode(tree5, 6, 25);
j=InsertNode(tree5, 6, 10);
j=InsertNode(tree5, 6, 12);
j=InsertNode(tree5, 6, 20);
j=InsertNode(tree5, 9, 25);
j=InsertNode(tree5, 10, 25);
PrintAVLTree(tree5);
//Create tree6
tree6=newAVLTree();
j=InsertNode(tree6, 6, 25);
j=InsertNode(tree6, 5, 10);
j=InsertNode(tree6, 6, 12);
j=InsertNode(tree6, 6, 20);
j=InsertNode(tree6, 8, 35);
j=InsertNode(tree6, 10, 25);
PrintAVLTree(tree6);
tree7=AVLTreesIntersection(tree5, tree6);
tree8=AVLTreesUnion(tree5,tree6);
PrintAVLTree(tree7);
PrintAVLTree(tree8);
return 0;
}