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.
AVL树是一种自平衡的二叉搜索树。在AVL树中,任意节点的两个子树的高度最多相差一;如果在任何时候,它们之间的差异超过一个,则进行重新平衡以恢复此属性。图1-4说明了旋转规则。
Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.
现在给定一个插入序列,您应该得到的AVL树的根。
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.
每个输入文件包含一个测试用例。对于每种情况,第一行包含一个正整数N(≤20),它是要插入的键的总数。然后在下一行中给出N个不同的整数键。一行中的所有数字都用空格隔开。
Output Specification:
输出规格:
For each test case, print the root of the resulting AVL tree in one line.
对于每个测试用例,在一行中打印产生的AVL树的根。
Sample Input 1:
样例输入1:
5
88 70 61 96 120
Sample Output 1:
样例输出1:
70
Sample Input 2:
样例输入2:
7
88 70 61 96 120 90 65
Sample Output 2:
样例输出:
88
解题思路:
设置平衡因子,在构建排序二叉树的过程中,每当插入一个结点时,先检查是否因插入而破坏了树的平衡性,若是,则找出最小不平衡树,在保证二叉排序树的前提下进行相应的旋转,实之成为新的平衡子树。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef struct AVLNode *AVLTree;
struct AVLNode {
int Data;
int Height;
AVLTree Left;
AVLTree Right;
}AVLNode;
//得到树高
int GetHeight(AVLTree T) {
if (!T)
return -1;
else
return T->Height;
}
//返回左右子树中最高的
int Max(int a, int b) {
return (a > b) ? a : b;
}
//单左旋
AVLTree SingleLeft(AVLTree K) {
AVLTree Tmp;
Tmp = K;
K = K->Left;
Tmp->Left = K->Right;
K->Right = Tmp;
Tmp->Height = Max(GetHeight(Tmp->Left), GetHeight(Tmp->Right)) + 1;
K->Height = Max(GetHeight(K->Left), GetHeight(K->Right)) + 1;
return K;
}
//单右旋
AVLTree SingleRight(AVLTree K) {
AVLTree Tmp;
Tmp = K;
K = K->Right;
Tmp->Right = K->Left;
K->Left = Tmp;
Tmp->Height = Max(GetHeight(Tmp->Left), GetHeight(Tmp->Right)) + 1;
K->Height = Max(GetHeight(K->Left), GetHeight(K->Right)) + 1;
return K;
}
AVLTree DoubleLeft(AVLTree K) {
K->Left = SingleRight(K->Left);
return SingleLeft(K);
}
AVLTree DoubleRight(AVLTree K) {
K->Right = SingleLeft(K->Right);
return SingleRight(K);
}
//因插入使二叉排序树失去平衡,则作平衡旋转处理
AVLTree Insert(int X, AVLTree T) {
if (!T) {//是空树
T = (AVLTree)malloc(sizeof(struct AVLNode));
T->Data = X;
T->Height = 0;
T->Left = T->Right = NULL;
}
else if (X < T->Data) {//在左子树中搜索
T->Left = Insert(X, T->Left);
if (GetHeight(T->Left) - GetHeight(T->Right) == 2) {//左高于右2,不平衡
if (X < T->Left->Data)
T = SingleLeft(T);
else
T = DoubleLeft(T);
}
}
else if (X > T->Data) {
T->Right = Insert(X, T->Right);
if (GetHeight(T->Right) - GetHeight(T->Left) == 2) {//右高于左2
if (X > T->Right->Data)
T = SingleRight(T);
else
T = DoubleRight(T);
}
}
T->Height = Max(GetHeight(T->Left), GetHeight(T->Right)) + 1;
return T;
}
int main(void) {
AVLTree T = NULL;
int n;
cin>>n;
int avl[n];
for(int i=0;i<n;i++){
cin>>avl[i];
T=Insert(avl[i], T);
}
if (T)
cout<<T->Data;
return 0;
}