# Root of AVL Tree

7-6 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

#include<iostream>
using namespace std;

typedef struct Node* Tree;
struct Node
{
int val;
struct Node* left;
struct Node* right;
int H;  //本子树的高度(深度，层数）
};

Tree NewNode(int val)
{
Tree T = new struct Node;
T->left = T->right = NULL;
T->val = val;	T->H = 1;
return T;
}

int Max(int a, int b)
{
return a > b ? a : b;
}

/*
Algorithm:

height(T)
1. 如果树为空，则返回0
2. 否则
(a) 通过递归地调用height(T.left)求出左子树的高度
(b) 通过递归的调用height(T.right)求出右子树的高度
(c) 利用如下公式求出二叉树的高度：
height(T) = max(height(T.left),  height(T.right)) + 1
(d) 返回height(T)
*/

/*
int Height(Tree Root)
{
if(!Root) return 0;
else
{
lHeight=Height(Root->left);
rHeight=Height(Root->right);
return lHeight>rHeight?lHeight+1:rHeight+1;
}
}
*/

int GetHeight(Tree Root)
{
if (!Root) return 0;
if (!Root->left && !Root->right) return 1;
if (Root->left && !Root->right) return GetHeight(Root->left) + 1;
if (!Root->left && Root->right) return GetHeight(Root->right) + 1;
return Max(GetHeight(Root->left), GetHeight(Root->right)) + 1;
}

bool isBalanced(Tree left, Tree right)
{
int lh = GetHeight(left);
int rh = GetHeight(right);
if ((lh > rh + 1) || (rh > lh + 1)) return false;
else return true;
}

Tree LLrotate(Tree Root)  //LL
{
Tree newRoot = Root->left;
Root->left = newRoot->right;  //根结点的左子树变成了左孩子的右子树
newRoot->right = Root;  //左孩子的右子树变成了根结点，左孩子变为根结点

Root->H = Max(GetHeight(Root->left), GetHeight(Root->right)) + 1;
newRoot->H = Max(GetHeight(newRoot->left), GetHeight(newRoot->right)) + 1;
return newRoot;
}

Tree RRrotate(Tree Root)  //RR
{
Tree newRoot = Root->right;
Root->right = newRoot->left;
newRoot->left = Root;

Root->H = Max(GetHeight(Root->left), GetHeight(Root->right)) + 1;
newRoot->H = Max(GetHeight(newRoot->left), GetHeight(newRoot->right)) + 1;
return newRoot;
}

Tree LRrotate(Tree Root)  //LR:左孩子进行RR，然后根结点进行LL
{
Root->left = RRrotate(Root->left);
Root = LLrotate(Root);
return Root;
}

Tree RLrotate(Tree Root)  //RL:右孩子进行LL（向右转），然后根结点进行RR（向左转）
{
Root->right = LLrotate(Root->right);
Root = RRrotate(Root);
return Root;
}

Tree Insert(Tree Root, int val)
{
if (!Root) Root = NewNode(val);
else
{
if (val < Root->val)  //val插在左子树
{
Root->left = Insert(Root->left, val);
if (!isBalanced(Root->left, Root->right))  //如果Root左右不平衡(左子树高）
{
if (val < Root->left->val)  //val插在左孩子的左子树上，则进行左单旋转,LL（向右转）
Root = LLrotate(Root);
else  //val插在左孩子的右子树上,LR
Root = LRrotate(Root);
}
}
else if (val > Root->val)  //val插在右子树
{
Root->right = Insert(Root->right, val);
if (!isBalanced(Root->left, Root->right))  //如果Root左右不平衡(右子树高）
{
if (val > Root->right->val)  //val插在右孩子的右子树上，则进行右单旋转,RR（向左转）
Root = RRrotate(Root);
else  //val插在右孩子的左子树上,RL
Root = RLrotate(Root);
}
}
}
Root->H = 1 + Max(GetHeight(Root->left), GetHeight(Root->right));
return Root;
}

Tree MakeAVLTree(int n)
{
int val;
cin >> val;
Tree Root = NewNode(val);
n--;
while (n--)
{
cin >> val;
Root = Insert(Root, val);
}
return Root;
}

int main()
{
int n;
cin >> n;
Tree T = MakeAVLTree(n);
cout << T->val << endl;

return 0;
}

08-13 1391

12-07 413
11-18 212
01-29 450
10-30 456