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 ...
随机推荐
- samba多用户
注意的点 1:安装 服务器 yum install -y samba* 客户端 yum install -y samba-client cifs-utils 服务器端和客户端网络能ping通 2 ...
- .net core 2.0 jwt身份认证系统
经历了很久,.net core 2.0 终于发布了! 之前一直用的core 1.1,升级了2.0后发现认证的机制(Auth)发生了比较大的变化,在1.1中认证配置是在Configure中完成,而在2. ...
- 安卓--ListView
实验目的: 学习使用ListView 实验要求: 实现一个列表,其中显示班级学号姓名,提供添加功能,如需要删去某一项,长按该项,通过弹出菜单显示删除功能. package com.flyuz.app3 ...
- .NET Services Stack笔记之手写版
- SpringMVC from 表单标签和 input 表单标签
刚学习很懵 不知道还有springmvc 自己的表单 于是乎就上网查了一下 这个真的好用多啦 刚学习很懵 不知道还有springmvc 自己的表单 于是乎就上网查了一下 这个真的好用多啦 ...
- Vscode 配置 maven debug
# maven.cmd 上方设置此变量 set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address= ...
- BestCoder Round #64 1001
Numbers Accepts: 480 Submissions: 1518 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553 ...
- Centos 7.4 配置Tomcat管理员用户
1,进入Tomcat路径下的conf文件夹 ,编辑tomcat-users.xml文件 2,在<tomcat-users>标签中增加user标签,用户名密码随便填写,roles可根据权限需 ...
- git 合并某个提交commit到指定的分支上
https://blog.csdn.net/anhenzhufeng/article/details/77962943 git checkout master git cherry-pick 62ec ...
- Log4web独立config配置
第一步:config配置,独立文件的 <?xml version="1.0" encoding="utf-8"?> <configuratio ...