平衡二叉树的旋转问题
2017-05-04 16:09
155 查看
主要是要想好有哪些情况
,只有想清楚全部情况后,才能对BF进行判断
#include<stdio.h>
#include<stdlib.h>
typedef struct BiTre{
int data;
int bf;
struct BiTre *rchild,*lchild;
}BiTnood,*BiTree;
void R_Rotate(BiTree *p)
{
BiTree l = (*p)->lchild;
(*p)->lchild = l->rchild;
l->rchild = (*p);
(*p) = l;
}
void L_Rotate(BiTree *p)
{
BiTree l = (*p)->rchild;
(*p)->rchild = l->lchild;
l->lchild = (*p);
(*p)= l;
}
void LeftBalance(BiTree *T)//改变左子树的状态
{
BiTree L,Lr;
L = (*T)->lchild;
switch(L->bf)
{
case 1:
L->bf=0;
(*T)->bf = 0;
R_Rotate(T);
break;
case -1:
Lr = L->rchild;
switch(Lr->bf)
{
case 0:
L->bf=0;
(*T)->bf=0;
break;
case 1:
(*T)->bf = 1;
L->bf = 0;
break;
case -1:
(*T)->bf = 0;
L->bf = -1;
break;
}
Lr->bf = 0;
L_Rotate(&((*T)->lchild));
R_Rotate(T);
}
}
void ReftBalance(BiTree *T)//改变右子树的状态
{
BiTree R,Rl;
R = (*T)->rchild;
switch(R->bf)
{
case -1:
(*T)->bf = 0;
R->bf = 0;
R_Rotate(T);
break;
case 1:
Rl = R->lchild;
switch(Rl->bf)
{
case 0:
(*T)->bf=0;
R->bf=0;
break;
case 1:
(*T)->bf = 0;
R->bf = -1;
break;
case -1:
(*T)->bf = 1;
R->bf = 0;
}
Rl->bf=0;
R_Rotate(&((*T)->rchild));
L_Rotate(T);
}
}
int main()
{
return 0;
}
,只有想清楚全部情况后,才能对BF进行判断
#include<stdio.h>
#include<stdlib.h>
typedef struct BiTre{
int data;
int bf;
struct BiTre *rchild,*lchild;
}BiTnood,*BiTree;
void R_Rotate(BiTree *p)
{
BiTree l = (*p)->lchild;
(*p)->lchild = l->rchild;
l->rchild = (*p);
(*p) = l;
}
void L_Rotate(BiTree *p)
{
BiTree l = (*p)->rchild;
(*p)->rchild = l->lchild;
l->lchild = (*p);
(*p)= l;
}
void LeftBalance(BiTree *T)//改变左子树的状态
{
BiTree L,Lr;
L = (*T)->lchild;
switch(L->bf)
{
case 1:
L->bf=0;
(*T)->bf = 0;
R_Rotate(T);
break;
case -1:
Lr = L->rchild;
switch(Lr->bf)
{
case 0:
L->bf=0;
(*T)->bf=0;
break;
case 1:
(*T)->bf = 1;
L->bf = 0;
break;
case -1:
(*T)->bf = 0;
L->bf = -1;
break;
}
Lr->bf = 0;
L_Rotate(&((*T)->lchild));
R_Rotate(T);
}
}
void ReftBalance(BiTree *T)//改变右子树的状态
{
BiTree R,Rl;
R = (*T)->rchild;
switch(R->bf)
{
case -1:
(*T)->bf = 0;
R->bf = 0;
R_Rotate(T);
break;
case 1:
Rl = R->lchild;
switch(Rl->bf)
{
case 0:
(*T)->bf=0;
R->bf=0;
break;
case 1:
(*T)->bf = 0;
R->bf = -1;
break;
case -1:
(*T)->bf = 1;
R->bf = 0;
}
Rl->bf=0;
R_Rotate(&((*T)->rchild));
L_Rotate(T);
}
}
int main()
{
return 0;
}
相关文章推荐
- 平衡二叉树插入—单旋转双旋转问题
- WPF中连续旋转时角度错误的问题
- J2ME 2D游戏开发中的图形旋转问题
- {Programming Pearls}第二章--向量旋转问题
- 求助,向量旋转问题
- 关于 元件中心旋转的问题
- 修正vtkboxwidget2不能禁止旋转的问题
- 旋转方阵问题
- [转载]MFC中OpenGL旋转的问题
- 标注随测量物体旋转问题
- 绕轴旋转问题
- 如何在 GDI+ 中指定旋转中心 - 回复 "wuheng66888" 的问题
- 绕点旋转(老问题)
- 桌面图标 背景 阴影 问题 + 屏幕 显示 旋转 斜看 问题
- WINCE 屏幕旋转的问题
- 绕轴旋转问题
- MFC中OpenGL旋转的问题
- [VB.NET]图形旋转的问题?
- 关于图形旋转的问题(GDI+)
- LAUNCHER横竖屏旋转后长按不能生效的问题