题目:
04-树5 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 the 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.这道题本质上就是要求你构造一个平衡二叉树,我们知道平衡二叉树的构造里面有LL,RR,LR,RL 四种方式。首先要理解这三种方式的含义。
2.在构造的时候采用递归的方法,然后逐层返回,返回的同时再判断是否存在不平衡现象,如果存在则调用旋转函数,直到返回到根节点才完成一次数的插入,这样做才能保证每一次插入以后树都是平衡状态。
遇到的问题:
1.这次遇到的bug比较少,还是那个当遇到异常停止的时候记得要检查带返回值的函数是否用法是正确的!
代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct TreeNode *AVLTree;
struct TreeNode{
int Data;
AVLTree Left;
AVLTree Right;
int Height;
};
int GetHeight(AVLTree T);
int Max(int a,int b);
AVLTree Insert(AVLTree T,int x);
AVLTree SingleLeftRotation(AVLTree A); //LL rotation
AVLTree SingleRightRotation(AVLTree A); //RR rotation
AVLTree DoubleLeftRotation(AVLTree A); //LR rotation
AVLTree DoubleRightRotation(AVLTree A); //Rl rotation
int GetHeight(AVLTree T){
if(!T) return 0;
return T->Height;
}
int Max(int a,int b){
return a>b?a:b;
}
//LL rotation
AVLTree SingleLeftRotation(AVLTree A){
AVLTree B;
B=A->Left;
A->Left=B->Right;
B->Right=A;
A->Height=Max(GetHeight(A->Left),GetHeight(A->Right))+1;
B->Height=Max(GetHeight(B->Left),A->Height)+1;
return B;
}
//RR rotation
AVLTree SingleRightRotation(AVLTree A){
AVLTree B;
B=A->Right;
A->Right=B->Left;
B->Left=A;
A->Height=Max(GetHeight(A->Left),GetHeight(A->Right))+1;
B->Height=Max(GetHeight(B->Left),A->Height)+1;
return B;
}
//LR rotation
AVLTree DoubleLeftRotation(AVLTree A){
A->Left=SingleRightRotation(A->Left);
return SingleLeftRotation(A);
}
//RL rotation
AVLTree DoubleRightRotation(AVLTree A){
A->Right=SingleLeftRotation(A->Right);
return SingleRightRotation(A);
}
AVLTree Insert(AVLTree T,int x){
if(!T){
T=(AVLTree)malloc(sizeof (struct TreeNode));
T->Data=x;
T->Left=T->Right=NULL;
T->Height=0;
}
else if(x<T->Data){
T->Left=Insert(T->Left,x);
if(GetHeight(T->Left)-GetHeight(T->Right)==2){
if(x<T->Left->Data)
T=SingleLeftRotation(T);
else
T=DoubleLeftRotation(T);
}
}
else if(x>T->Data){
T->Right=Insert(T->Right,x);
if(GetHeight(T->Left)-GetHeight(T->Right)==-2){
if(x>T->Right->Data)
T=SingleRightRotation(T);
else
T=DoubleRightRotation(T);
}
}
T->Height=Max(GetHeight(T->Left),GetHeight(T->Right))+1;
return T;
}
int main()
{
int N,x;
AVLTree T=NULL;
scanf("%d",&N);
for(int i=0;i<N;i++){
scanf("%d",&x);
T=Insert(T,x);
}
if(T) printf("%d",T->Data);
return 0;
}