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. 《MFC游戏开发》笔记六 图像双缓冲技术:实现一个流畅的动画

    本系列文章由七十一雾央编写,转载请注明出处.  http://blog.csdn.net/u011371356/article/details/9334121 作者:七十一雾央 新浪微博:http:/ ...

  2. (转)Git Gui for Windows的建库、克隆(clone)、上传(push)、下载(pull)、合并

    原文地址: http://blog.csdn.net/fym0512/article/details/7713006 本教程将讲述:gitk的Git Gui的部分常用功能和使用方法,包括:建库.克隆( ...

  3. html使用空格对齐文本(&nbsp;&emsp;&ensp;)

    字符以及HTML实体 描述以及说明   这是我们使用最多的空格,也就是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的(只算1个).要使用html实体表示才可累加. ...

  4. MongoDB - MongoDB CRUD Operations

    CRUD operations create, read, update, and delete documents. Create Operations Create or insert opera ...

  5. 自定义View(二)ViewPage广告轮播

    自定义View的第二个学习案例,使用ViewPage实现广告轮播,通过组合现有的View实现效果如下: 有关ViewPage使用可以学习谷歌官方API,和训练案例: 1.使用ViewPage实现屏幕滑 ...

  6. 六、Android学习笔记_JNI_c调用java代码

    1.编写native方法(java2c)和非native方法(c2java): package com.example.provider; public class CallbackJava { // ...

  7. (转)Android之自定义适配器

    ListView作为一个实际开发中使用率非常高的视图,一般的系统自带的适配器都无法满足开发中的需求,这时候就需要开发人员来自定义适配器使得ListView能够有一个不错的显示效果. 有这样一个Demo ...

  8. linux 在xenserver上安装如何显示图形界面

    centos5.8 64-bit和 centos 6.5 64-bit xenserver安装linux的时候默认使用的VHM,选择对应的虚拟机模板安装linux是Linux Text界面. VHM ...

  9. 网络请求的null值处理

    最近项目中经常有遇到从服务器请求的数据是null的情况,这种情况下如果用[dic objectForKey:@"key"]方法,程序会发生崩溃现象,因为项目是以前的老项目,而且有太 ...

  10. "Could not load file or assembly 'DTcms.Web.UI' or one of its dependencies. 拒绝访问。" 的解决办法

    出现的问题提示如下: