pat1066. 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, 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)的更多相关文章
- 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 ...
- 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 ...
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 【C#】 通过http webRequest方式调用webservice
上一篇文章写了关于webservice的调用部署等内容,在上一篇文章中,我们通过微软给的方式--添加[WebReference服务引用]来进行调用如下图 那今天,我们说的是通过webrequest的方 ...
- 求解热传导方程matlab
这是非稳态一维热传导的方法,也叫古典显格式. 如果是做数学建模,就别用了,这种方法计算量比较大,算的很慢,而且收敛不好. 但是如果实在没办法也能凑合用. 该改的地方我都用???代替了. 给个详细解释h ...
- Iterator 遍历器
1.遍历器(Iterator)是一种接口,为各种不同的数据结构提供统一的访问机制.任何数据结构只要部署Iterator接口,就可以完成遍历操作(即依次处理该数据结构的所有成员). 2.Iterator ...
- 【Linux】-Ubuntu常用命令吐血整理
前言 刚刚接触Linux操作系统,真的是各种艰难啊,用个什么东西都得从头开始配置,这个时候才明白从头再来是什么滋味了.自己装了数个数十几次的Centos版本的Linux系统,好不容易争气了一次,跑了起 ...
- kuangbin专题16I(kmp)
题目链接: https://vjudge.net/contest/70325#problem/I 题意: 求多个字符串的最长公共子串, 有多个则输出字典序最小的. 思路: 这里的字符串长度固定为 60 ...
- linux下redis的安装与django-redis使用方法
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set( ...
- Python之‘数据结构’
简介 数据结构基本上就是--它们是可以处理一些数据的结构.或者说,它们是用来存储一组相关数据的.在Python里面有三种内建的数据结构--列表.元组和字典. 一.列表 list是处理一组有序项目的数据 ...
- kuangbin专题十二 HDU1114 Piggy-Bank (完全背包)
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- nginx的使用(启动、重启、关闭)
1. 首先利用配置文件启动nginx. 命令: nginx -c /usr/local/nginx/conf/nginx.conf 重启服务: service nginx restart 2. 快速停 ...
- CF D. Fair(思维+DFS)
http://codeforces.com/contest/987/problem/D 题目大概: 给出一个n个城镇m条边的图,给出每个城镇拥有的特产(可能多个城镇有相同特产).有k种不同特产. 要求 ...