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<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->height = ;
Node->lchild = Node->rchild = NULL;
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);
updateHeight(temp);
root = temp;
} void insert(node* &root,int v){
if(root == NULL){
root = newNode(v);
return;
}
if(root->v > 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 ;
}

04-树5 Root of AVL Tree (25 分)的更多相关文章

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

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

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

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

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

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

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

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

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

  10. pat1066. Root of AVL Tree (25)

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

随机推荐

  1. Makefile 编写规则 - 1

    Makefilen内容 1. 显示规则:显示规则说明了,如何生成一个或多个目标.这是由Makefile指出要生成的文件和文件依赖的文件.2. 隐晦规则:基于Makefile的自动推导功能3. 变量的定 ...

  2. Vue 与Angular、React框架的对比

    首先,我们先了解什么是MVX框架模式? MVX框架模式:MVC+MVP+MVVM 1.MVC:Model(模型)+View(视图)+controller(控制器),主要是基于分层的目的,让彼此的职责分 ...

  3. WebDriverWait等设置等待时间和超时时间

    1.显示等待 等待页面加载完成,找到某个条件发生后再继续执行后续代码,如果超过设置时间检测不到则抛出异常 WebDriverWait(driver, timeout, poll_frequency=0 ...

  4. SpringMVC——RequestMapping

    一.@RequestMapping 映射请求 Spring MVC 通过@RequestMapping注解可以定义不同的处理器映射规则. @RequestMapping放在类名上边,设置请求前缀 方法 ...

  5. 用JQuery获取输入框中的光标位置

    (function ($, undefined) { $.fn.getCursorPosition = function () { var el = $(this).get(0); var pos = ...

  6. Responsive设计——meta标签

    media-queries.js(http://code.google.com/p/css3-mediaqueries-js/) respond.js(https://github.com/scott ...

  7. 如何偷Android的内存-Tricking Android MemoryFile

    之前在做一个内存优化的时候,使用到了MemoryFile,由此发现了MemoryFile的一些特性以及一个非常trickly的使用方法,因此在这里记录一下 What is it MemoryFile是 ...

  8. javascript总结8:JavaScript 类型转换

    1 JavaScript 数据类型转换 1.1 数字类型转字符串 n1 = 10;var n2 =String(n1); 或者 var n3 = n1.toString(n1); 1.2 字符串转数字 ...

  9. 命令(Command)模式

    命令(Command)模式:命令模式是对命令的封装.命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象 /* * 客户(Client)角色:创建了一个具体命令(ConcreteComma ...

  10. DB2 添加license

    DB2 - DB2COPY1 - DB2-0 服务不能启动报的错是这样的:Microsoft Management Console   Windows 不能在 本地计算机 启动 DB2 - DB2.有 ...