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 ...
随机推荐
- h5-35-ajax轮询实现推送效果
data.txt { "number1":1200, } index.html <!DOCTYPE html> <html> <head> &l ...
- windows密码长度最小值改不了
控制台输入gpedit.msc或者在“开始→控制面板→管理工具→本地安全策略→账户策略→密码策略→密码长度最小值”中修改不了,是灰色的,不让修改 用命令行可以修改开始-->运行-->输入& ...
- 用NPOI从DataTable到Excel,向Excel模板填充数据
DataTable---->Excel,填充数据 private IWorkbook workbook = null; private ISheet sheet = null; private ...
- AJPFX总结java开发常用类(包装,数字处理集合等)(三)
4.Map是一种把键对象和值对象进行关联的容器,而一个值对象又可以是一个Map,依次类推,这样就可形成一个多级映射.对于键对象来说,像Set一样,一 个Map容器中的键对象不允许重复,这是为了保持查找 ...
- mysql多表查询20题
+-------------------+| Tables_in_dapeng3 |+-------------------+| class || course || s1 || score || s ...
- Java网络编程学习笔记
Java网络编程,我们先来看下面这一张图: 由图可得:想要进行网络编程,首先是服务器端通过ServerSocket对某一个端口进行监听.通过accept来判断是否有客户端与其相连.若成功连上,则通过r ...
- ag-grid-vue的 行默认选中
that.$nextTick(() => { that.gridListOptions.api.onGroupExpandedOrCollapsed(); that.$nextTick(() = ...
- 如何解决MySQL在高版本需要指明是否进行SSL连接问题
WARN: Establishing SSL connection without server's identity verification is not recommended. Accordi ...
- RecycleView的万能适配器
转载自http://www.cnblogs.com/liushilin/p/5720926.html 由于RecyclerView的Adapter必须继承自RecyclerView.Adapter,并 ...
- 第一章 熟悉Objective -C 编写高质量iOS与OS X代码的52 个有效方法
第一章 熟悉Objective -C 编写高质量iOS与OS X代码的52 个有效方法 第一条: 了解Objective-C 语言的起源 关键区别在于 :使用消息结构的语言,其运行时所应执行 ...