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的更多相关文章

  1. 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 ...

  2. 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, ...

  3. 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 ...

  4. PAT甲级1066. Root of AVL Tree

    PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...

  5. 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 ...

  6. 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 ...

  7. pat1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  8. pat 甲级 1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  9. 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. spring+redis 报错 org.springframework.core.serializer.support.DeserializingConverter.<init>(Ljava/lang/ClassLoader;)V

    这个问题的原因大概就是spring-data-redis.jar包版本不对 ,下面版本可以正常启动 <dependency> <groupId>org.springframew ...

  2. 跟我一起玩Win32开发(15):ListView控件

    这个控件其实不用阿拉来介绍,因为它太常见了,就好像我们一出门就会看到妹子一样常见.当然也可以说,它是对ListBox的扩充. 在使用该控件之前,我先介绍VS的一个相当好玩的功能. 在代码文件的#inc ...

  3. Jmeter之添加响应断言,bean shell post processor

    一直在想运用jmeter来实现接口自动化测试,但是每次每个接口执行完,需要肉眼去看一看,执行的结果对不对,总结了两种办法, 一.将每个请求的响应结果,导出到文件 选中请求右键-->添加后置处理器 ...

  4. MongoDB学习笔记~监控Http请求的消息链

    在微服务架构里,你的一个任务可以需要经过多次中转,去多个接口获取数据,而在这个过程中,出现问题后的解决就成了一个大难点,你无法定位它的问题,这时,大叔的分布式消息树就出现了,费话不多说,主要看一下实现 ...

  5. new几种用法

    在 C# 中,new 关键字可用作运算符.修饰符或约束. new 运算符 用于创建对象和调用构造函数. new 修饰符 用于向基类成员隐藏继承成员. new 约束 用于在泛型声明中约束可能用作类型参数 ...

  6. 2019/05/13 JAVA虚拟机堆内存调优

    -Xms4000m 堆内存初始值 * -Xmx4000m 堆内存最大值 * -XX:+PrintGCDetails 打印GC信息 * -XX:+UseSerialGC 使用串行GC * -XX:+Pr ...

  7. springboot之读取配置文件

    1.propertie配置读取数据 /** * 通过value取配置文件中的数据 */ @Component @PropertySource(value = {"config/db-conf ...

  8. AndroidStudio中使用SVN

    AndroidStudio中使用SVN提交项目 1.安装SVN,我选择使用TortoiseSVN-1.8.7.25475-x64-svn-1.8.9.msi(安装文件地址如下:http://downl ...

  9. logging模块基础

    很多程序都有记录日志的需求,日志不仅可以保存访问记录,也可以有错误,警告等信息输出. python的logging模块提供了标准的日志接口,可以通过logging存储各种格式的日志.logging模块 ...

  10. 【学习笔记】响应式布局的常用解决方案(媒体查询、百分比、rem、和vw/vh)

    原文转载:https://blog.csdn.net/sinat_17775997/article/details/81020417 一.媒体查询 不同物理分辨率的设备,在还原设计稿时,css中设置的 ...