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 ...
随机推荐
- Steamroller-freecodecamp算法题目
Steamroller 1.要求 对嵌套的数组进行扁平化处理.你必须考虑到不同层级的嵌套. 2.思路 设定结果数组res 用for循环遍历arr的元素,判断是否为数组,是,则用res=res.conc ...
- APCInject
#include <iostream> #include <Windows.h> #include <TlHelp32.h> using namespace std ...
- ElasticSearch High Level REST API【2】搜索查询
如下为一段带有分页的简单搜索查询示例 在search搜索中大部分的搜索条件添加都可通过设置SearchSourceBuilder来实现,然后将SearchSourceBuilder RestHighL ...
- Mysql--子查询、分页查询、联合查询
一. 子查询的定义 出现在其他语句中的select语句,称为子查询或者内查询,外部的查询语句称为主查询或者外查询,子查询可以包含普通select可以包含的任何语句. 外部查询:select.inser ...
- react与微信小程序
由组员完成 原文链接 都说react和微信小程序很像,但是像在什么部分呢,待我稍作对比. 生命周期 1.React React的生命周期在16版本以前与之后发生了重大变化,原因在于引入的React F ...
- 二十四、MySQL ALTER命令
MySQL ALTER命令 当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. 开始本章教程前让我们先创建一张表,表名为:testalter_tbl. root@ho ...
- 快速搭建FTP服务
Linux下ftp服务可以通过搭建vsftpd服务来实现,以CentOS为例,首先查看系统中是否安装了vsftpd,可以通过执行命令 rpm -qa | grep vsftpd 来查看是否安装相应的包 ...
- A Bug's Life(削弱版食物链)
Description Background Professor Hopper is researching the sexual behavior of a rare species of bug ...
- Python中的dict
dict_lst = [ ('字典的键必须可哈希',), ('字典的键重复覆盖',), ('字典可迭代') ('增',), ('删',), ('改',), ('查',), ('练习',), ] 字典的 ...
- Python入门基础--变量与基本数据类型
变量 什么是变量 变量就是变化的量,变就是变化,量用于衡量描述对象的状态 为什么要有变量 程序执行的本质就是一系列状态的变化,变是程序执行的直接体现,所以我们需要有一种机制能够反映或者说是保存下来程序 ...