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;
}

1066 Root of AVL Tree (25分)(AVL树的实现)的更多相关文章

  1. PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

    1066 Root of AVL Tree (25 分)   An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...

  2. PAT甲级:1066 Root of AVL Tree (25分)

    PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...

  3. PTA 04-树5 Root of AVL Tree (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree   (25分) An AVL tree ...

  4. 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 sub ...

  5. 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)

    题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...

  6. pat 甲级 1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  7. 04-树4. Root of AVL Tree (25)

    04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  8. pat1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  9. pat04-树4. Root of AVL Tree (25)

    04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An A ...

随机推荐

  1. sonarqube配置全指南,集成阿里巴巴p3c规范

    环境准备 内置数据库 Sonar安装成功后,默认内置H2数据库,用于记录单次的扫描结果,对同一个project重复扫码,会覆盖之前的扫描记录,所以H2 数据库只应用于测试,不可以用于生产环境,那如果你 ...

  2. 关于PHP命名空间的讨论

    什么是命名空间? 根据php.net官方翻译文档描述,命名空间是这样定义的: 什么是命名空间?从广义上来说,命名空间是一种封装事物的方法. 在PHP中,命名空间用来解决在编写类库或应用程序时创建可重用 ...

  3. Java探针技术-retransformclasses的介绍

    retransformclasses void retransformclasses(class... classes) throws unmodifiableclassexception 重转换提供 ...

  4. 树莓派上搭建唤醒词检测引擎 Snowboy

    Snowboy 是一款高度可定制的唤醒词检测引擎,可以用于实时嵌入式系统,并且始终监听(即使离线).当前,它可以运行在 Raspberry Pi.(Ubuntu)Linux 和 Mac OS X 系统 ...

  5. python第一次作业

    import turtle turtle.pensize(2) turtle.pencolor("black") turtle.fillcolor("red") ...

  6. Vue2.0 【第三季】第1节 propsData Option 全局扩展的数据传递

    目录 Vue2.0 [第三季]第1节 propsData Option 全局扩展的数据传递 第1节 propsData Option 全局扩展的数据传递 Vue2.0 [第三季]第1节 propsDa ...

  7. go源码分析(三) 使用go http包开发web时遇到的坑之卸载插件,重启插件管理,仍然可以访问已经卸载的插件

    问题描述: web页面下发重启指令后,对卸载插件的处理不完整(虽然列表已经没有插件描述,但是在HandleFunc的路由列表中依然存在) 我们需要清空路由列表map 路由列表结构见代码: net/ht ...

  8. 《JavaScript 模式》读书笔记(2)— 基本技巧3

    这是基本技巧的最后一篇内容,这篇内容示例代码并不多.主要是概念比较多一点. 编码约定 确定并一致遵循约定比这个具体约定是什么更为重要. 一.缩进 无论是使用tab还是空格,只要是一致遵循的,是什么并不 ...

  9. 【Weiss】【第03章】增补附注

    基本上每章到增补附注这里就算是结束了. 根据设想,每章的这一篇基本上会注明这一章哪些题没有做,原因是什么,如果以后打算做了也会在这里补充. 还有就是最后会把有此前诸多习题的代码和原数据结构放整理后,以 ...

  10. Java-用集合存储对象(新手)

    //导入的包.import java.util.ArrayList;//用集合存储对象,遍历集合,取所有元素. 用get方法.//创建的一个类.public class zylx4 { //公共静态的 ...