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 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树的过程。
四个旋转方法,左单旋,右单旋,左右双旋,右左双旋。具体看代码,函数名写得挺清楚的。
还需要一个高度函数,递归一下就得出来了。
最后再弄一个insert 函数,注意AVL树的特点,左右子树的高度差为2时必须发生平衡旋转。
code
#include <iostream>
using namespace std;
struct node{
int data;
node *left, *right;
node(int data){this->data = data, this->left = this->right = NULL;}
};
node* rotate_left(node* root){
node* temp = root->left;
root->left = temp->right;
temp->right = root;
return temp;
}
node* rotate_right(node* root){
node* temp = root->right;
root->right = temp->left;
temp->left = root;
return temp;
}
node* rotate_left_right(node* root){
root->right = rotate_left(root->right);
return rotate_right(root);
}
node* rotate_right_left(node* root){
root->left = rotate_right(root->left);
return rotate_left(root);
}
int getHeight(node* root){
if(root == NULL) return 0;
return max(getHeight(root->right), getHeight(root->left)) + 1;
}
node* insert(int data, node* root){
if(root == NULL) root = new node(data);
else if(data > root->data) {
root->right = insert(data, root->right);
if(getHeight(root->right) - getHeight(root->left) >= 2)
root = root->right->data > data ? rotate_left_right(root) : rotate_right(root);
}
else {
root->left = insert(data, root->left);
if(getHeight(root->left) - getHeight(root->right) >= 2)
root = root->left->data < data ? rotate_right_left(root) : rotate_left(root);
}
return root;
}
int main(){
int n = 0, temp = 0;
scanf("%d", &n);
node *root = NULL;
for(int i = 0; i < n; i++){
scanf("%d", &temp);
root = insert(temp, root);
}
printf("%d", root->data);
return 0;
}
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 (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- 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 ...
- 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甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)
题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...
- PTA 04-树5 Root of AVL Tree (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree (25分) An AVL tree ...
- 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 ...
随机推荐
- 狂神说linux笔记:基本操作
Linux介绍 Linux的概述本文就不赘述了,如果想仔细了解的小伙伴可以百度Linux的历史或者看看狂神的原文.本文主要写linux的操作过程知识点. 狂神的原文如下: https://mp.wei ...
- .Net RabbitMQ实战指南——HTTP API接口调用
RabbitMQ Management插件还提供了基于RESTful风格的HTTP API接口来方便调用.一共涉及4种HTTP方法:GET.PUT.DELETE和POST.GET方法一般用来获取如集群 ...
- Flink从Kafka取数WordCount后TableApi写入ES
一.背景说明 需求为从Kafka消费对应主题数据,通过TableApi对数据进行WordCount后,基于DDL写法将数据写入ES. 二.代码部分 说明:代码中关于Kafka及ES的连接部分可以抽象到 ...
- 一款基于SpringBoot+SpringSecurity的后台管理系统,强烈推荐
简介 Base Admin一套简单通用的后台管理系统,主要功能有:权限管理.菜单管理.用户管理,系统设置.实时日志,API加密,以及登录用户修改密码.配置个性菜单等. 技术栈 前端:Layui 后端: ...
- 【linux】驱动-15-定时器
目录 前言 15. 定时器 15.1 内核函数汇总 15.2 内核滴答 15.3 相关结构体 15.4 setup_timer() 设置定时器 15.5 add_timer() 向内核添加定时器 15 ...
- 遇到禁止复制该怎么办?幸好我会Python...
相信大家都有遇到这种情况(无法复制): 或者是这种情况 以上这种情况都是网页无法复制文本的情况.不过这些对于Python来说都不是问题.今天辰哥就叫你们用Python去解决. 思路:利用pdfkit库 ...
- RobotFramework + Python 自动化入门 二 (关键字)
在<RobotFramwork + Python 自动化入门 一>中,完成了Robot环境搭建及测试脚本的创建和执行. 本节,对RobotFramework的关键字使用和查看源码进行介绍. ...
- 注解式项目开发!详细解析Java中各个注解的作用和使用方式
@Target 作用: 指明了修饰的这个注解的使用范围, 即被描述的注解可以用在哪里 @Target(ElementType.Type) ElementType取值的类型: TYPE: 类,接口或者枚 ...
- Golang去除字符串前后空格
Golang去除字符串前后空格 实现Demo package main import "fmt" func DeletePreAndSufSpace(str string) str ...
- 安装VMwareTools
2.1.挂载VMwareTools镜像