题目
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
运行结果
Case | Hint | Result | Run Time | Memory |
---|---|---|---|---|
0 | fig 1 - LL | Accepted | 3 ms | 392 KB |
1 | fig 2 - RR | Accepted | 2 ms | 492 KB |
2 | fig 3 - RL | Accepted | 2 ms | 416 KB |
3 | fig 4 - LR | Accepted | 3 ms | 416 KB |
4 | 深度LL旋转 | Accepted | 2 ms | 460 KB |
5 | 最大N,深度RL旋转 | Accepted | 2 ms | 416 KB |
6 | 最小N | Accepted | 2 ms | 488 KB |
程序
#include<iostream>
using namespace std;
typedef int KeyType;
typedef struct Node *PtrToTree;
typedef PtrToTree Tree;
struct Node{
KeyType Key;
Tree Left;
Tree Right;
int Height;
};
Tree BuildNode(KeyType Key);
Tree Insert(Tree BT, KeyType Key);
int GetHeight(Tree T);
Tree SingleLeftRotation(Tree Watcher);
Tree DoubleLeftRightRotation(Tree Watcher);
Tree SingleRightRotation(Tree Watcher);
Tree DoubleRightLeftRotation(Tree Watcher);
int Max(int A, int B);
int main()
{
KeyType NewKey;
Tree BT = NULL;
int RootKey;
/*
//测试用例
static int N = 7;
int InputData[N] = {88,70,61,96,120,90,65};
for(int i=0; i<N; i++){
NewKey = InputData[i];
BT = Insert(BT, NewKey);
}
*/
int N;
//读取N,获得总结点个数
cin >> N;
//陆续读取结点键值,始终保持平衡二叉树
for(int i=0; i<N; i++){
cin >> NewKey;
BT = Insert(BT, NewKey);
}
RootKey = BT->Key;
cout << RootKey <<endl;
return 0;
}
Tree Insert(Tree BT, KeyType NewKey)
{
//递归出口设计:遍历到最下的一颗未建结点,则弹出递归堆栈
if(!BT){
BT = BuildNode(NewKey);
}
//递归逻辑:规模减小,遍历树向左右走,
//直到递归出口,到达最底下的未存在结点
//插入左子树,发现并调整非平衡
else if(NewKey < BT->Key){
BT->Left = Insert(BT->Left, NewKey);
//判断是否出现了不平衡树发现者
if( GetHeight(BT->Left) - GetHeight(BT->Right) == 2){
//判断不平衡旋转的类型
if(NewKey < BT->Left->Key)//此为左单旋
BT = SingleLeftRotation(BT);
else //此为左右双旋
BT = DoubleLeftRightRotation(BT);
}
}
//插入右子树,发现并调整非平衡
else if(NewKey > BT->Key){
BT->Right = Insert(BT->Right, NewKey);
if( GetHeight(BT->Left) - GetHeight(BT->Right) == -2){
if(NewKey > BT->Right->Key)//此为右单旋
BT = SingleRightRotation(BT);
else//此为右左双旋
BT = DoubleRightLeftRotation(BT);
}
}
BT->Height = Max( GetHeight(BT->Left), GetHeight(BT->Right) ) +1;
return BT;
}
int Max(int A, int B)
{
return A>B?A:B;
}
Tree DoubleRightLeftRotation(Tree Watcher)
{
//先将发现者的右孩子树进行左单旋
Watcher->Right = SingleLeftRotation(Watcher->Right);
//最后再对自身进行右单旋
return SingleRightRotation(Watcher);
}
Tree SingleRightRotation(Tree Watcher)
{
Tree Passer = Watcher->Right;
Watcher->Right = Passer->Left;
Passer->Left = Watcher;
//每调整一次模型,都要记得更新结点的树高
Watcher->Height = Max( GetHeight(Watcher->Left), GetHeight(Watcher->Right)) +1;
Passer->Height = Max( Watcher->Height, GetHeight(Passer->Right)) +1;
return Passer;
}
Tree DoubleLeftRightRotation(Tree Watcher)
{
//先将发现者的左孩子树进行右旋单旋
Watcher->Left = SingleRightRotation(Watcher->Left);
//最后对自身进行左单旋
return SingleLeftRotation(Watcher);
}
Tree SingleLeftRotation(Tree Watcher)
{
Tree Passer = Watcher->Left;
Watcher->Left = Passer->Right;
Passer->Right = Watcher;
//每调整一次模型,都要记得更新结点的树高
Watcher->Height = Max( GetHeight(Watcher->Left), GetHeight(Watcher->Right)) +1;
Passer->Height = Max( GetHeight(Passer->Left), Watcher->Height) +1;
return Passer;
}
int GetHeight(Tree T)
{
int MaxH, HR, HL;
if(T){
HL = GetHeight(T->Left);
HR = GetHeight(T->Right);
MaxH = Max(HL, HR)+1;
return MaxH;
}
return -1;
}
Tree BuildNode(KeyType Key)
{
Tree NewNode = new Node;
NewNode->Key = Key;
NewNode->Left = NewNode->Right = NULL;
NewNode->Height = 0;
return NewNode;
}