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 subtrees of any node difer by at most one; if at any time they difer 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
题目分析
已知平衡二叉树建树序列,求建树后的根节点
解题思路
1.建树(平衡二叉树insert节点)
2.打印根节点
易错点
左旋、右旋、插入节点方法,参数列表中要用指针引用node *&root,否则是值传递,方法中对root本身的修改不会在main函数中生效
Code
#include <iostream>
using namespace std;
struct node {
int data;
int heigh=0;
node * left=NULL;
node * right=NULL;
node() {}
node(int _data):data(_data) {
heigh=1;
}
};
int getHeigh(node * root) {
if(root==NULL)return 0;
return root->heigh;
}
void updateHeigh(node * root) {
root->heigh=max(getHeigh(root->left),getHeigh(root->right))+1;
}
void L(node * &root) {
//左旋
node * temp=root->right;
root->right=temp->left;
temp->left=root;
updateHeigh(root);
updateHeigh(temp);
root=temp;
}
void R(node * &root) {
//右旋
node * temp=root->left;
root->left=temp->right;
temp->right=root;
updateHeigh(root);
updateHeigh(temp);
root=temp;
}
int getBalanceFactor(node *root) {
return getHeigh(root->left)-getHeigh(root->right);
}
void insert(node * &root, int val) {
if(root==NULL) {
root=new node(val);
return;
}
if(val<root->data) {
insert(root->left,val);
updateHeigh(root);
if(getBalanceFactor(root)==2) {
if(getBalanceFactor(root->left)==1) {
//LL
R(root);
} else if(getBalanceFactor(root->left)==-1) {
//LR
L(root->left);
R(root);
}
}
} else {
insert(root->right,val);
updateHeigh(root);
if(getBalanceFactor(root)==-2) {
if(getBalanceFactor(root->right)==-1) {
//RR
L(root);
} else if(getBalanceFactor(root->right)==1) {
//RL
R(root->right);
L(root);
}
}
}
}
int main(int argc,char * argv[]) {
int n,m;
scanf("%d",&n);
node * root=NULL;
for(int i=0; i<n; i++) {
scanf("%d",&m);
insert(root,m);
}
printf("%d",root->data);
return 0;
}
PAT Advanced 1066 Root of AVL Tree (25) [平衡⼆叉树(AVL树)]的更多相关文章
- 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)
题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...
- PAT Advanced 1102 Invert a Binary Tree (25) [树的遍历]
题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...
- PAT (Advanced Level) 1110. Complete Binary Tree (25)
判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...
- 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 ...
- 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分)
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)
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 : ...
- pat1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
随机推荐
- vue+ui
一.elementui import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.u ...
- Json实体类驼峰名称转化器
背景 我们常用一些网站,将json转化成实体类.但不巧的是,这些自动生成的都是小驼峰.需要进一步的改成大驼峰+JsonProperty.接着同事说他已经有个工具了.我稍微简化了一下 方法 首先行分离. ...
- js实现连续输入之后发送请求
输入框是我们经常会用到的功能,想要实现输入就请求的功能 但是在实际开发中,为了减少服务器压力,会在输入之后停留1s没有输入之后再进行搜索 研究之后用原生js及表单写了一个简单的demo,如果有好的de ...
- POJ 2309:BST lowbit
BST Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9140 Accepted: 5580 Description C ...
- Oracle 序列(查询序列的值,修改序列的值)
1.序列的语法形式 create sequence 序列名 increment by n start with n maxvalue n | nomaxvalue minvalue n | nomin ...
- laravel.01.一些细节
0:参考1,参考2,参考3,参考4,参考5 1.读取项目的配置文件内容,比如app.php下的name属性,用config('app.name','default-value'); 2.读取.ENV文 ...
- 15.swoole学习笔记--异步写入文件
<?php //异步写入文件 $content="hello world"; swoole_async_writefile('2.txt',$content,function ...
- 面向对象设计思想和MVC设计模式
虽然之前学习Java时有接触过面向对象的设计思想,但因当时Java没学好.所以导致这两天讲php的面向对象设计时,感到没有头绪,这也反应了我练习少和逻辑能力的不足.而MVC设计思想 面向对象就是要将系 ...
- sql server C#操作。原文在收藏页面
C#操作SQL Server数据库 1.概述 2.连接字符串的写法 3.SqlConnection对象 4.SqlCommand对象 5.SqlDataReader对象 6.DataSet对象 7 ...
- 逆向-PE头解析
目录 PE头解析 数据结构 IMAGE_DOS_HEADER IMAGE_NT_HEADERS 区块 PE头解析 PE 格式是Windows系统下组织可执行文件的格式.PE文件由文件头和对应的数据组成 ...