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 ...
随机推荐
- android 使用 git 进行版本控制
远程建立仓库 vcs --> import into version control --> create git respository 选中整个工程(project 页面) vcs - ...
- Centos 8双网卡设置
原理:不管开发板是通过直连.路由器还是交换机连接到PC机,最终都是接到PC的以太网网卡(对笔记本来说,一般存在两个网卡,一个WIFI网卡和以太网网卡):因此要实现PC机与虚拟机的互ping,必须把虚拟 ...
- redis学习(五)
一.Redis 发布订阅 1.Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. 2.Redis 客户端可以订阅任意数量的频道. 比如你订阅 ...
- redis.conf文件配置
最重要三个配置 1. bind 127.0.0.1 需要注释掉这一行,使别的主机可以访问 2. daemonize no 需要改为yes,使其后台运行 3. requirepass foobared ...
- C语言-整数类型
C语言-整数类型 整数类型 Reg为寄存器 字长,是说这个寄存器是多少宽的,每个寄存器可以表示32bit数据,也是说CPU与RAM每一次传递的数据也是32bit 计算机内部一切都是二进制 所有的类型, ...
- 053-switch分支结构
<?php $week=3; //定义并初始化星期变量 switch($week){ case 0: //变量为0的情况 echo '星期日.'; break; case 1: //变量为1的情 ...
- Essay写作如何提升自己的辩驳水平?
辩证思维在英文写作上的表现方式有许多种,今天来讲讲Counterargument&Rebut,广泛用于英文写作和口语辩论.其作用就是通过辩驳和你论点相反的意见,来突出自己的论点更正确. 话说衡 ...
- js 加密解密 TripleDES
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- Python 内置类型 dict, list,线程安全吗
近段时间发现一个 Python 连接数据库的连接是线程不安全的,结果惹得我哪哪儿都怀疑变量的多线程是否安全的问题,今天终于找到了正确答案,那就是 Python 内置类型 dict,list ,tupl ...
- Cheat Engine 入门操作
Cheat Engine(简称CE,中文名-作弊引擎),用于查找.修改内存数据,是游戏逆向的基础工具. 本文仅介绍基础操作. 1.打开进程 运行游戏程序,并将CE附加到进程 2.寻找数据地址,并修改数 ...