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 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
AVL树的旋转。
#include <bits/stdc++.h>
using namespace std; const int maxn = 101000;
struct Node {
int val;
int son[2];
int height;
}s[maxn];
int root, sz;
int n; int add(int x) {
s[sz].val = x;
s[sz].son[0] = s[sz].son[1] = -1;
s[sz].height = 0;
sz ++;
return sz - 1;
} int Height(int id) {
if(id == -1) return -1;
return s[id].height;
} int R(int k2) {
int k1 = s[k2].son[0];
s[k2].son[0] = s[k1].son[1];
s[k1].son[1] = k2;
s[k2].height = max(Height(s[k2].son[0]), Height(s[k2].son[1])) + 1;
s[k1].height = max(Height(s[k1].son[0]), Height(s[k1].son[1])) + 1;
return k1;
} int L(int k2) {
int k1 = s[k2].son[1];
s[k2].son[1] = s[k1].son[0];
s[k1].son[0] = k2;
s[k2].height = max(Height(s[k2].son[0]), Height(s[k2].son[1])) + 1;
s[k1].height = max(Height(s[k1].son[0]), Height(s[k1].son[1])) + 1;
return k1;
} int RL(int k3) {
int k1 = s[k3].son[1];
s[k3].son[1] = R(k1);
return L(k3);
} int LR(int k3) {
int k1 = s[k3].son[0];
s[k3].son[0] = L(k1);
return R(k3);
} int Insert(int id, int val) {
if(id == -1) {
id = add(val);
} else if(val < s[id].val) {
s[id].son[0] = Insert(s[id].son[0], val);
if(Height(s[id].son[0]) - Height(s[id].son[1]) == 2) { // 需要调整
if(val < s[s[id].son[0]].val) id = R(id);
else id = LR(id);
}
} else {
s[id].son[1] = Insert(s[id].son[1], val);
if(Height(s[id].son[1]) - Height(s[id].son[0]) == 2) { // 需要调整
if(val > s[s[id].son[1]].val) id = L(id);
else id = RL(id);
}
}
s[id].height = max(Height(s[id].son[0]), Height(s[id].son[1])) + 1;
return id;
} int main() {
scanf("%d", &n);
root = -1;
for(int i = 1; i <= n; i ++) {
int x;
scanf("%d", &x);
root = Insert(root, x);
// cout << root << endl;
}
cout << s[root].val << endl;
return 0;
}
PAT 1066. Root of AVL Tree (25)的更多相关文章
- 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 ...
- pat 甲级 1066. 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 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...
- 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, ...
- PAT Advanced 1066 Root of AVL Tree (25) [平衡⼆叉树(AVL树)]
题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...
- 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 ...
- 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 ...
- 1066 Root of AVL Tree (25分)(AVL树的实现)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- PAT (Advanced Level) 1066. Root of AVL Tree (25)
AVL树的旋转.居然1A了.... 了解旋转方式之后,数据较小可以当做模拟写. #include<cstdio> #include<cstring> #include<c ...
随机推荐
- 【bzoj1040】 ZJOI2008—骑士
http://www.lydsy.com/JudgeOnline/problem.php?id=1040 (题目链接) 题意 一个基环森林,从中选出不相邻的若干个点使得这些点的点权和最大. Solut ...
- 压力测试以及编译安装httpd2.4
压力测试以及编译安装httpd2.4 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.使用 deflate_module模块压缩页面优化传输速度 我们的httpd软件自带的有一个 ...
- Does Deep Learning Come from the Devil?
Does Deep Learning Come from the Devil? Deep learning has revolutionized computer vision and natural ...
- 作为一个有B格的前端工程师需要掌握的一些知识
如果说你3年还在不停地切页面的... 那么你对http协议的了解程度 你的原生的javascript的掌握程度 你的页面的优化的理念 你在写页面是否会有什么独特地技巧 你对ajax的get和post方 ...
- 流行的软件工程过程--Rational统一过程!
RUP提供了一个给角色分配任务和责任的严格方法,在J2EE开发中使用RUP出于以下三个原因: RUP以架构为中心:在将资源分配给全面开发之前,它先开发一个可执行的架构原型. UP是迭代并基于构件的. ...
- Maven 环境的配置
Maven 环境的配置 现在Java新架构的不断出现,例如Struts,Spring,Hibernate等,项目的配置文件的增多,给开发人员带来很大麻烦.在实际的开发当中,Myeclipse中的pro ...
- iframe引入网页
<!DOCTYPE html> <html> <body> <iframe src="/example/html/demo_iframe.html& ...
- mongoexport导出csv中文乱码
在用mongoexport导出csv文件时,发现数据库中的中文在excel中都显示为乱码,用notepad打开则正常. 解决办法: 在notepad中,将编码格式改为UTF-8,保存,再用excel打 ...
- python內建模块之datetime
from:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143193755 ...
- linux常用运维命令【转】
自己的小网站跑在阿里云的ECS上面,偶尔也去分析分析自己网站服务器日志,看看网站的访问量.看看有没有黑阔搞破坏!于是收集,整理一些服务器日志分析命令,大家可以试试! 1.查看有多少个IP访问: awk ...