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 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
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct NODE{
struct NODE* lchild, *rchild;
int key;
int height;
}node;
int getHeight(node* root){
if(root == NULL)
return ;
else return root->height;
}
void updateHeight(node *root){
root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + ;
}
void L(node* &root){
node* temp = root;
root = root->rchild;
updateHeight(root);
temp->rchild = root->lchild;
root->lchild = temp;
updateHeight(temp);
}
void R(node* &root){
node *temp = root;
root = root->lchild;
updateHeight(root);
temp->lchild = root->rchild;
root->rchild = temp;
updateHeight(temp);
}
void insert(node* &root, int key){
if(root == NULL){ //此处可获得插入节点的信息
node* temp = new node;
temp->lchild = NULL;
temp->rchild = NULL;
temp->key = key;
temp->height = ;
root = temp;
return;
}
if(key < root->key){ //此处可获得距离插入节点最近得父节点得信息
insert(root->lchild, key);
updateHeight(root);
if(abs(getHeight(root->lchild) - getHeight(root->rchild)) == ){
if(getHeight(root->lchild->lchild) > getHeight(root->lchild->rchild)){
R(root);
}else{
L(root->lchild);
R(root);
}
}
}else{
insert(root->rchild, key);
updateHeight(root);
if(abs(getHeight(root->lchild) - getHeight(root->rchild)) == ){
if(getHeight(root->rchild->rchild) > getHeight(root->rchild->lchild)){
L(root);
}else{
R(root->rchild);
L(root);
}
}
}
}
int main(){
int N, key;
scanf("%d", &N);
node* root = NULL;
for(int i = ; i < N; i++){
scanf("%d", &key);
insert(root, key);
}
printf("%d", root->key);
cin >> N;
return ;
}
总结:
1、题意:按照题目给出的key的顺序,建立一个平衡二叉搜索树。
2、二叉搜索树的几个关键地方:
- 每个节点使用height来记录自己的高度,叶节点高度为1;
- 获得某个节点高度的函数,主要是由于在获取平衡因子时,有些树的子树是空的,需要返回0,为避免访问空指针,获取节点高度都要通过该函数而非height字段。
- 更新当前节点的高度,应更新为左右子树的最大高度+1。
- 左旋与右旋:一定是三步操作而不是两步(不要忘记新的root的原子树)。注意更新节点高度的先后顺序。
- 插入与建树:插入操作基于二叉搜索树的插入。在root = NULL时进行新建节点并插入,在此处可以获得插入节点的信息。而在递归插入语句处,可以获取插入A节点之后距离A节点最近的父节点。因此在递归插入结束后就要对该节点进行更新高度,并在此处更新完之后检查平衡因子,并做LL、LR、RR、RL旋转。
3、对rootA的左子树做插入,导致rootA的左子树与右子树高度差为2,则对以rootA为根的树旋转。
4、调试的时候,可以取很少的几个节点,然后画出调试过程中树的形状。
A1066. Root of AVL Tree的更多相关文章
- 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 ...
- PAT_A1066#Root of AVL Tree
Source: PAT A1066 Root of AVL Tree (25 分) Description: An AVL tree is a self-balancing binary search ...
- 04-树5 Root of AVL Tree + AVL树操作集
平衡二叉树-课程视频 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the tw ...
- PAT 1066 Root of AVL Tree[AVL树][难]
1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...
- PTA (Advanced Level) 1066 Root of AVL Tree
Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 04-树4. Root of AVL Tree (25)
04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- pat04-树4. Root of AVL Tree (25)
04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- pat1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
随机推荐
- Day2 列表,元组,字典,集合
一,列表 定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素. list=['alex', 'jack', 'chen', 'shaoye'] #创建一个列表. 特性: 1.可存 ...
- MVC最全jar包
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> & ...
- Jmeter安装与使用(压测)
一.介绍 Apache JMeter是100%纯JAVA桌面应用程序,被设计为用于测试客户端/服务端结构的软件(例如web应用程序).它可以用来测试静态和动态资源的性能,例如:静态文件,Java Se ...
- SpringBoot之通过yaml绑定注入数据
依赖包: <!--配置文件注解提示包--> <dependency> <groupId>org.springframework.boot</groupId&g ...
- 扩展运算符(spread)是三个点(…)
扩展运算符(spread)是三个点(…),将一个数组||类数组||字符串转为用逗号分隔的序列. js中用来对数组进行操作,把数组里面的东西统统拿出来 一.展开数组 //展开数组 let a = [1, ...
- php1
正则表达式 $p = '/name:(\w+\s?\w+)/'; $str = "name:steven jobs"; preg_match($p, $str, $match); ...
- 手机连接WiFi有感叹号x怎么回事?如何消除手机WiFi感叹号?
经过多年的革新,现在的安卓系统已经非常优秀了,某些程度已经超越iOS,卡顿和耗电也不再是安卓系统的代名词了.而为了体验到最优秀的安卓系统,不少人都会购买海外的手机,因为海外手机的安卓系统都比较精简,非 ...
- Visual Studio 2017 and Swagger: Building and Documenting Web APIs
Swagger是一种与技术无关的标准,允许发现REST API,为任何软件提供了一种识别REST API功能的方法. 这比看起来更重要:这是一个改变游戏技术的方式,就像Web服务描述语言一样WSDL( ...
- Java 设计模式 ------ 模板设计模式
模板设计模式主要来源于生活中有一些事情是有模板可以遵循的.举两个生活中的例子,如泡茶和泡咖啡,看一看. 泡茶有以下四个步骤: 1, 烧开水; 2 把茶放到水杯中; 3,倒入开水; 4, 加糖. 泡 ...
- poj-2823(单调队列)
题意:给你长度位n的数组,问每个长度为m的段的最值: 解题思路:这道题是单调队列的入门题: #include<iostream> #include<algorithm> #in ...