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 ...
随机推荐
- Json文件解析(上)
Json文件解析(上) 代码地址:https://github.com/nlohmann/json 自述文件 alt=GitHub赞助商 data-canonical-src="https: ...
- jps不是内部或外部命令, 亲测有用
https://blog.csdn.net/qq_41558341/article/details/105676741 亲测有用, 别的链接找了一大堆,无用
- Java swing JFrame用repaint出现闪烁的问题解决
这几天用swing写登录页面背景动图的时候发现一直会有闪烁(我的类是继承JFrame),就来搜原因后发现好像是因为repaint会调用update()方法中的清屏操作导致闪烁. 我当时看的是这个文章 ...
- Java 反射编程(上)
文章目录 反射的泛型就是用`? `来描述 反射与类的操作 (取得父类信息) 取得父类信息 1. 获得本类的包名称: 2. 取得父类的Class 对象 3. 取得父类接口 案例: 使用上述方法 反射与类 ...
- 【NX二次开发】Block UI 选项卡控件
[NX二次开发]Block UI 选项卡控件
- You Only Look One-level Feature
你只需要看一个层次的特征 摘要:本文回顾了单阶段检测器的特征金字塔网络(FPN),指出FPN的成功在于其对目标检测优化问题的分治解决,而不是多尺度特征融合.从优化的角度来看,我们引入了一种替代的方法来 ...
- 循序渐进BootstrapVue,开发公司门户网站(3)--- 结合邮件发送,收集用户反馈信息
在我们公司门户网站里面,如果有需要,我们可以提供一个页面给用户反馈信息,以便获得宝贵的用户信息反馈或者一些产品咨询的记录,一般这个结合邮件发送到负责人的邮箱即可.本篇随笔结合后端发送邮件的操作,把相关 ...
- Scala语言笔记 - 第三篇(容器方法篇)
Scala语言笔记 - 第三篇(容器方法篇) 目录 Scala语言笔记 - 第三篇(容器方法篇) map和flapMap方法: 最近研究了下scala语言,这个语言最强大的就是它强大的函数式编程( ...
- 在vs中调试关闭之后不关闭页面
在vs中调试api时会自动打开一个新的浏览器窗口,在关闭这个浏览器窗口时,会关闭调试.关闭调试时也会关闭浏览器窗口. 设置成调试时在已有的浏览器中打开调试页面,关闭调试也不会关掉浏览器窗口,反之亦然 ...
- element的日期选择使用value-format之后表单验证报错
在表单验证的时候报错 添加一个日期控件,但是发现在表单验证中遇到了冲突如下: Error in event handler for "el.form.change": " ...