Root of AVL Tree
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 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 the 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<iostream>
using namespace std;
struct treenode{
int data,h;
treenode* left=NULL;
treenode* right=NULL;
};
using tree=treenode*;
int height(tree t){
//cout<<"height(tree t)"<<endl;
if(!t) return ;
return max(height(t->left),height(t->right))+;
}
tree RotateLL(tree t){
//cout<<" RotateLL(tree t)"<<endl;
tree a=t->left;
t->left=a->right;
a->right=t;
a->h=max(height(a->left),height(a->right))+;
t->h=max(height(t->left),height(t->right))+;
return a;
}
tree RotateRR(tree t){
//cout<<"RotateRR(tree t)"<<endl;
tree a=t->right;
t->right=a->left;
a->left=t;
a->h=max(height(a->left),height(a->right))+;
t->h=max(height(t->left),height(t->right))+;
return a;
}
tree RotateLR(tree t){
//cout<<"RotateLR(tree t)"<<endl;
t->left=RotateRR(t->left);
return RotateLL(t);
}
tree RotateRL(tree t){
//cout<<"RotateRL(tree t)"<<endl;
t->right=RotateLL(t->right);
return RotateRR(t);
}
tree insert(tree t,int v){
//cout<<" insert(tree t,int v)"<<endl;
if(t==NULL){
t=new treenode();
t->data=v; t->h=;
return t;
}else if(v<t->data){
t->left=insert(t->left,v);
if(height(t->left)-height(t->right)==)
if(v<t->left->data)
t=RotateLL(t);
else t=RotateLR(t);
}else{
t->right=insert(t->right,v);
if(height(t->left)-height(t->right)==-)
if(v>t->right->data)
t=RotateRR(t);
else t=RotateRL(t);
}
t->h=height(t);
return t;
}
int main(){
int n;
cin>>n;
tree t=NULL;
for(int i=;i<n;i++){
int v; cin>>v;
t=insert(t,v);
}
cout<<t->data<<endl;
return ;
}
Root of AVL Tree的更多相关文章
- 04-树5 Root of AVL Tree + AVL树操作集
平衡二叉树-课程视频 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the tw ...
- PAT 1066 Root of AVL Tree[AVL树][难]
1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...
- PTA (Advanced Level) 1066 Root of AVL Tree
Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 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 ...
- pat1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 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 ...
随机推荐
- 1-10super和this关键字
什么是super? super代表的是当前子类对象中的父类型特征. 什么时候使用super? 子类和父类中都有某个数据,例如,子类和父类中都有name这个属性.如果要再子类中访问父类中的name属性, ...
- DNS递归查询、主从、加密认证、负载均衡
环境同DNS练习之正向解析. 在sishen64主机上安装必要软件 [root@sishen64 ~]# yum install -y bind bind-chroot bind-libs bind- ...
- Redis java操作客服端——jedis
1. Jedis 需要把jedis依赖的jar包添加到工程中.Maven工程中需要把jedis的坐标添加到依赖. 推荐添加到服务层.happygo-content-Service工程中. 1.1. 连 ...
- jdk1.8新日期时间类(DateTime、LocalDateTime)demo代码
//获取当前时间 LocalDateTime d0 = LocalDateTime.now(); System.out.println(DataConvertUtil.localDateTimeToS ...
- 在idea启动tomcat出现The JAVA_HOME environment variable is not defined correctly的解决
情况:某套代码是用jdk 1.6编译,然后电脑的JAVA_HOME系统变量配的是jdk1.7的,在tomcat启动时报错 The JAVA_HOME environment variable is n ...
- Python学习日记之Python函数及方法使用总结
1. DocStrings 文档字符串 可以直接输出位于函数内定义的说明 # -*- coding:utf-8 -*- def printMax(x, y): '''示例: 说明文档''' ...
- PHP CLI应用的调试原理
我们在Eclipse里选中一个PHP文件,右键选择Debug As->PHP CLI Application. 所谓CLI应用,是指这种脚本文件不需要任何Web服务器即可运行,当然, PHP运行 ...
- 原创:PHP编译安装配置参数说明
--prefix=/application/php-5.5.32 \ #指定PHP的安装路径 --with-mysql=/application/mysql/ \ ...
- HDU 5778 abs (暴力枚举)
abs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem De ...
- python中*号和**号的用法
1.乘法符号 2.可变长参数 当我们使用函数时,需要传入不定个数的位置参数时,就可以使用*号表示,即*args,以元组形式传入:需要传入不定个数的关键字参数时,使用**表示,即**kwargs,以字典 ...