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 ...
随机推荐
- sqlserver 时间差转换为天时分秒
DECLARE @starttime DATETIME = '2016-12-01' , @endtime DATETIME = '2016-12-02 14:56:39.927'; DECLARE ...
- Python3中实现简单的购物车程序
product_list = [ ('iphone',5800), ('imac',15800), ('watch',9800), ('cloth',550), ('coffe latee',35), ...
- (原创)E - Straight Shot Gym - 101652R
解题思路:这道题的题意就是给你n,总距离X,速度v:以及n组数据:人行道的左端点和右端点,以及人行道的速度(竖直方向),如果从(0,0)到(X,0)的时间小于2X/v,则输出其时间,否则输出”Too ...
- 【bzoj2751】[HAOI2012]容易题(easy) 数论-快速幂
[bzoj2751][HAOI2012]容易题(easy) 先考虑k=0的情况 那么第一个元素可能为[1,n] 如果序列长度为m-1时的答案是ans[m-1] 那么合并得 然后同理答案就是 k很小 而 ...
- P2723 丑数 Humble Numbers
题意:给你k个质数,定义丑数集合为k个质数随机(1--k)个相乘得到的数 求第n小的丑数 暴力...貌似不太可行,(把所有大量丑数求出来,sort QAQ) 可以想到,对于第i个丑数f[i],它一 ...
- How to grow up as a BA
简书 https://www.jianshu.com/p/8f62b5c7fe1b Thoughtworks https://mp.weixin.qq.com/s/n1hGAM2nUoLvkE5xuU ...
- 雷林鹏分享:jQuery EasyUI 数据网格 - 添加查询功能
jQuery EasyUI 数据网格 - 添加查询功能 本实例演示如何从数据库得到数据,并将它们显示在数据网格(datagrid)中.然后演示如何根据用户输入的搜索关键词搜寻显示结果. 创建数据网格( ...
- powdesigner建表
默认打开powerDesigner时,创建table对应的自动生成sql语句没有注释. 方法1.comment注释信息 在Columns标签下,一排按钮中找到倒数第2个按钮:Customize Col ...
- POJ1052 Plato's Blocks
题目来源:http://poj.org/problem?id=1052 题目大意: 把1*1*1的小立方体通过粘接相邻面组成大的立方体的形状.如下图所示: 一层一层地堆叠,立方体从三个方向的投影会分别 ...
- SprimgMVC学习笔记(一)—— SpringMVC入门
一.什么是 SpringMVC ? 在介绍什么是 SpringMVC 之前,我们先看看 Spring 的基本架构.如下图: 我们可以看到,在 Spring 的基本架构中,红色圈起来的 Spring W ...