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 ...
随机推荐
- 已解决: mybatis报错 org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'xxx' in 'class java.lang.String'
最近在练习MyBatis时 进行姓名的模糊查询时候出现 org.apache.ibatis.exceptions.PersistenceException: ### Error querying da ...
- Qt之QThread随记
这是一篇随记,排版什么的就没有那么好了:) 首先要知道,一个线程在资源分配完之后是以某段代码为起点开始执行的,例如STL内的std::thread,POSIX下的pthread等,都是以函数加其参数之 ...
- Mysql操作方法类
帮助类: using System; using System.Collections.Generic; using System.Data; using System.Linq; using Sys ...
- Linux时区修改
Linux修改时区的正确方法 CentOS和Ubuntu的时区文件是/etc/localtime,但是在CentOS7以后localtime以及变成了一个链接文件 [root@centos7 ~]# ...
- 简述在php中 = 、==、 === 的区别(简述在php中 等于 、双等于、 三等于 的区别)
= 是赋值:就是说给一个变量赋值 == 是轻量级的比较运算,只看值不看类型 === 是重量级的比较运算,既看值,也看类型,要绝对相等才会为true
- DeepFaceLab进阶(4):通过Colab免费使用Tesla K80 跑模型!
当学会了换脸软件DeepFaceLab基本使用,各种参数配置,各有优化技能之后.唯一约束你的可能是电脑配置. CPU能跑,但是慢到怀疑人生,低配模型都得跑一周 低配显卡,显存不够,H128 根本就跑不 ...
- batch-normalization为什么效果好
batch-normalization为什么效果好 深度学习中 Batch Normalization为什么效果好? - 龙鹏-言有三的回答 - 知乎 https://www.zhihu.com/qu ...
- Farm Tour POJ - 2135 (最小费用流)
When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= ...
- Codeforces Round #464 (Div. 2) C. Convenient For Everybody
C. Convenient For Everybody time limit per test2 seconds memory limit per test256 megabytes Problem ...
- leetcode 【 Remove Duplicates from Sorted Array 】python 实现
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...