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 (≤) 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

题目分析:写一个AVL树 背课文是好的 但也要理解清除AVL树的原理 之后还要学习各种平衡树 如红黑树什么的
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
typedef struct TNode* Tree;
struct TNode {
int Data;
Tree TL;
Tree TR;
int Height; //要初始化为-1;
};
int GetHeight(Tree T) {
if (T)
return T->Height;
else
return -;
}
Tree singleLeftRotate(Tree T) {
Tree TL = T->TL;
T->TL = TL->TR;
TL->TR = T;
T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + ;
TL->Height = max(GetHeight(TL->TL), GetHeight(TL->TR)) + ;
return TL;
}
Tree singleRightRotate(Tree T) {
Tree TR = T->TR;
T->TR = TR->TL;
TR->TL = T;
T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + ;
TR->Height = max(GetHeight(TR->TL), GetHeight(TR->TR)) + ;
return TR;
}
Tree doubleLeftRightRotate(Tree T) {
T->TL = singleRightRotate(T->TL);
return singleLeftRotate(T);
}
Tree doubleRightLeftRotate(Tree T) {
T->TR = singleLeftRotate(T->TR);
return singleRightRotate(T);
}
Tree Insert(Tree T,int data) {
if (!T)
{
T = new TNode();
T->Data = data;
T->Height = ;
T->TL = T->TR = NULL;
}
else if (data > T->Data) {
T->TR = Insert(T->TR, data);
T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + ;
if (GetHeight(T->TR) - GetHeight(T->TL) == ) {
if (data > T->TR->Data)
T=singleRightRotate(T);
else
T=doubleRightLeftRotate(T);
}
}
else {
T->TL = Insert(T->TL, data);
T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + ;
if (GetHeight(T->TL) - GetHeight(T->TR) == ) {
if (data < T->TL->Data)
T=singleLeftRotate(T);
else
T=doubleLeftRightRotate(T);
}
}
return T;
} int main()
{
int N;
Tree T = NULL;
int data;
cin >> N;
for (int i = ; i < N; i++)
{
cin >> data;
T = Insert(T, data);
}
cout << T->Data;
}

最新文章

  1. 14. Longest Common Prefix
  2. QT常用资料
  3. Ubuntu使用ApkTool进行APK反编译
  4. 【转】赞一下huicpc035
  5. SharpGL学习笔记(十二) 光源例子:解决光源场景中的常见问题
  6. C# Excel写入
  7. web 性能忧化(IIS篇)
  8. mvc web api 保存多个实体类的方法
  9. HTML5会砸掉iOS和Android开发者的饭碗么?
  10. 从运行原理及使用场景看Apache和Nginx
  11. 乐酷工作室孙志伟:Testin云測试有广度有深度 省钱省力值得信赖
  12. [转]:如何使用Android Studio把自己的Android library分享到jCenter和Maven Central
  13. HA集群heartbeat配置--Nginx
  14. Spring Boot 2.x 启动全过程源码分析(上)入口类剖析
  15. centos7 安装python2.7与3共存
  16. springMVC参数传递实例
  17. Linux 的伪终端的基本原理 及其在远程登录(SSH,telnet等)中的应用
  18. 深入浅出 Java Concurrency (8): 加锁的原理 (Lock.lock)
  19. 重温CLR(八 ) 泛型
  20. jsp页面的传值(list)

热门文章

  1. Python操作系统
  2. 如何在国内离线安装Chrome扩展并科学查资料
  3. Excel 电子表格中,快速修改表格中的数值
  4. (转)协议森林12 天下为公 (TCP堵塞控制)
  5. 建议8:恰当选用if和switch
  6. 2016 Multi-University Training Contest 1 T4
  7. 【Weiss】【第03章】队列例程
  8. Java序列化和反序列化-(新手)
  9. oracle的sql语句优化
  10. CentOs安装配置Jenkins(一)