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 ...
随机推荐
- 51nod 1577 异或凑数
思路真的是挺巧妙的. 让我惊叹,原来线性基还能这么做?!?! 好吧,这种取若干个数异或凑数的题目怎么能少的了线性基呢? 但是,问题集中在于怎么快速提取一个区间的线性基 暴力n^2 线段树维护线性基?分 ...
- (转)Maven学习总结(五)——聚合与继承
孤傲苍狼只为成功找方法,不为失败找借口! Maven学习总结(五)——聚合与继承 一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 1 <mod ...
- EasyUI实战篇之datagrid:如何重新设置datagrid所配置的属性(options)并重新查询列表(relaod)
http://www.stepday.com/topic/?873 今天在使用EasyUI的datagrid列表组件想实现一个列表的展现,且列表上方有搜索条件,初始化的时候我是这样配置的: 1.< ...
- 在Linux中将脚本做成系统服务
有一些情况下,我们需要将某些脚本作为系统服务来运行.比如,在我使用workerman框架开发php程序时,需要使用管理员权限来运行,而且需要开机自行启动程序提供服务.这个时候将启动程序写成服务就可以很 ...
- springboot的日志框架slf4j (使用logback输出日志以及使用)
1.为什么使用logback? ——在开发中不建议使用System.out因为大量的使用会增加资源的消耗.因为使用System.out是在当前线程执行的,写入文件也是写入完毕之后才继续执行下面的程序. ...
- 检测传入字符串是否存在重复字符,返回boolean
检测传入字符串是否存在重复字符,返回boolean,比如"abc"返回true:"aac"返回false 这里提供两种思路: 第一种: import java. ...
- 实战:使用SVN+apache搭建一个版本控制服务器
今天讲的内容: 实战:使用SVN+apache搭建一个版本控制服务器 每天: 10:00 晚上:21:00 服务端:xuegod63.cn IP:192.168.10.63 服务概述: SVN(s ...
- SQL提高查询效率【in、not in、between、like】等条件讲述
在使用SQL语句查询数据库记录时,如果要查询相同的内容,有着不同的多种方法. 仍然,尽管使用多种方法可以得到相同的结果,但是,如果您使用不同的方法,在执行效益上是截然不同的.因此,我们得仔细考虑,如果 ...
- INF文件详解
安装信息(Setup Information)文件是Windows系统支持的一种安装信息存放文件,一般以INF作为扩展名,因此也叫INF文件.安装信息INF文件与Windows内建的安装服务引擎(AP ...
- ★itext-为pdf文件添加页眉页脚 | 3步完成 |
由于上一篇自定义生成pdf的功能需求又增加了,需要加上页码.所以本博客诞生了~ 1. 通过继承PdfPageEventHelper类,实现需要实现的方法 import com.lowagie.text ...