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 Ndistinct 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<cstdio>
#include<algorithm>
using namespace std;
struct Node{
int v,height;
Node* lchild,*rchild;
}*root; Node* newNode(int v){
Node* node = new Node;
node -> v = v;
node -> lchild = node -> rchild = NULL;
node -> height = ;
return node;
} int getHeight(Node* root){
if(root == NULL) return ;
return root -> height;
} void updateHeight(Node* root){
root -> height = max(getHeight(root -> lchild),getHeight(root -> rchild))+;
} int getBalanceFactor(Node* root){
return getHeight(root -> lchild) - getHeight(root -> rchild);
} void R(Node* &root){
Node* temp = root -> lchild;
root -> lchild = temp -> rchild;
temp -> rchild = root;
updateHeight(root);
updateHeight(temp);
root = temp;
}
void L(Node* &root){
Node* temp = root -> rchild;
root -> rchild = temp -> lchild;
temp -> lchild = root;
updateHeight(root); //先更新root(子树)的高度
updateHeight(temp);
root = temp;
} void insert(Node* &root, int v){
if(root == NULL){
root = newNode(v);
return;
}
if(v < root -> v){
insert(root -> lchild,v);
updateHeight(root);
if(getBalanceFactor(root) == ){
if(getBalanceFactor(root -> lchild) == ){
R(root);
}else if(getBalanceFactor(root -> lchild) == -){
L(root -> lchild);
R(root);
}
}
}else{
insert(root -> rchild,v);
updateHeight(root);
if(getBalanceFactor(root) == -){
if(getBalanceFactor(root -> rchild) == -){
L(root);
}else if(getBalanceFactor(root -> rchild) == ){
R(root -> rchild);
L(root); }
}
}
} int main(){
int n,v;
scanf("%d",&n);
for(int i = ; i < n; i++){
scanf("%d",&v);
insert(root,v);
}
printf("%d",root -> v);
return ;
}


1066 Root of AVL Tree (25)的更多相关文章

  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)

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

  3. 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 ...

  4. PAT Advanced 1066 Root of AVL Tree (25) [平衡⼆叉树(AVL树)]

    题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...

  5. 1066. 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 ...

  6. PAT 1066. 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 ...

  7. 1066 Root of AVL Tree (25分)(AVL树的实现)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

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

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

  9. PAT (Advanced Level) 1066. Root of AVL Tree (25)

    AVL树的旋转.居然1A了.... 了解旋转方式之后,数据较小可以当做模拟写. #include<cstdio> #include<cstring> #include<c ...

随机推荐

  1. ByteBuf源码

    ByteBuf是顶层的抽象类,定义了用于传输数据的ByteBuf需要的方法和属性. AbstractByteBuf 直接继承ByteBuf,一些公共属性和方法的公共逻辑会在这里定义.例如虽然不同性质的 ...

  2. C#使用共享内存与C++进行数据交互

    现在做桌面的不多了.前端太流行了,大家都去搞前端了. 需求如下: 上层UI使用C#开发,数据采集模块使用C++开发.数据采集模块采集到的数据比较大,上层需要接收这一块数据并显示 进程间通信的方式有多种 ...

  3. java之单元测试

    这篇主要简单讲下java的单元测试 目录结构如下: 如图,其中1是需要被测试的功能:2是测试模块:3是单元测试需要的引入包: 1. 功能模块1中 Calculator 的代码: package cn. ...

  4. ADO.NET 二(Connection)

    C# 语言中 Connection 类是 ADO.NET 组件连接数据库时第一个要使用的类,也是通过编程访问数据库的第一步. 接下来了解一下 Connection 类中的常用属性和方法,以及如何连接 ...

  5. D1-JavaScript

    下面的代码,我想要打印出hey jack,结果却打印出hey rose,为什么? function greet(person) { if (person == {name: 'jack'}) { co ...

  6. 爬虫之PyQuery的base了解

    爬虫之PyQuery的base了解 pyquery库是jQuery的Python实现,能够以jQuery的语法来操作解析 HTML 文档,易用性和解析速度都很好,和它差不多的还有BeautifulSo ...

  7. keil5工程移植到IAR工程

    keil5工程移植到IAR工程 一. 软件版本 MDK-ARM Professional  Version: 5.14.0.0 IAR 8.1 移植工程:基于正点原子开发板建立的STM32F407ZG ...

  8. Linux GRUB手动安装方法详解

    需要手工安装 GRUB 主要有两种情况: Linux 系统原先不是使用 GRUB 作为引导程序而现在想要使用 GRUB 来作为引导程序: MBR 中的引导程序被覆盖,需要在 MBR 中重新安装 GRU ...

  9. vue-cli3.0 脚手架搭建项目的过程详解

    1.安装vue-cli 3.0 ? 1 2 3 npm install -g @vue/cli # or yarn global add @vue/cli 安装成功后查看版本:vue -V(大写的V) ...

  10. 适合公司和个人的目标管理方法:OKR!

    1.定义   OKR就是Objectives and Key Results的简称,包括目标(Objectives)和关键结果(Key Results)两个要素.   2.目的    就公司和团队而言 ...