-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl.c
executable file
·278 lines (254 loc) · 6.26 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
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
/**
* @file avl.c node contains the hight
* @brief node contains the hight
* the hight is the max number in {left , right} +1
* ps: when i use the function similar to rb tree insert
* i found avl tree insert a node change the every node member in this son tree
* the hight all changed but when final to insert need to count the every node in
* son tree hight, so i decide to use the recursive way to realize the avl insert
* @author tmd
* @version 1.0
* @date 2016-12-01
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include"avl.h"
int get_hight(Node* node){
if(node == NULL)
return 0;
return node->hight;
}
int max(int a, int b){//avl tree not exist equal number
return a>b?a:b;
}
/**
* @brief get_stat get the hight difference
* case1 or case2 left-left >1
* case3 or case4 right-right <-1
* @param node
* @return
*/
int get_stat(Node* node){
if(node == NULL)
exit(1);
return get_hight(node->left)-get_hight(node->right);
}
Node* avl_init_node(int key){
Node* tmp = (Node*)malloc(sizeof(Node));
if(tmp == NULL)
return NULL;
tmp->left = NULL;
tmp->pre = NULL;
tmp->right = NULL;
tmp->key = key;
tmp->hight = 1;
return tmp;
}
Tree* avl_init_tree(){
Tree* tree = (Tree*)malloc(sizeof(Tree));
tree->root = NULL;
if(tree == NULL)
return NULL;
return tree;
}
//return the new root node
Node* left_rotate(Tree* tree, Node* node){
Node* y = node->right;
node->right = y->left;
if(y->left != NULL)
y->left->pre = node;
y->pre = node->pre;
if(node->pre == NULL){
tree->root = y;
}else if(node->pre->left == node){
node->pre->left = y;
}else{
node->pre->right = y;
}
y->left = node;
node->pre = y;
//change the hight
node->hight = max(get_hight(node->left),
get_hight(node->right)) + 1;
y->hight = max(get_hight(y->left),
get_hight(y->right)) + 1;
return y;
}
Node* right_rotate(Tree* tree, Node* node){
Node* y = node->left;
node->left = y->right;
if(y->right != NULL)
y->right->pre = node;
y->pre = node->pre;
if(node->pre == NULL){
tree->root = y;
}else if(node->pre->left == node){
node->pre->left = y;
}else{
node->pre->right = y;
}
y->right = node;
node->pre = y;
//change the hight
node->hight = max(get_hight(node->left),
get_hight(node->right)) + 1;
y->hight = max(get_hight(y->left),
get_hight(y->right)) + 1;
return y;
}
//the iter way
Node* avl_search(Tree* tree, int key){
while(tree->root != NULL && tree->root->key != key){
if(key < tree->root->key){
tree->root = tree->root->left;
}else{
tree->root = tree->root->right;
}
}
return tree->root;
}
/**
* @brief balance use the functhion of get_stat
* according the each case to let the avl tree balance
* case1 left-left use rightrotate
* case2 left-right use left then right rotate
* case3 right-left use right then left rotate
* case4 right-right use left rotate
* @param node
*
* @return
*/
int get_balance(Node* node){
if(node == NULL)
return 0;
return get_hight(node->left) - get_hight(node->right);
}
Node* avl_insert(Tree* tree,Node* root, Node* node){
//the normal bst also can use this way
if(root == NULL)
return node;
if(node->key < root->key){
root->left = avl_insert(tree,root->left,node);
}else{
root->right = avl_insert(tree,root->right,node);
}
//reset the node hight
root->hight = max(get_hight(root->left),
get_hight(root->right)) + 1;
//get balance
int balance = get_balance(root);
//according to the balance judge which case
//case1 Left Left
if(balance > 1 && node->key < root->left->key){
return right_rotate(tree,root);
}
//case2 Left Right
if(balance > 1 && node->key > root->right->key){
left_rotate(tree,root->left);
return right_rotate(tree,root);
}
//case3 Right Left
if(balance < -1 && node->key < root->right->key){
right_rotate(tree,root->right);
return left_rotate(tree,root);
}
//case4 Right Right
if(balance < -1 && node->key > root->right->key){
return left_rotate(tree,root);
}
//return the unchanged root node;
return root;
}
//minnum
Node* minnum(Node* node){
while(node->left != NULL)
node = node->left;
return node;
}
/**
* @brief avl_delete this function only suit for the node
* which dont have the pre element
* @param tree
* @param root
* @param node
*
* @return
*/
Node* avl_delete(Tree* tree,Node* root, Node* node){
//the normal bst delete
if(root == NULL)
return root;
if(node->key < root->key){
root->left = avl_delete(tree,root->left,node);
}else if(node->key > root->key){
root->right = avl_delete(tree,root->right,node);
}else{// find the euqual node
//have only one child or no child
if((root->left == NULL)|| (root->right == NULL)){
Node* tmp = root->left?root->left:root->right;
//no child
if(tmp == NULL){
tmp = root;
root = NULL;
}else{//have only child
*root = *tmp;//copy the contents
}
free(tmp);
}else{
//node with two child
Node* successer = minnum(root->right);
root->key = successer->key;
//delete this successer node
root->right = avl_delete(tree,root->right,successer);
}
}
//if tree has only one node then return
if(root == NULL){
return root;
}
//update hight
root->hight = max(get_hight(root->left),get_hight(root->right)) + 1;
int balance = get_balance(root);
// If this node becomes unbalanced, then there are 4 cases
// Left Left Case
if (balance > 1 && get_balance(root->left) >= 0)
return right_rotate(tree,root);
// Left Right Case
if (balance > 1 && get_balance(root->left) < 0)
{
root->left = left_rotate(tree,root->left);
return right_rotate(tree,root);
}
// Right Right Case
if (balance < -1 && get_balance(root->right) <= 0)
return left_rotate(tree,root);
// Right Left Case
if (balance < -1 && get_balance(root->right) > 0)
{
root->right = right_rotate(tree,root->right);
return left_rotate(tree,root);
}
return root;
}
int show(Node* root){
if(root != NULL){
printf("%d\n",root->key);
show(root->left);
show(root->right);
}
return -1;
}
//test main function
int main(){
Tree* tree = avl_init_tree();
Node* tmp = avl_init_node(12);
Node* tmp1 = avl_init_node(9);
Node* tmp2 = avl_init_node(8);
tree->root = avl_insert(tree,tree->root,tmp);
tree->root = avl_insert(tree,tree->root,tmp1);
tree->root = avl_insert(tree,tree->root,tmp2);
show(tree->root);
return 0;
}