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. hql语法及自定义函数(含array、map讲解) + hive的java api

    本博文的主要内容如下: .hive的详细官方手册    .hive支持的数据类型   .Hive Shell .Hive工程所需依赖的jar包  .hive自定义函数 .分桶4   .附PPT hiv ...

  2. Hexo瞎折腾系列(5) - 使用hexo-neat插件压缩页面静态资源

    为什么要压缩页面静态资源 对于个人博客来说,优化页面的访问速度是很有必要的,如果打开你的个人站点,加载个首页就要十几秒,页面长时间处于空白状态,想必没什么人能够忍受得了吧.我个人觉得,如果能把页面的加 ...

  3. Java | 基础归纳 | java时间格式处理总结

    https://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html https://blog.csdn.net/xsj_blog/a ...

  4. AtCoder Grand Contest 015 E - Mr.Aoki Incubator

    题目传送门:https://agc015.contest.atcoder.jp/tasks/agc015_e 题目大意: 数轴上有\(N\)个点,每个点初始时在位置\(X_i\),以\(V_i\)的速 ...

  5. php 缩略图

    <!DOCTYPE html><!-- HTML5表单 --><form method="post" action="" enct ...

  6. DNS练习之反向解析

    环境同正向解析一样. 切换到/var/named/chroot/etc目录下: 编辑named.rfc1912.zones文件,在末尾添加如下内容: [root@sishen63 etc]# vim ...

  7. 搭建一个高可用的redis环境

    一.环境准备 我的环境: Fedora 25 server  64位版 6台: 192.168.10.204 192.168.10.205 192.168.10.206 192.168.10.203 ...

  8. 页面html图片按钮多种写法

    原地址:http://blog.163.com/weison_hi/blog/static/17680404720118534033788/ 第一种: 在一般情况下按钮提交表单: <form i ...

  9. Java-每日编程练习题③

    一.计算圆周率 中国古代数学家研究出了计算圆周率最简单的办法: PI=4/1-4/3+4/5-4/7+4/9-4/11+4/13-4/15+4/17...... 这个算式的结果会无限接近于圆周率的值, ...

  10. P3374 【模板】树状数组 1 单点修改与区间查询

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...