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. Spring AOP创建BeforeAdvice和AfterAdvice实例

    BeforeAdvice 1.会在目标对象的方法执行之前被调用. 2.通过实现MethodBeforeAdvice接口来实现. 3.该接口中定义了一个方法即before方法,before方法会在目标对 ...

  2. 单例模式的双重锁为什么要加volatile(转)

    单例模式如下: 需要volatile关键字的原因是,在并发情况下,如果没有volatile关键字,在第5行会出现问题. instance = new TestInstance();可以分解为3行伪代码 ...

  3. Android目前流行三方数据库ORM分析及对比

    Android 平台上的数据库框架非常多,但是有一个共同特点就是基于对象关系映射(ORM)模型的.实现的目标也都是不需要写SQL语句,通过对对象的操作保存和操作数据.要是从语法的简洁性来说都有自己的特 ...

  4. python 笔记一

    1. is 和 ==区别 is 判断是否是一个ID(内存中的数据是否是同一个), == 判断内容是否一致. 2.python 常量池包括 1.短整型的-5~256 2.字符串的数字.大小写字母随意组合 ...

  5. expect脚本远程登录、远程执行命令和脚本传参简单用法

    expect介绍: 最近想写一个自动化安装脚本,涉及到远程登录.分发文件包.远程执行命令等,其中少不了来回输入登录密码,交互式输入命令等,这样就大大降低了效率,那么有什么方法能解决呢?不妨试试expe ...

  6. SQL SERVER-日期按时区转换

    SELECT SWITCHOFFSET('2019-07-19 08:35:06.637','+08:00')

  7. PCM时序

    PCM(Pulse Code Modulation),脉冲编码调制,PCM总线用于传输数字语音信号,包括4根信号线:FSYNC(同步)/PCLK(时钟)/DTX(发送)/DRX(接收) PCM分为Ma ...

  8. Go数据类型之复合数据类型--Slice

    3.2 Slice 一个slice是一个轻量级的数据结构,提供了访问数组子序列(或者全部)元素的功能,而且slice的底层确实引用一个数组对象. 一个slice由三个部分构成:指针.长度和容量.指针指 ...

  9. 2013.4.30 - KDD第十二天

    早上来实验室,本来打算向秦师兄要文献的,不过秦师兄上午不在,所以就没有联系他.于是就开始调试郑茂的代码,发现原来那个itoa函数不是标准库里面 的,所以可能只有windows上可以用.然后我就在打电脑 ...

  10. python3_pygame游戏窗口创建

    python3利用第三方模块pygame创建游戏窗口 步骤1.导入pygame模块 步骤2.初始化pygame模块 步骤3.设置游戏窗口大小 步骤4.定义游戏窗口背景颜色 步骤5.开始循环检测游戏窗口 ...