-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl.c
138 lines (119 loc) · 3.36 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
#include <stdio.h>
#include <stdlib.h>
#include "avl.h"
#define MAX(a, b) (a)>(b)?a:b
int
avl_get_height(AvlTree *root) {
if (!root) return 0;
if (root->left && root->right){
return MAX(root->left->height, root->right->height)+1;
} else if(root->left) {
return root->left->height+1;
} else if (root->right) {
return root->right->height+1;
}
return MAX(avl_get_height(root->left), avl_get_height(root->right)) + 1;
}
int avl_get_balance_factor(AvlTree *root) {
if (!root) return 0;
return avl_get_height(root->left) - avl_get_height(root->right);
}
AvlTree*
avl_rr(AvlTree* tree) {
AvlTree* left = tree->left;
tree->left = left->right;
left->right = tree;
left->height = MAX(avl_get_height(left->left), avl_get_height(left->right)) + 1;
tree->height = MAX(avl_get_height(tree->left), avl_get_height(tree->right)) + 1;
return left;
}
AvlTree*
avl_rl(AvlTree* tree) {
AvlTree* right = tree->right;
tree->right = right->left;
right->left = tree;
right->height = MAX(avl_get_height(right->left), avl_get_height(right->right)) + 1;
tree->height = MAX(avl_get_height(tree->left), avl_get_height(tree->right)) + 1;
return right;
}
AvlTree*
avl_rebalance(AvlTree *tree) {
int factor = avl_get_balance_factor(tree);
if (factor > 1 && avl_get_balance_factor(tree->left) > 0) {
return avl_rr(tree);
} else if (factor > 1 && avl_get_balance_factor(tree->left) <= 0) {
tree->left = avl_rl(tree->left);
return avl_rr(tree);
} else if (factor < -1 && avl_get_balance_factor(tree->right) <= 0) {
return avl_rl(tree);
} else if (factor < -1 && avl_get_balance_factor(tree->right) > 0) {
tree->right = avl_rr(tree->right);
return avl_rl(tree);
} else {
tree->height = avl_get_height(tree);
return tree;
}
}
AvlTree*
avl_insert(AvlTree *tree, int key) {
if (!tree) {
tree = malloc(sizeof(AvlTree));
tree->left = NULL;
tree->right = NULL;
tree->height = 1;
tree->key = key;
} else if (tree->key == key) {
return tree;
} else {
if (tree->key < key) {
tree->right = avl_insert(tree->right, key);
}
else {
tree->left = avl_insert(tree->left, key);
}
}
return avl_rebalance(tree);
}
int
avl_search(AvlTree* tree, int val) {
if (!tree) return -1;
if (val == tree->key) {
return 1;
}
else if (tree->key < val) {
return avl_search(tree->right, val);
}
else {
return avl_search(tree->left, val);
}
}
void
avl_travel(AvlTree* tree, int key, int dir) {
if(!tree) return;
if (dir == 0) {
printf("tree is root: %d, height:%d\n", tree->key, tree->height);
} else if (dir == -1) {
// printf("%d is %d left child:%d\n", tree->key, key, tree->height);
} else {
// printf("%d is %d right child:%d\n", tree->key, key, tree->height);
}
avl_travel(tree->left, tree->key, -1);
avl_travel(tree->right, tree->key, 1);
}
#ifdef DEBUG
int main()
{
AvlTree *tree = NULL;
for (int i=0; i<1000000; i++) {
tree = avl_insert(tree, i);
}
avl_travel(tree,0,0);
int ret = avl_search(tree, 31);
if (ret == 1) {
printf("found\n");
} else {
printf("not found\n");
}
return 0;
}
#endif //DEBUG