-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAVLTreeSort.c
103 lines (95 loc) · 2.67 KB
/
AVLTreeSort.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
#include "insertion.h"
short height(AVLT *tree){
short right,left;
if(isEmpty(tree))
return 0;
right = height(tree->right);
left = height(tree->left);
return left > right ? (left+1) : (right+1);
}
short calculateBF(AVLT *tree){
if(isEmpty(tree))
return 0;
return height(tree->left) - height(tree->right);
}
void leftRotate(AVLT **tree){
AVLT *aux1 = (*tree)->right, *aux2 = aux1->left;
aux1->left = (*tree);
(*tree)->right = aux2;
(*tree) = aux1;
(*tree)->BF = calculateBF(*tree);
aux1->left->BF = calculateBF(aux1->left);
aux1->BF = calculateBF(aux1);
}
void rightRotate(AVLT **tree){
AVLT *aux1 = (*tree)->left, *aux2 = aux1->right;
aux1->right = (*tree);
(*tree)->left = aux2;
(*tree) = aux1;
(*tree)->BF = calculateBF(*tree);
aux1->right->BF = calculateBF(aux1->right);
aux1->BF = calculateBF(aux1);
}
void insertNodeAVLTree(AVLT **tree, long int number){
if(isEmpty(*tree)){
(*tree) = malloc(sizeof(AVLT));
if(isEmpty(*tree)) return;
(*tree)->value = number;
(*tree)->right = (*tree)->left = NULL;
}
else{
if(number < (*tree)->value)
insertNodeAVLTree(&(*tree)->left,number);
if(number >= (*tree)->value)
insertNodeAVLTree(&(*tree)->right,number);
}
(*tree)->BF = calculateBF(*tree);
//if((*tree)->BF == 2 && (*tree)->left->BF == 1)
//if((*tree)->BF < -1 && (*tree)->right->BF <= 0)
if((*tree)->BF < -1 && number >= (*tree)->right->value)
leftRotate(tree);
//if((*tree)->BF == -2 && (*tree)->right->BF == -1)
//if((*tree)->BF > -1 && (*tree)->left->BF >= 0)
if((*tree)->BF > 1 && number <= (*tree)->left->value)
rightRotate(tree);
//if((*tree)->BF == -2 && (*tree)->right->BF == 1){
//if((*tree)->BF > 1 && (*tree)->left->BF < 0){
if((*tree)->BF < -1 && number <= (*tree)->right->value){
rightRotate(&(*tree)->right);
leftRotate(tree);
}
//if((*tree)->BF == 2 && (*tree)->left->BF == -1){
//if((*tree)->BF < -1 && (*tree)->right->BF > 0){
if((*tree)->BF > 1 && number >= (*tree)->left->value){
leftRotate(&(*tree)->left);
rightRotate(tree);
}
}
AVLT* freeMemory(AVLT *tree){
if(!isEmpty(tree)){
freeMemory(tree->right);
freeMemory(tree->left);
free(tree);
return NULL;
}
}
void storeValueAVLTree(long int *array, AVLT *t, int **i){
if(t != NULL){
if(t->left != NULL)
storeValueAVLTree(array, t->left, i);
*(array + **i) = t->value;
(**i)++;
if(t->right != NULL)
storeValueAVLTree(array, t->right, i);
}
}
void AVLTreeSort(long int *array, int length){
AVLT *tree = NULL;
int *p,i;
for(i ^= i; i < length; i++)
insertNodeAVLTree(&tree,*(i+array));
i ^= i;
p = &i;
storeValueAVLTree(array,tree,&p);
tree = freeMemory(tree);
}