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
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct TNode *PtrToTNode;
typedef PtrToTNode Tree;
struct TNode{
int data;
Tree left;
Tree right;
};
Tree LeftSingleRotation(Tree T1); //四种旋转
Tree RightSingleRotation(Tree T1);
Tree LeftRightRotation(Tree T1);
Tree RightLeftRotation(Tree T1);
int GetHeight(Tree T1);
Tree Insert(int n,Tree T1) //后续输入节点依次插入并做平衡因子判断
{
if(!T1)
{
T1 = (Tree)malloc(sizeof(struct TNode));
T1->left = nullptr;
T1->right = nullptr;
T1->data = n;
T1->height = 1;
}
else if(T1->data>n)
{
T1->left = Insert(n, T1->left);
if(GetHeight(T1->left)-GetHeight(T1->right)==2) //左子树麻烦节点出现
{
if(n<T1->left->data)//左左
{
T1 = LeftSingleRotation(T1);
}
else if(n>T1->left->data)//左右
{
T1 = LeftRightRotation(T1);
}
}
}
else if(T1->data<n)
{
T1->right = Insert(n, T1->right);
if(GetHeight(T1->right)-GetHeight(T1->left)==2) //右子树麻烦节点出现
{
if(n>T1->right->data)//右右
{
T1 = RightSingleRotation(T1);
}
else if(n< T1->right->data)//右左
{
T1 = RightLeftRotation(T1);
}
}
}
return T1;
}
Tree BuildTree(int n) //第一个数据用于建树
{
Tree T1;
T1 = (Tree)malloc(sizeof(struct TNode));
T1->data = n;
T1->left = nullptr;
T1->right = nullptr;
return T1;
}
int GetHeight(Tree T1) //递归求树高用作判断平衡依据
{
int hl, hr,h;
if(!T1)
return 0;
else
{
hl = GetHeight(T1->left);
hr = GetHeight(T1->right);
h = max(hl, hr);
return h + 1;
}
}
Tree LeftSingleRotation(Tree A)
{
Tree B;
B = A->left;
A->left = B->right;
B->right = A;
//根结点和根节点的左子树高度发生变化,其余不变
return B;//B成为原A树的根结点
}
Tree RightSingleRotation(Tree A)
{
Tree B;
B = A->right;
A->right = B->left;
B->left = A;
return B;
}
Tree LeftRightRotation(Tree A)
{
Tree B, C;
B = A->left;
C = B->right;
A->left = C->right;
B->right = C->left;
C->right = A;
C->left = B;
return C;
}
Tree RightLeftRotation(Tree A)
{
Tree B, C;
B = A->right;
C = B->left;
A->right = C->left;
B->left = C->right;
C->left = A;
C->right = B;
return C;
}
int max(int a,int b)
{
return a > b ? a : b;
}
int main()
{
int n,da;
Tree T;
//cin >> n;
scanf("%d", &n);
//cin >> da;
scanf("%d", &da);
T = BuildTree(da);
for (int i = 1; i < n;i++)
{
scanf("%d", &da);
T = Insert(da, T);
}
printf("%d", T->data);
system("pause");
return 0;
}