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, 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 ythe 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树的实现。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 100+5
typedef long long ll;
int n,a[N_MAX];
struct AVLtree {
int key=INF;
AVLtree* l_child, *r_child;
int height;
}T;
AVLtree *NIL;
AVLtree* root;
int Height(AVLtree* T) {//返回x的高度
int l_height=, r_height=;
if (T) {
l_height = Height(T->l_child);
r_height = Height(T->r_child);
return T->height = max(l_height, r_height) + ;
}
return ;//节点不存在,没有高度
} AVLtree* LeftRotation(AVLtree* a) {//左单旋
AVLtree *b = a->l_child;
a->l_child = b->r_child;
b->r_child = a;
a->height = Height(a);
b->height = Height(b);
return b;
}
AVLtree* RightRotation(AVLtree* a) {//右单旋
AVLtree* b = a->r_child;
a->r_child = b->l_child;
b->l_child = a;
a->height = Height(a);
b->height = Height(b);
return b;
} AVLtree* LeftRightRotation(AVLtree* a) {
a->l_child = RightRotation(a->l_child);
return LeftRotation(a);
}
AVLtree* RightLeftRotation(AVLtree* a) {
a->r_child = LeftRotation(a->r_child);
return RightRotation(a);
} AVLtree* insert(int key,AVLtree*root) {//root是当前AVL树的根, 返回根
if (root == NIL) {
root = new AVLtree;
root->key = key;
root->height = ;
root->l_child = root->r_child = NIL;
}
if (key < root->key) {
root->l_child = insert(key,root->l_child);
if (Height(root->l_child)-Height(root->r_child)==) {
if (key < root->l_child->key) {
root = LeftRotation(root);
}
else root = LeftRightRotation(root);
}
}
else if (key > root->key) {
root->r_child = insert(key,root->r_child);
if (Height(root->r_child) - Height(root->l_child) == ) {
if (key > root->r_child->key) {
root = RightRotation(root);
}
else root = RightLeftRotation(root);
}
} root->height = Height(root);//节点插入完毕,计算当前根的高度
return root;
} int main() {
while (scanf("%d",&n)!=EOF) {
for (int i = ; i < n; i++) {
int a; scanf("%d",&a);
root=insert(a, root);
}
printf("%d\n",root->key);
}
return ;
}
pat 甲级 1066. Root of AVL Tree (25)的更多相关文章
- 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
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- PAT 甲级 1066 Root of AVL Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...
- 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 ...
- 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)
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)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- PAT甲级——A1066 Root of AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
随机推荐
- 第十六篇、OC_按比例适配
// 屏幕高度 #define XMGHeight [UIScreen mainScreen].bounds.size.height // 屏幕宽度 #define XMGWidth [UIScree ...
- 前端开发APP,从HBuilder开始~
内容简介 介绍目前前端人员开发app的几种方法,具体介绍hbuilder开发app,一扇赞新的大门~ 无所不能的js 最开始js仅仅局限于网页上一些效果,操作网页内容等, 但是nodejs把js带入了 ...
- P4746 C’s problem(c)
时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试 描述 题目描述 小C是一名数学家,由于它自制力比较差,经常通宵研究数学问题. 这次它因为这个数 ...
- window.onload和$(docunment).ready的区别
浏览器加载完DOM后,会通过javascript为DOM元素添加事件,在javascript中,通常使用window.onload()方法. 在jquery中,则使用$(document).ready ...
- vue.js 四(指令和自定义指令)
官方的指令说明已经很简单了,这里再写一遍,也是自己加深一下印象 v-text 就是写入单纯的文本,可以忽略这个指令直接双花括号代替 <span v-text="msg"> ...
- PHP 优化
来源:歪麦博客 https://www.awaimai.com/1050.html 1 字符串 1.1 少用正则表达式 能用PHP内部字符串操作函数的情况下,尽量用他们,不要用正则表达式, 因为其效率 ...
- JZOJ 5185. 【NOIP2017提高组模拟6.30】tty's sequence
5185. [NOIP2017提高组模拟6.30]tty's sequence (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB ...
- HDU:1358-Period
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Desc ...
- MySQL基础9-主键约束、外键约束、等值连接查询、一对一和多对多关系
1.主键约束和外键约束 外键约束 * 外键必须是另一表的主键的值(外键要引用主键!) * 外键可以重复 * 外键可以为空 * 一张表中可以有多个外键! 概念模型在数据库中成为表 数据库表中的多对一关系 ...
- wap html5播放器和直播开发小结
此文已由作者吴家联授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 去年年中的时候,借着产品改版的机会,将之前的h5播放器好好整理重构了一番.之前的h5播放器较为简陋,有几个大 ...