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 120Sample Output 1:
70Sample Input 2:
7 88 70 61 96 120 90 65Sample Output 2:
88
//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;
}