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 ...
随机推荐
- h265webplayer
h265webplayer https://github.com/ksvc/h265webplayer h265webplayer是金山云的Web端H.265视频播放器,该播放器Web SDK让您可以 ...
- 英特尔 i5-9400F,或将成为本年最高性价比的游戏处理器
英特尔 i5-9400F,或将成为本年最高性价比的游戏处理器 一直以来,每一代英特尔 i5 系列都是很多游戏玩家主要的选购对象,它和任何一款显卡似乎都能够很好的搭配起来.可凡事都有美中不足的地方,比如 ...
- 浅谈Gson和fastjson使用中的坑
相信大家在代码编写中都用过Gson和fastjson吧,用来进行 Java对象和json字符串之间的转换. 本篇文章就主要介绍博主在工作中使用这两款工具时遇到的坑和对应的解决办法. 觉得有用的可以点个 ...
- springboot异常错误处理
1.在有模板引擎的情况下: springboot会默认找 templates/error/错误状态码.html,所以我们要定制化错误页面就可以到templates/error下创建一个[对应错误状态码 ...
- ffmpeg实战-音视频合成案例
转发自白狼栈:查看原文 很多小伙伴私下里留言说,之前没接触过音视频,对于ffmpeg可以做什么还是有些懵. 今天我们一起看下我们究竟可以用 ffmpeg 做什么? 很多小伙伴应该都玩过抖音,你在&qu ...
- LuatOS | 全新在线模拟器,随时随地发挥创意
LuatOS --运行在嵌入式硬件的实时操作系统,开启全新物联网生态. 聚焦嵌入式应用生态,内置功能可支持绝大多数物联网应用场景.深度整合Lua语言,只需少量内存和Flash空间就能运行.不局限于合宙 ...
- Binding(三):资源和ValueConverter
这节讲资源和值转换器(ValueConverter). 资源 在XAML中,我们想要使用外部的数据或者类,需要引入其命名空间,然后将其定义为XAML页面的资源,供给控件使用,或者我们需要封装一个共用的 ...
- split截取字符串
一.根据单个分隔字符用split截取字符串:string st="GT123_1";split代码:string[] sArray=st.split("_"); ...
- 解决 Golnag Gin框架跨域
package main import ( "github.com/gin-gonic/gin" "awesomeProject/app/app_routers" ...
- external-provisioner源码分析(1)-主体处理逻辑分析
更多ceph-csi其他源码分析,请查看下面这篇博文:kubernetes ceph-csi分析目录导航 概述 接下来将对external-provisioner组件进行源码分析. 在external ...