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 ...
随机推荐
- 关于找List的中间Node
初始值 slow = fast = head; 如果使用 fast && fast->next && fast->next->next 作为快慢指针循 ...
- 解题:NOI 2010 超级钢琴
题面 WC时候写的题,补一下 做法比较巧妙:记录每个位置和它当前对应区间的左右端点,做前缀和之后重载一下小于号,用优先队列+ST表维护当前最大值.这样贡献就是区间最大值和端点左边差分一下,可以O(1) ...
- [学习笔记]凸优化/WQS二分/带权二分
从一个题带入:[八省联考2018]林克卡特树lct——WQS二分 比较详细的: 题解 P4383 [[八省联考2018]林克卡特树lct] 简单总结和补充: 条件 凸函数,限制 方法: 二分斜率,找切 ...
- 【洛谷P1272】道路重建
题目大意:给定一个 N 个节点的树,求至少剪掉多少条边才能使得从树中分离出一个大小为 M 的子树. 题解:考虑树形 dp,定义 \(dp[u][i][t]\) 为以 u 为根节点与前 i 个子节点构成 ...
- 【codevs1245】最小的 N 个和
题目大意:给定两个有 N 个数字的序列,从这两个序列中任取一个数相加,共有 \(N^2\) 个和,求这些和中最小的 N 个. 题解:由于数据量是 10W,必须减少每次选取的决策集合中元素的个数.可以发 ...
- RabbitMQ 客户端开发向导
准备工作:composer 引入 php-amqplib 说明:本文说明基于 Java(主要说明原理),实现使用 php RabbitMQ Java 客户端使用 com.rabbitmq.client ...
- 在ubuntu server上搭建Hadoop
1. Java安装: Because everything work with java. $ sudo apt-get install openjdk-7-jdk 安装之后,可以查看java的版本信 ...
- vue子组件的自定义事件
父子组件的信息传递无碍就是父组件给子组件传值(props和$attrs)和父组件触发子组件的事件($emit) 之前已经谈过了父组件给子组件传值了,现在来说说父组件触发子组件的自定义事件吧-- 实际上 ...
- Java基础-数据类型应用案例展示
Java基础-数据类型应用案例展示 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.把long数据转换成字节数组,把字节数组数据转换成long. /* @author :yinz ...
- haproxy acl访问限制IP
http-request: 7层过滤 tcp-request content: tcp层过滤,四层过滤 注意 四层 采用 accept和reject 七层采用 allow和deny ...