平衡二叉树的旋转问题

主要是要想好有哪些情况

,只有想清楚全部情况后,才能对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;
 } 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值