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 ythe 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<stdio.h>
#include<vector>
#include<algorithm>
using namespace std; struct node
{
node(int v):left(NULL),right(NULL),high(),val(v){}
node* left,* right;
int val,high;
}; int gethigh(node* root)
{
int a = , b = ;
if(root->left!= NULL)
a = root->left->high;
if(root->right!= NULL)
b = root->right->high;
return a > b ? a+:b+;
} void R(node* & root)
{
node* tem = root->left;
root->left = tem->right;
tem->right = root;
root->high = gethigh(root);
tem->high = gethigh(tem);
root = tem;
} void L(node* & root)
{
node* tem = root->right;
root->right = tem->left;
tem->left = root;
root->high = gethigh(root);
tem->high = gethigh(tem);
root = tem;
} void insert(node*& root,int val)
{
if(root == NULL)
{
root = new node(val);
return;
} if(val < root->val)
{
insert(root->left,val);
root->high = gethigh(root);
int a = root->left == NULL ? : root->left->high;
int b = root->right == NULL ? : root->right->high;
if(a - b == )
{
int c = root->left->left == NULL ? :root->left->left->high;
int d = root->left->right == NULL ? :root->left->right->high;
if(c - d == )
{
R(root);
}
else if(c - d == -)
{
L(root->left);
R(root);
}
}
}
else
{
insert(root->right,val);
root->high = gethigh(root);
int a = root->left == NULL ? : root->left->high;
int b = root->right == NULL ? : root->right->high;
if(a - b == -)
{
int c = root->right->right == NULL ? :root->right->right->high;
int d = root->right->left == NULL ? :root->right->left->high;
if(c - d == )
{
L(root);
}
else if(c - d == -)
{
R(root->right);
L(root);
}
}
}
} int main()
{
int n,tem;
scanf("%d",&n);
node* Tree = NULL;
for(int i = ;i < n;++i)
{
scanf("%d",&tem);
insert(Tree,tem);
}
printf("%d\n",Tree->val);
return ;
}

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

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

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

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

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

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

  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 (Advanced Level) 1066. Root of AVL Tree (25)

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

  9. PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. iOS数据持久化存储:归档

    在平时的iOS开发中,我们经常用到的数据持久化存储方式大概主要有:NSUserDefaults(plist),文件,数据库,归档..前三种比较经常用到,第四种归档我个人感觉用的还是比较少的,恰恰因为用 ...

  2. 重构17-Extract Superclass(提取父类)

    当一个类有很多方法希望将它们“提拔”到基类以供同层次的其他类使用时,会经常使用该重构.下面的类包含两个方法,我们希望提取这两个方法并允许其他类使用. public class Dog { public ...

  3. C语言中的命名空间

    C语言中的命名空间 命名空间是为了解决 "在相同作用域内如何区分 相同的标识符". 说明: ①只有在相同作用域的情况下才能使用到命名空间去区分标识符,在嵌套的作用域.不同的作用域区 ...

  4. jq使用手册

    jq 使用手册   翻译整理:Young.J 官方网站:http://jquery.com    jQuery是一款同prototype一样优秀js开发库类,特别是对css和XPath的支持,使我们写 ...

  5. Oracle 11g R2 for Win7旗舰版(64位)的安装步骤

    1.下载Oracle 11g R2 for Windows的版本 下载地址:http://www.oracle.com/technetwork/database/enterprise-edition/ ...

  6. CentOS7,Firewalld防火墙使用方法

    查看防火墙状态 systemctl status firewalld.service 启动firewall systemctl stop firewalld.service 停止firewall sy ...

  7. crawler4j:轻量级多线程网络爬虫实例

    crawler4j是Java实现的开源网络爬虫.提供了简单易用的接口,可以在几分钟内创建一个多线程网络爬虫. 下面实例结合jsoup(中文版API),javacvs 爬取自如租房网(http://sh ...

  8. 剑指Offer15 合并两个已排序链表

    /************************************************************************* > File Name: 15_MergeT ...

  9. 揪出ie和Edge的js代码

    var userAgent = navigator.userAgent; var isIE = userAgent.indexOf("compatible") > -1 &a ...

  10. Backbone.js学习之Router

    官方文档的解释: Web applications often provide linkable, bookmarkable, shareable URLs for important locatio ...