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. 树莓派(Raspberry Pi 3) 使用wifi模块连接网络

    树莓派3B内置了wifi和蓝牙模块,启动WIFI模块有两种方式,一种是图形界面,一种是命令行模式. 使用图形界面: 在桌面右上角的菜单栏里面选择wifi,输入密码就可以了. 使用命令行: 第一步:配置 ...

  2. @RequestMapping与@ModelAttribute 套路

    新接触一个项目,使用了大量注解: 在通过请求路径查看时一直找不到页面的跳转,再查看了文件内所有方法与注解后才找到对应的路径,特此记下: @ModelAttribute("totalfinal ...

  3. C#中控制线程池的执行顺序

    在使用线程池时,当用线程池执行多个任务时,由于执行的任务时间过长,会导制两个任务互相执行,如果两个任务具有一定的操作顺序,可能会导制不同的操作结果,这时,就要将线程池按顺序操作.下面先给一段代码,该代 ...

  4. CSS预处理器(SASS和LESS)

    Sass框架应用Sass简介 Sass又名SCSS,是CSS预处理器之一,它能让你更好更轻松的工作.Sass官网是这样描述Sass的:**Sass是一门高于CSS的元语言,能用来清晰的.结构化地描述文 ...

  5. docker,mysql,Navicat

    Navicat破解网址  https://www.jianshu.com/p/5f693b4c9468 docker pull mysql docker run -d -p 3306:3306 --n ...

  6. 等和的分隔子集(DP)

    晓萌希望将1到N的连续整数组成的集合划分成两个子集合,且保证每个集合的数字和是相等.例如,对于N=3,对应的集合{1,2,3}能被划分成{3} 和 {1,2}两个子集合. 这两个子集合中元素分别的和是 ...

  7. [Django笔记] admin 深入学习

    admin django 内置的管理后台,大部分时候可以通过对admin进行配置来提高开发效率. 数据列表展示 默认情况下显示一个models-objects的列表,如果model定义了 __str_ ...

  8. 【转】.net算术运算导致溢出

    源地址:http://blog.csdn.net/hawksoft/article/details/70470136

  9. ubuntu下中文乱码解决

    这个方法只对该用户有效. 方法二:修改/etc/environment,增加以下内容: LANGUAGE=”zh_CN:zh:en_US:en” LANG=zh_CN.GBK

  10. freemarker macro 使用

    转载... macro, nested, return语法 <#macro name param1 param2 ... paramN>...<#nested loopvar1, l ...