1066. Root of AVL Tree (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
struct AVLtreenode{
int h,v;
AVLtreenode *l,*r;
};
#define max(a,b) (a>b?a:b)
int GetHeight(AVLtreenode *root){
if(!root){
return ;
}
return root->h;
}
AVLtreenode* AVLRightRotation(AVLtreenode* root){
AVLtreenode* temp=root->r;
root->r=temp->l;
temp->l=root;
root->h=max(GetHeight(root->l),GetHeight(root->r))+;
temp->h=max(GetHeight(temp->r),GetHeight(root))+;
return temp;
}
AVLtreenode* AVLLeftRotation(AVLtreenode* root){
AVLtreenode* temp=root->l;
root->l=temp->r;
temp->r=root;
root->h=max(GetHeight(root->l),GetHeight(root->r))+;
temp->h=max(GetHeight(temp->l),GetHeight(root))+;
return temp;
}
AVLtreenode* AVLRightLeftRotation(AVLtreenode* root){
root->r=AVLLeftRotation(root->r);
root=AVLRightRotation(root);
return root;
}
AVLtreenode* AVLLeftRightRotation(AVLtreenode* root){
root->l=AVLRightRotation(root->l);
root=AVLLeftRotation(root);
return root;
}
AVLtreenode* AVLInsert(int num,AVLtreenode *root){
if(!root){ //cout<<1<<endl; root=new AVLtreenode();
root->h=;
root->l=root->r=NULL;
root->v=num;
return root;
}
//cout<<2<<endl;
if(root->v>num){//插入左子树
root->l=AVLInsert(num,root->l);
if(GetHeight(root->l)-GetHeight(root->r)==){//需要左旋
if(root->l->v>num){//单左旋
root=AVLLeftRotation(root);
}
else{//左右旋
root=AVLLeftRightRotation(root);
}
}
}
else{
root->r=AVLInsert(num,root->r);
if(GetHeight(root->r)-GetHeight(root->l)==){//
if(root->r->v<num){//
root=AVLRightRotation(root);
}
else{//
root=AVLRightLeftRotation(root);
}
}
}
root->h=max(GetHeight(root->l),GetHeight(root->r))+;
return root;
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i,num;
AVLtreenode *root=NULL;
for(i=;i<n;i++){
scanf("%d",&num); //cout<<"i: "<<i<<endl; root=AVLInsert(num,root);
}
cout<<root->v<<endl;
return ;
}

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Mac下的UI自动化测试 (三)

    使用sikuli进行UI自动化测试固然是方便很多,不用一切都使用AppleScript那烦人的语法,只要界面的UI没有变化,结构的变化不会影响到基于sikuli的自动化,但是基于AppleScript ...

  2. 抽象类(abstract)【转】

    抽象类(abstract) abstract修饰符可以和类.方法.属性.索引器及事件一起使用.在类声明中使用abstract修饰符以指示某个类只能是其它类的基类.标记为抽象或包含在抽象类中的成员必须通 ...

  3. 关于C# WinForm_Tree View的一些基本用法(摘抄)

    下面是treeview的用法TreeView组件是由多个类来定义的,TreeView组件是由命名空间"System.Windows .Forms"中的"TreeView& ...

  4. Failed to export using the options you specified. Please check your options and try again

    参考这篇<从ASP.NET传递参数给水晶报表> http://www.cnblogs.com/insus/p/3281114.html  是可以传递参了.但是点击报表的菜单条上的打印图标没 ...

  5. Android Android环境安装

    Android环境安装 一.下载: 所需:eclipse.sdk.jdk.adt 参考 工具下载地址:http://www.androiddevtools.cn/ 二安装: 1.eclipse: 选择 ...

  6. Puppeteer入门初探

    本文来自网易云社区 作者:唐钊 最近在看 node 爬虫相关的一些东西,我记得还是很久以前常用的 node 爬虫工具还是 superagengt+cherrio,他们的思路是通过发起 http 请求然 ...

  7. Linq to Objects for Java

    好几年不写博客了,人也慢慢变懒了.然而想写了却不知道写点啥,正好最近手头有点小项目就分享一下经历. 现在 java 的大环境下,基本都是围着 spring 转,加上一堆其他的库.有了架子就开始搞业务了 ...

  8. IMP-00003: 遇到 ORACLE 错误 959 ORA-00959: 表空间 '' 不存在

    描述 在使用imp命令将dmp文件导入oracle中时,遇到如下错误: IMP: 遇到 ORACLE 错误 ORA: 表空间 'TBS_CDUSER' 不存在 IMP命令如下: IMP cduser/ ...

  9. ThinkPHP3.2.3完整版创建前后台入口文件 http://jingyan.baidu.com/article/7e4409533fc1092fc1e2ef53.html

    ThinkPHP3.2.3完整版创建前后台入口文件   1 2 3 4 5 6 7 分步阅读 ThinkPHP是为了简化企业级应用开发和敏捷WEB应用开发而诞生的优秀的国产php框架,值得我们去探索学 ...

  10. sql_trace基本用法

    sql_trace是oracle提供的一个非常好的跟踪工具,主要用来检查数据库的异常情况,通过跟踪数据库的活动,找到有问题的语句. 一.概述:    SQL_TRACE是Oracle的一个非常强大的工 ...