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 ...
随机推荐
- 遍历ListView,查出每一项的内容
private ListView.OnItemClickListener showItemDetail = new ListView.OnItemClickListener() { public vo ...
- CSS3: box-sizing & content-box 属性---元素的border 和 padding 影响内容的 width 和 height解决方案
/* 关键字 值 */ box-sizing: content-box; box-sizing: border-box; /* 全局 值 */ box-sizing: inherit; box-siz ...
- ZSTU4269 买iphone 2017-03-22 14:31 73人阅读 评论(0) 收藏
4269: 买iphone Time Limit: 3 Sec Memory Limit: 128 MB Submit: 1710 Solved: 316 Description 自从上次仓鼠中了 ...
- delphi FastReport 安装方法
(最近记忆力真的不行了,装了很多遍,过段时间重装delphi又不记得了,又要折腾,现在先记录下来,留给下次翻) 1.下载安装包,这里提供一个百度云盘共享链接,版本为fastreport5: https ...
- 使用zookeeper自带的zkCli.sh客户端工具实现对zk的CURD常见操作详解
一.zookeeper自带的 zkCli.sh 客户端工具 1. 应急和测试使用到的一个工具. 还有C# dirver java dirver (驱动) 二.driver的使用方式有两种 zkCl ...
- Android-GsonUtil工具类
JSON解析封装相关工具类 public class GsonUtil { private static Gson gson = null; static { if (gson == null) { ...
- Javascript的事件模型和Promise实现
1. Javascript的运行时模型——事件循环 JS的运行时是个单线程的运行时,它不像其他编程语言,比如C++,Java,C#这些可以进行多线程操作的语言.当它执行一个函数时,它只会一条路走到黑, ...
- Postgresql 分区表 一
Postgres 10 新特性 分区表 http://francs3.blog.163.com/blog/static/40576727201742103158135/ Postgres 10 之前分 ...
- Linux Guard Service - 进程分裂与脱离
进程分裂更名 void set_ps_name(char *name) { prctl(PR_SET_NAME, name); } 修改进程长名称 备份进程环境变量空间 for (i = 1; i & ...
- Selenium下拉菜单(Select)的操作-----Selenium快速入门(五)
对于一般元素的操作,我们只要掌握本系列的第二,三章即可大致足够.对于下拉菜单(Select)的操作,Selenium有专门的类Select进行处理.文档地址为:http://seleniumhq.gi ...