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 ...
随机推荐
- logback 日志相关
日志相关: logback:https://www.cnblogs.com/gavincoder/p/10091757.html https://www.cnblogs.com/hhhshct/p/9 ...
- WPF 的 Application.Current.Dispatcher 中,Dispatcher 属性一定不会为 null
原文:WPF 的 Application.Current.Dispatcher 中,Dispatcher 属性一定不会为 null 在 WPF 程序中,可能会存在 Application.Curren ...
- GC偏好
GC偏好 测序中的GC偏好指的是基因组上GC含量在50%左右的区域更容易被测到,产生的reads更多,这些区域的覆盖度更高, 在高GC或者低GC区域,不容易被测到,产生较少的reads,这些区域的覆盖 ...
- UCOSIII互斥信号量
互斥信号量可以解决优先级反转问题 优化后现象 优化方法:L和H等待同一个信号量的时候,将L任务优先级提至H相同优先级 实验举例 void start_task(void *p_arg) { OS_CR ...
- linux下载并安装redis
1 到http://redis.io/download 官网下下载最新稳定的redis 2 下载的redis安装包移动到要安装的位置 3 解压安装包 tar zxvf redis-3.0.5.tar. ...
- Linux在丢失的情况下重置密码
1.开机菜单是 移动光标到第一行 --敲击e 2.找到UTF-8,加上空格rd.break,敲击ctrl+x 3.输入以下命令 mount -o remount,rw /sysroot chroot ...
- ICS2019汇编实验在Linux下使用GDB调试程序
- Spring -09 -在Spring工程 中加载 properties 文件 -为某个属性添加注解赋初值
1.在src 下新建 xxx.properties 文件,不要任意加空格,注明jdbc等标识名!2.在spring 配置文件中先引入xmlns:context,在下面添加2.1如果需要记载多个配置文件 ...
- 51nod 1720 祖玛
吉诺斯在手机上玩祖玛的游戏.在这个游戏中,刚开始有n个石头排成一排,第i个石头的颜色是ci.游戏的目标是尽可能快的把所有石头都消掉. 每一秒钟,吉诺斯可以选择一段连续的子段,并且这个子段是回文,然后把 ...
- 【转】Deep dive into pipe function in RxJS
原文: https://codewithstyle.info/deep-dive-pipe-function-rxjs/ --------------------------------------- ...