Pat(Advanced Level)Practice--1066(Root of AVL Tree)

Pat1066代码

题目描述:
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

AC代码:
/*
首先平衡二叉树是一个二叉排序树;
其基本思想是:
在构建二叉排序树的过程中,当每插入一个节点时,
先检查是否因为插入而破坏了树的平衡性,若是,
找出最小不平衡树,进行适应的旋转,使之成为新的平衡二叉树。
*/
#include<cstdio>
#include<cstdlib>
#define LH 1
#define EH 0
#define RH -1
#define MAX 25

using namespace std;

typedef struct BTNode
{
	int data;
	int BF;//平衡因子(balance factor)
	struct BTNode *lchild,*rchild;
}BTNode,*BTree;

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

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

void LeftBalance(BTree *T)
{
	BTree 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(BTree *T)
{
	BTree 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(BTree *T,int e,bool *taller)//变量taller反应T长高与否
{
	if(!*T)
	{
		*T=(BTree)malloc(sizeof(BTNode));
		(*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(!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(!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 main(int argc,char *argv[])
{
	int i,n;
	int A[MAX];
	BTree 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;
}

参照了数据结构中平衡二叉树的实现。。。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值