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 (≤) which is the total number of keys to be inserted. Then Ndistinct 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
#include<cstdio>
#include<algorithm>
using namespace std;
struct Node{
int v,height;
Node* lchild,*rchild;
}*root; Node* newNode(int v){
Node* node = new Node;
node -> v = v;
node -> lchild = node -> rchild = NULL;
node -> height = ;
return node;
} int getHeight(Node* root){
if(root == NULL) return ;
return root -> height;
} void updateHeight(Node* root){
root -> height = max(getHeight(root -> lchild),getHeight(root -> rchild))+;
} int getBalanceFactor(Node* root){
return getHeight(root -> lchild) - getHeight(root -> rchild);
} void R(Node* &root){
Node* temp = root -> lchild;
root -> lchild = temp -> rchild;
temp -> rchild = root;
updateHeight(root);
updateHeight(temp);
root = temp;
}
void L(Node* &root){
Node* temp = root -> rchild;
root -> rchild = temp -> lchild;
temp -> lchild = root;
updateHeight(root); //先更新root(子树)的高度
updateHeight(temp);
root = temp;
} void insert(Node* &root, int v){
if(root == NULL){
root = newNode(v);
return;
}
if(v < root -> v){
insert(root -> lchild,v);
updateHeight(root);
if(getBalanceFactor(root) == ){
if(getBalanceFactor(root -> lchild) == ){
R(root);
}else if(getBalanceFactor(root -> lchild) == -){
L(root -> lchild);
R(root);
}
}
}else{
insert(root -> rchild,v);
updateHeight(root);
if(getBalanceFactor(root) == -){
if(getBalanceFactor(root -> rchild) == -){
L(root);
}else if(getBalanceFactor(root -> rchild) == ){
R(root -> rchild);
L(root); }
}
}
} int main(){
int n,v;
scanf("%d",&n);
for(int i = ; i < n; i++){
scanf("%d",&v);
insert(root,v);
}
printf("%d",root -> v);
return ;
}
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 (25分)
PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...
- 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)
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)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- 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 ...
- PAT (Advanced Level) 1066. Root of AVL Tree (25)
AVL树的旋转.居然1A了.... 了解旋转方式之后,数据较小可以当做模拟写. #include<cstdio> #include<cstring> #include<c ...
随机推荐
- angularjs 动态加载指令------编译服务$compile
场景: 我们写了一个自定义的指令,这条指令需要一些数据,而这些数据需要在某些操作之后才能就绪,这时候,我们就需要在数据就绪之后,动态加载指令. 示例: js: $scope.$watch('repor ...
- Postman请求后台报错:Invalid character found in method name. HTTP method names must be tokens
在使用Postman请求后台时Postman出现 开发工具控制台报 信息: Error parsing HTTP request header Note: further occurrences of ...
- gitlab异地备份并验证MD5值
最近公司发生了蛮多事情的,唉,咱也不管问啊,好好干活吧 需求 把gitlab的备份文件异地备份一份,备份无论失败还是成功通知某邮箱 实现思路 先rsync文件过去,判断rsync这个步骤有没有成功,失 ...
- Java经典逻辑编程50题 (转)
注: 转自https://blog.csdn.net/Alias_fa/article/details/52985112 (摘了题目部分和部分分析,想看原作代码的见原作链接) [程序1] 題目:古典 ...
- Python学习日记(十九) 模块导入
模块导入 当文件夹中有这样一个自定义的command模块 在它的内部写下下列代码: print('这个py文件被调用!') def fuc(): print('这个函数被调用!') 然后我们在comm ...
- 腿部——哑铃&杠铃
- tcpdump截帧工具使用
一.tcpdump介绍 tcpdump是Linux下功能强大的截帧工具,相当于windows下的wireshark一下,只是操作方式是命令行的,需要熟悉Linux命令行操作. 常用的Linux ...
- Linux环境变量$PATH
3. 使用env命令显示所有的环境变量 # env HOSTNAME=redbooks.safe.org PVM_RSH=/usr/bin/rsh Shell=/bin/bash TERM=xterm ...
- ISCC之msc_无法运行的exe
打开hxd,里面老长一串base64 解码试了一下,解出来是png文件头,但是图片有错误 百度了一下,PNG文件头是89 50 4E 47 0D 0A 1A 0A 再回去看 改成0A了事, 出来一张二 ...
- 介绍一个二次排序的小技巧(best coder27期1001jump jump jump)
先来描述一下问题: 问题描述 有n小孩在比赛跳远,看谁跳的最远.每个小孩可以跳3次,这个小孩的成绩就是三次距离里面的最大值.例如,一个小孩跳3次的距离分别时10, 30和20,那么这个小孩的成绩就是3 ...