本来不想往blog上面贴一大堆代码的,不过好不容易写出来了,贴出来给大家做个参考把!

程序输入一连串的整数序列,会根据这个整数序列构建一个平衡二叉树。

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
#include <stdio.h>
#include <stdlib.h>
//节点数据结构
struct node{
int val;//值
int bf;//平衡因子
struct node* left;
struct node* right;
};
 
struct node* root;
//struct node* top;
//获得父节点的函数
struct node* GetFather(struct node* son){
	struct node* temp;
	if(son==root)
		return NULL;
	temp=root;
	while(1){
		if(temp==NULL)
			return NULL;
		else if(son->val<=temp->val){
			if(temp->left == son)
				return temp;
			else{
				temp = temp->left;
				continue;
			}
		}
		else if(son->val>temp->val){
			if(temp->right == son)
				return temp;
			else{
				temp = temp->right;
				continue;
			}
		}
	}
}
 
//LL类型旋转
int RorateLL(struct node* inode, struct node* blance){
	struct node* father;
	struct node* left;
	left = blance->left;
	if(father=GetFather(blance)){
		if(blance->val<=father->val)
			father->left = left;
		else 
			father->right = left;
	}
	else
		root = left;
	left->bf = 0;
	blance->bf = 0;
	blance->left = left->right;
	left->right=blance;
	father = left->left;
	while(father!=inode){
		if(inode->val<=father->val){
			father->bf++;
			father = father->left;
		}
		else{
			father->bf--;
			father = father->right;
		}
 
	}
}
//RR类型旋转
int RorateRR(struct node* inode, struct node* blance){
	struct node* father;
	struct node* right;
	right = blance->right;
	if(father=GetFather(blance)){
		if(blance->val<=father->val)
			father->left = right;
		else 
			father->right = right;
	}
	else 
		root = right;
	right->bf = 0;
	blance->bf = 0;
	blance->right = right->left;
	right->left=blance;
	father = right->right;
	while(father!=inode){
		if(inode->val<=father->val){
			father->bf++;
			father = father->left;
		}
		else{
			father->bf--;
			father = father->right;
		}
 
	}
}
//LR类型旋转
int RorateLR(struct node* inode, struct node* blance){
	struct node* father;
	struct node* newtop;
	father = blance;
	while(father!=inode){
		if(inode->val<=father->val){
			father->bf++;
			father = father->left;
		}
		else{
			father->bf--;
			father = father->right;
		}
 
	}
 
	newtop = blance->left->right;
	if(father=GetFather(blance)){
 
		if(blance->val<=father->val)
			father->left = newtop;
		else 
			father->right = newtop;
	}
	else 
		root = newtop;
	blance->left->right = newtop->left;
	newtop->left = blance->left;
	blance->left = newtop->right;
	newtop->right=blance;
	if(newtop->bf == 0){
		newtop->left->bf==0;
		newtop->right->bf==0;
	}
	else if(newtop->bf == 1){
		newtop->left->bf==0;
		newtop->right->bf==-1;
	}
	else if(newtop->bf == -1){
		newtop->left->bf==1;
		newtop->right->bf==0;
	}
	return 1;
 
}
//RL类型旋转
int RorateRL(struct node* inode, struct node* blance){
	struct node* father;
	struct node* newtop;
	father = blance;
	while(father!=inode){
		if(inode->val<=father->val){
			father->bf++;
			father = father->left;
		}
		else{
			father->bf--;
			father = father->right;
		}
 
	}
 
	newtop = blance->right->left;
	if(father=GetFather(blance)){
 
		if(blance->val<=father->val)
			father->left = newtop;
		else 
			father->right = newtop;
	}
	else root=newtop;
	blance->right->left = newtop->right;
	newtop->right = blance->right;
	blance->right = newtop->left;
	newtop->left=blance;
	if(newtop->bf == 0){
		newtop->left->bf==0;
		newtop->right->bf==0;
	}
	else if(newtop->bf == 1){
		newtop->right->bf==0;
		newtop->left->bf==-1;
	}
	else if(newtop->bf == -1){
		newtop->right->bf==1;
		newtop->left->bf==0;
	}
	return 1;
 
}
//插入函数
int AvlInsert(struct node* inode){
	struct node* blance;
	struct node* temp;
	temp=root;
	while(1){
		if(temp->bf==1|temp->bf==-1)
			blance = temp;
		if(inode->val<=temp->val){
			if(temp->left){
				temp=temp->left;
				continue;
			}
			else{
				temp->left=inode;
				break;
			}
		}
		else if(inode->val>temp->val){
			if(temp->right){
				temp=temp->right;
				continue;
			}
			else{
				temp->right=inode;
				break;
			}
		}
	}
 
	if(!blance){
		temp=root;
		while(temp!=inode){
			if(inode->val<=temp->val){
				temp->bf++;
				temp=temp->left;
			}
			else{
				temp->bf--;
				temp=temp->right;
			}
 
		}
		return 1;
 
	}
	else{
		if(blance->bf==-1 && blance->val>=inode->val || blance->bf==1 && blance->val<inode->val){
			blance->bf=0;
			return 1;
		}
		else if(inode->val<=blance->val && inode->val<=blance->left->val)
			RorateLL(inode, blance);
		else if(inode->val<=blance->val && inode->val>blance->left->val)
			RorateLR(inode, blance);
		else if(inode->val>blance->val && inode->val<=blance->right->val)
			RorateRL(inode, blance);
		else if(inode->val>blance->val && inode->val>blance->right->val)
			RorateRR(inode, blance);
 
	}	
	return 1;
 
}
 
int AvlCreate(struct node* inode){
 
	if(!root){
		root = inode;
		return 1;
	}	
	AvlInsert(inode);
 
}
 
//打印整个树的函数
void Traveser(struct node* r)
{   
     if(r->left) Traveser(r->left);
     if(r) printf("%d, %d\n",r->val, r->bf);
     if(r->right) Traveser(r->right);   
}
 
int main(){
 
	int a;
	struct node* Node;
	while(1){
		scanf("%d",&a);
		if (a==-1)
			break;
		Node = malloc(sizeof(struct node));
		Node->val=a;
		Node->bf=0;
		Node->left=NULL;
		Node->right=NULL;
		AvlCreate(Node);
	}
	Traveser(root);
 
}
anyShare分享到:

4 Responses to “建立平衡二叉树(AVLTree)实现代码”

  1. rh says:

    怎么在vc里编译不能通过呀

    • sponge says:

      @rh 我的编译环境是gcc4.3+linux, 可能编译器会有稍许差别,但应该不打,稍微改动下就可以了

  2. RockU says:

    再写个红黑树吧:)

  3. Alex says:

    讲数据结构是要画图的,这样通俗易懂。看不懂啊,刚从陈皓的CoolShell过来,学习了~。加个QQ:543446988

Leave a Reply

Note: Commenter is allowed to use '@User+blank' to automatically notify your reply to other commenter. e.g, if ABC is one of commenter of this post, then write '@ABC '(exclude ') will automatically send your comment to ABC. Using '@all ' to notify all previous commenters. Be sure that the value of User should exactly match with commenter's name (case sensitive).