1066. Root of AVL Tree (25)

题目:

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

    

    

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print ythe root of the resulting AVL tree in one line.

Sample Input 1:
5
88 70 61 96 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88
注意:
1、刚看完题目顿时就起劲了,1064不就是跟这个类似嘛,遂直接把1064计算根结点的代码复制过来提交,结果case 4和case 5一直都过不了。
2、别偷懒了,老老实实用AVL自平衡二叉树的算法吧。
3、程序参考的是程杰的《大话数据结构》。

代码:
//1066
#include<iostream>
using namespace std;
#define LH 1 //左高
#define EH 0 //等高
#define RH -1 //右高

typedef struct BiTNode
{
	int data;//节点数据
	int bf;//平衡因子(balance factor),左树高度减去右树高度
	struct BiTNode *lchild,*rchild;//左右孩子指针
}BiTNode,*BiTree;

void R_Rotate(BiTree *p)//以p为根节点的二叉排序树进行右旋转
{
	BiTree L;
	L=(*p)->lchild;
	(*p)->lchild=L->rchild;
	L->rchild=(*p);
	*p=L;
}

void L_Rotate(BiTree *p)//以p为根节点的二叉排序树进行左旋转
{
	BiTree R;
	R=(*p)->rchild;
	(*p)->rchild=R->lchild;
	R->lchild=(*p);
	*p=R;
}

void LeftBalance(BiTree *T)
{
	BiTree L,Lr;
	L=(*T)->lchild;
	switch(L->bf)
	{
		//检查T的左子树平衡度,并作相应的平衡处理
		case LH://新节点插入在T的左孩子的左子树上,做单右旋处理
			(*T)->bf=L->bf=EH;
			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(BiTree *T)
{
	BiTree R,Rl;
	R=(*T)->rchild;
	switch(R->bf)
	{
		case RH://新节点插在T的右孩子的右子树上,要做单左旋处理
			(*T)->bf=R->bf=EH;
			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);
	}
}

bool InsertAVL(BiTree *T,int e,bool *taller)//变量taller反映T长高与否
{
	if(!*T)
	{//插入新结点,树“长高”,置taller为true
		*T=new BiTNode;
		(*T)->data=e;
		(*T)->lchild=(*T)->rchild=NULL;
		(*T)->bf=EH;
		*taller=true;
	}
	else
	{
		if(e==(*T)->data)
		{//树中已存在和e有相同关键字的结点则不再插入
			*taller=false;
			return false; 
		}
		if(e<(*T)->data)
		{//应继续在T的左子树中进行搜索
			if(!InsertAVL(&(*T)->lchild,e,taller))//未插入
				return false;
			if(*taller)//已插入到T的左子树中且左子树“长高”
			{
				switch((*T)->bf)//检查T的平衡度
				{
					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(!InsertAVL(&(*T)->rchild,e,taller))
				return false;
			if(*taller)//已插入到T的右子树且右子树“长高”
			{
				switch((*T)->bf)//检查T的平衡度
				{
					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 main()
{
	int i,n;
	int a[21];
	BiTree T=NULL;
	bool taller;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=0;i<n;i++)
		InsertAVL(&T,a[i],&taller);
	printf("%d\n",T->data);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值