-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL1.cpp
412 lines (393 loc) · 9.97 KB
/
AVL1.cpp
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
#include<stdio.h>
#include<stdlib.h>
#define LH 1
#define EH 0
#define RH -1
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType;
//定义树结点
typedef struct BBSTNode
{
ElemType data;
int bf;//平衡因子(balance factor)
struct BBSTNode *lchild,*rchild;
} BBSTNode,*BBSTree;
void R_Rotate(BBSTree &p) //以p为根节点的二叉排序树进行右旋转
{
BBSTree L;
L=p->lchild;
p->lchild=L->rchild;
L->rchild=p;
p=L;//p指向新的根节点
}
void L_Rotate(BBSTree &p) //以p为根节点的二叉排序树进行左旋转
{
BBSTree R;
R=p->rchild;
p->rchild=R->lchild;
R->lchild=p;
p=R;
}
void LeftBalance(BBSTree &T) //对二叉平衡树进行左平衡处理
{
BBSTree L,Lr;
L=T->lchild;
switch(L->bf) //检查T的左子树平衡度,并作相应的平衡处理
{
case LH: //新节点插入在T的左孩子的左子树上,做单右旋处理
T->bf=L->bf=EH;
R_Rotate(T);
break;
case EH: /*这个地方需要特别注意,在如果程序涉及到删除结点,这个情况就必须考虑*/
T->bf = EH;
L->bf = RH;
R_Rotate(T);
break;
case RH: //新插入节点在T的左孩子的右子树上,做双旋处理
Lr=L->rchild;
switch(Lr->bf)
{
case LH:
T->bf=RH;
L->bf=EH;
break;
case EH:
T->bf=L->bf=EH;
break;
case RH:
T->bf=EH;
L->bf=LH;
break;
}
Lr->bf=EH;
L_Rotate(T->lchild);
R_Rotate(T);
}
}
void RightBalance(BBSTree &T) //对二叉平衡树进行右平衡处理
{
BBSTree R,Rl;
R=T->rchild;
switch(R->bf)
{
case RH: //新节点插在T的右孩子的右子树上,要做单左旋处理
T->bf=R->bf=EH;
L_Rotate(T);
break;
case EH: /*这个地方需要特别注意,在如果程序涉及到删除节点,这个情况就必须考虑*/
T->bf = RH;
R->bf = LH;
L_Rotate(T);
break;
case LH: //新节点插在T的右孩子的左子树上,要做双旋处理
Rl=R->lchild;
switch(Rl->bf)
{
case LH:
T->bf=EH;
R->bf=RH;
break;
case EH:
T->bf=R->bf=EH;
break;
case RH:
T->bf=LH;
R->bf=EH;
break;
}
Rl->bf=EH;
R_Rotate(T->rchild);
L_Rotate(T);
}
}
//向平衡二叉树T插入数值为e的结点
Status InsertAVL(BBSTree &T, int e, Status &taller)//变量taller反映T长高与否
{
if(NULL == T)
{
T = (BBSTree)malloc(sizeof(BBSTNode));
T->data = e;
T->lchild = T->rchild = NULL;
T->bf = EH;
taller = TRUE;
}
else
{
if(e==T->data)//不插入
{
taller = FALSE;
return FALSE;
}
if(e<T->data)
{
if(FALSE == InsertAVL(T->lchild,e,taller))//未插入
return FALSE;
if(taller) //以插入左子树,且左子树变高
{
switch(T->bf)
{
case LH: //原本左子树比右子树高,需要做左平衡处理
LeftBalance(T);
taller = FALSE;
break;
case EH: //原本左右子树等高,现因左子树增高而树增高
T->bf=LH;
taller = TRUE;
break;
case RH: //原本右子树比左子树高,现在左右子树等高
T->bf=EH;
taller = FALSE;
break;
}
}
}
else
{
//应在T的右子树中搜寻
if(FALSE == InsertAVL(T->rchild,e,taller))
return FALSE;
if(taller) //插入右子树,且右子树长高
{
switch(T->bf)
{
case LH: //原本左子树比右子树高,现在左右子树等高
T->bf = EH;
taller = FALSE;
break;
case EH: //原本左右子树等高,现在右子树变高
T->bf = RH;
taller = TRUE;
break;
case RH: //原本右子树比左子树高,现在需做右平衡处理
RightBalance(T);
taller = FALSE;
break;
}
}
}
}
return TRUE;
}
//求平衡二叉树的深度
int Depth(BBSTree T)
{
if(NULL == T) return 0;
int l,r;
l = Depth(T->lchild);
r = Depth(T->rchild);
return l>r?(l+1):(r+1);
}
int TreeBf(BBSTree T)
{
return Depth(T->lchild)-Depth(T->rchild);
}
//删除平衡二叉树中数值为e的结点
Status DeleteAVL(BBSTree &T, ElemType e, Status &shorter)
{
if(T == NULL)
{
return FALSE;
}
else if(e == T->data) //删除
{
BBSTree temp = T;
if(T->lchild == NULL)
{
T = T->rchild;
free(temp);
shorter = TRUE;
}
else if(T->rchild == NULL)
{
T = T->lchild;
free(temp);
shorter = TRUE;
}
else
{
BBSTree p = T->lchild;
while(p->rchild)
{
p = p->rchild;
}
T->data = p->data;
DeleteAVL(T->lchild,p->data,shorter);
if(shorter == TRUE)
{
switch(T->bf)
{
case LH:
T->bf = EH;
shorter = TRUE;
break;
case EH: //子树高度不降低的特殊情况:即左右都有结点,删除一个不影响子树高度
T->bf = RH;
shorter = FALSE;
break;
case RH:
RightBalance(T); //处理右平衡
if(T->rchild->bf == EH) //删除时的特殊情况:右孩子结点bf为0,子树高度不变
{
shorter = FALSE;
}
else
{
shorter = TRUE;
}
break;
}
}
}
}
else if(e < T->data) //左子树中继续查找
{
if(!DeleteAVL(T->lchild,e,shorter))//删除失败直接return flase
{
return FALSE;
}
if(shorter) //左子树中结点删除成功,并且子树高度降低
{
switch(T->bf)
{
case LH:
T->bf = EH;
shorter = TRUE;
break;
case EH: /*子树高度不降低的特殊情况:即左右都有结点,删除一个不影响子树高度*/
T->bf = RH;
shorter = FALSE;
break;
case RH:
RightBalance(T); //处理右平衡
if(T->rchild->bf == EH)/*删除时的特殊情况:右孩子结点bf为0,子树高度不变*/
{
shorter = FALSE;
}
else
{
shorter = TRUE;
}
break;
}
}
}
else //右子树中继续查找
{
if(!DeleteAVL(T->rchild,e,shorter))
{
return FALSE;
}
if(shorter)
{
switch(T->bf)
{
case LH:
LeftBalance(T);
if(T->lchild->bf == EH) /*删除时的特殊情况:左孩子结点bf为0,子树高度不变*/
{
shorter = FALSE;
}
else
{
shorter = TRUE;
}
break;
case EH:
T->bf = LH;
shorter = FALSE;
break;
case RH:
T->bf = EH;
shorter = TRUE;
break;
}
}
}
return TRUE;
}
//打印平衡二叉树
void PrintBBSTree(BBSTree T)
{
if(T)
{
printf("%d",T->data);
int i = TreeBf(T);
printf("[%d]",i);
if(T->lchild||T->rchild)
{
printf("(");
PrintBBSTree(T->lchild);
printf(",");
PrintBBSTree(T->rchild);
printf(")");
}
}
}
//中序遍历平衡二叉树
void InorderTraverse(BBSTree T)
{
if(NULL == T) return;
InorderTraverse(T->lchild);
printf("%d ",T->data);
InorderTraverse(T->rchild);
}
int main()
{
int i,select,data;
ElemType A[]= {3,2,1,4,5,6,7,10,9,8}; //预先插入一些数据
BBSTree T = NULL;
Status taller = FALSE, shorter = FALSE;
for(i=0; i<10; i++)
{
InsertAVL(T,A[i],taller);
}
printf("现在的平衡树结点值为:\n");
PrintBBSTree(T);
printf("\n");
do
{
printf("输入 1 插入结点\n");
printf("输入 2 删除结点\n");
printf("输入 3 打印平衡二叉树\n");
printf("输入 4 退出程序\n");
printf("输入 5 中序遍历平衡二叉树\n");
printf("请输入你的选择\n");
scanf("%d",&select);
switch(select)
{
case 1:
printf("请输入你要插入的结点值\n");
scanf("%d", &data);
if(FALSE == InsertAVL(T,data,taller))
printf("插入出错,请检查插入的数值是否有误\n");
else printf("插入成功!\n");
break;
case 2:
printf("请输入你要删除的结点值\n");
scanf("%d", &data);
if(FALSE == DeleteAVL(T,data,shorter))
printf("删除出错,请检查删除的数值是否有误\n");
else printf("删除成功!\n");
break;
case 3:
printf("现在平衡二叉树的所有结点为:\n");
PrintBBSTree(T);
printf("\n");
break;
case 4:
break;
case 5:
InorderTraverse(T);
printf("\n");
break;
default:
printf("输入有误,请重新选择!\n");
break;
}
}
while(select!=4); //当输入4时退出程序,否则重新进入循环体
printf("程序已退出,谢谢!\n");
return 0;
}