04-树5 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;
int height;
Node* lchild;
Node* rchild;
}*root; int getHeight(Node* root);
void updateHeight(Node* root);
int getBalanceFactor(Node* root);
Node* NewNode(int v);
void Insert(Node* &root, int v);
void L(Node* &root);
void R(Node* &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 ;
}
void Insert(Node* &root, int v){
if(root == NULL){
root = NewNode(v);
return;
}
if(root->v > 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);
}
}
}
} Node* NewNode(int v){
Node* node = new Node;
node->v = v;
node->lchild = node->rchild = NULL;
node->height = ;
return node;
} void updateHeight(Node* root){
root->height = max(getHeight(root->lchild),getHeight(root->rchild))+;
} int getHeight(Node* root){
if(root == NULL) return ;
return root->height;
} int getBalanceFactor(Node* root){
return getHeight(root->lchild) - getHeight(root->rchild);
} void L(Node* &root){
Node* temp = root->rchild;
root->rchild = temp->lchild;
temp->lchild = root;
updateHeight(root);
updateHeight(temp);
root = temp;
} void R(Node* &root){
Node* temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
updateHeight(root);
updateHeight(temp);
root = temp;
}
04-树5 Root of AVL Tree (25 分)的更多相关文章
- 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 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...
- 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 ...
- 04-树5 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 分)(AVL树建树模板)
题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...
- 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 ...
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 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 ...
随机推荐
- docker-compose示例与命令介绍
一.docker-compose.yml示例 version: ‘2‘ #指定compose版本 services: log: #服务名称 image: vmware/harbor-log #指定镜像 ...
- RocketMQ 服务器3模式
22 a b-s---------------------------sh mqbroker -c $ROCKETMQ_HOME/conf/2m-2s-async/broker-a.propertie ...
- linux每天一小步---tail命令详解
1 命令功能 tail命令用于显示文件中末尾的内容(默认显示最后10行内容) 2 命令语法 tail [选项参数] [文件名1] [文件名2] 3 命令参数 -f 用于循环读取文件的内容,监视文件的 ...
- Computer
Computer 1. Ctrl+N .根据惯例,“Control”.“Shift” 以及 “Alternate” 按键将以 Ctrl.Shift 以及 Alt 来表示,需要特别指出的是,其中第一个按 ...
- 深海划水队项目--七天冲刺之day7
站立式会议: 昨天已完成的工作:设置游戏按键,检查重合.检查是否超出边界.检查是否可以下落,方块的硬下落和软下落方法. 今日已完成的工作:添加方法:方块的着陆和消除. 工作中遇到的困难:在消除方块的时 ...
- Github注册及心得
注册Github流程: 1.搜索www.github.com 2.有两个按钮sign up(注册).sign in(登入)
- [51单片机] nRF24L01 无线模块 串口法命令 通过无线控制另一个的灯
>_<!概述: 这是在上一个的基础上通过按键发送4种不同命令来控制接收端的LED灯亮的改进版(上一个:http://www.cnblogs.com/zjutlitao/p/3840013. ...
- CI-Excel-Generation-Library php导出excel乱码。
修改 private function generate($headers, $data) { $this->set_headers(); $data = " ...
- (zxing.net)一维码UPC E的简介、实现与解码
UPC(Universal Product Code)码是最早大规模应用的条码,其特性是一种长度固定.连续性的条 码,目前主要在美国和加拿大使用,由于其应用范围广泛,故又被称万用条码. UPC码仅可 ...
- [C#]C#中ToString()和Convert.ToString()的区别
一.一般用法说明 ToString()是Object的扩展方法,所以都有ToString()方法;而Convert.ToString(param)(其中param参数的数据类型可以是各种基本数据类型, ...