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 ...
随机推荐
- 2019.3.16 noiac的原题模拟赛
RT,这太谔谔了,我不承认这是模拟赛 但是虽然是搬了三道题,题目本身也还能看,就这么着吧 (怎么机房里就我一道原题都没做过啊 T1 CF24D Broken Robot 比较简单地列出式子之后,我们发 ...
- 【模板】LCA
代码如下 #include <bits/stdc++.h> using namespace std; const int maxn=5e5+10; inline int read(){ i ...
- influxdb简单使用
之前对influxdb有一个简单的了解和入门的使用,近期由于想使用influxdb做一点东西玩玩,又要捡起influxdb.本篇就针对influxdb的数据库.表的概念,增删改查操作.RESTful操 ...
- Git error: hint: Updates were rejected because the remote contains work that you do hint: not have locally
hint: Updates were rejected because the remote contains work that you dohint: not have locally. This ...
- Splay模板讲解及一些题目
普通平衡树模板以及文艺平衡树模板链接. 简介 平衡二叉树(Balanced Binary Tree)具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二 ...
- python学习笔记7-excel操作
一.操作excel import xlwt book = xlwt.Workbook() #新建一个excel sheet = book.add_sheet('sheet1') #添加一个sheet页 ...
- 分布式监控工具Ganglia 介绍 与 集群部署.
如果你目的很明确就是冲着标题来的,不爱看我唠叨,请直接进入第二个分割线之后的内容. 其实之前就是有做Swift监控平台的打算的,但是因为没什么硬性需求么,也不要紧的,就一直搁置了.最近实验室来了个大二 ...
- 关于如何在Python中使用静态、类或抽象方法的权威指南
Python中方法的工作方式 方法是存储在类属性中的函数,你可以用下面这种方式声明和访问一个函数 >>> class Pizza(object): ... def __init__( ...
- 日常训练赛 Problem C – Complete Naebbirac’s sequence
比赛链接https://vjudge.net/contest/256988#status/17111202012/C/0/ 大意:三个操作,使得输入的数中,从1-n,每一个数出现的次数相同. wa代码 ...
- 谁在call我-backtrace的实现原理【转】
转自:http://www.xuebuyuan.com/1504689.html 显示函数调用关系(backtrace/callstack)是调试器必备的功能之一,比如在gdb里,用bt命令就可以查看 ...