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
#include <stdio.h>
#include <stdlib.h>
#define ElementType int
typedef struct TNode *AVLTree;
struct TNode{
ElementType Data;
AVLTree Left;
AVLTree Right;
int Height;
};
AVLTree Insert(AVLTree T,ElementType X);
int Max(int a,int b);
int GetHeight(AVLTree T);
AVLTree SingleLeftRotation ( AVLTree A );
AVLTree DoubleLeftRightRotation ( AVLTree A );
AVLTree SingleRightRotation(AVLTree T);
AVLTree DoubleRightLeftRotation(AVLTree T);
int main()
{
int N,X;
AVLTree Tree=NULL;
scanf("%d",&N);
for(int i=0;i<N;i++)
{
scanf("%d",&X);
Tree = Insert(Tree,X);
}
printf("%d\n",Tree->Data);
return 0;
}
int Max(int a,int b)
{
return a>b?a:b;
}
int GetHeight(AVLTree T)
{
if(!T) return -1;
else return T->Height;
}
AVLTree Insert(AVLTree T,ElementType X)
{
/**
如果是空,则构造树节点
如果不为空,就插入到树中去。比节点的值小就插入到左子树,大就插入到右子树。
要看的麻烦节点相对于发现者的位置关系。
如果是右子树的右子树(可能在最底层是查到了左边)那么就是RR,右单旋
如果是左子树的左子树(可能最底层是插到了右边)那么就是LL,左单旋
如果是左子树的右子树,那就是LR,那么就先做右单旋,再做左单旋
如果是右子树的左子树,那么就是RL,先做左单旋,再做右单旋
每次做完调整就要调整每个节点的高度,因为调整都是设计到最底层的节点,所以高度可以理解为从叶子结点往上推
**/
if(!T)
{
T=(AVLTree)malloc(sizeof(struct TNode));
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){
// printf("T=%d\n",T->Data);
if(X< T->Left->Data)
T = SingleLeftRotation(T);
else
T = DoubleLeftRightRotation(T);
}
}else if(X>T->Data){
T->Right = Insert(T->Right,X);
if(GetHeight(T->Right)-GetHeight(T->Left)==2){
// printf("T=%d\n",T->Data);
if(X>T->Right->Data)
T = SingleRightRotation(T);
else
T = DoubleRightLeftRotation(T);
}
}
//更新树高
T->Height = Max(GetHeight(T->Left),GetHeight(T->Right))+1;
return T;
}
AVLTree SingleLeftRotation ( AVLTree A )
{ /* 注意:A必须有一个左子结点B */
/* 将A与B做左单旋,更新A与B的高度,返回新的根结点B */
AVLTree 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;
}
AVLTree DoubleLeftRightRotation ( AVLTree A )
{ /* 注意:A必须有一个左子结点B,且B必须有一个右子结点C */
/* 将A、B与C做两次单旋,返回新的根结点C */
/* 将B与C做右单旋,C被返回 */
A->Left = SingleRightRotation(A->Left);
/* 将A与C做左单旋,C被返回 */
return SingleLeftRotation(A);
}
AVLTree SingleRightRotation(AVLTree A)
{
AVLTree 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->Right), A->Height ) + 1;
return B;
}
AVLTree DoubleRightLeftRotation(AVLTree A)
{
A->Right = SingleLeftRotation(A->Right);
return SingleRightRotation(A);
}