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 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
#include<cstdio>
#include<algorithm>
using namespace std; struct Node
{
int v;
int height;
Node *lchild, *rchild;
}*root; void Insert(Node* &root,int v);
Node* NewNode(int v);
void updateHeight(Node *root);
int getHeight(Node* root);
int getBalanceFactor(Node* root);
Node* R(Node* &root);
Node* L(Node* &root); int main()
{
int n;
int 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 (NULL == root)
{
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->lchild = node->rchild = NULL;
node->v = v;
node->height = ;
return node;
} void updateHeight(Node *root)
{
root->height = max(getHeight(root->lchild),getHeight(root->rchild)) + ; } int getHeight(Node* root)
{
if (NULL == root)
{
return ;
}
else
{
return root->height;
}
} int getBalanceFactor(Node* root)
{
return getHeight(root->lchild) - getHeight(root->rchild);
} Node* R(Node* &root)
{
Node *tmp = root->lchild;
root->lchild = tmp->rchild;
tmp->rchild = root;
updateHeight(root);
updateHeight(tmp);
root = tmp;
} Node* L(Node* &root)
{
Node *tmp = root->rchild;
root->rchild = tmp->lchild;
tmp->lchild = root;
updateHeight(root);
updateHeight(tmp);
root = tmp;
}
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 ...
- 【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 ...
随机推荐
- C#多线程解决程序卡顿问题
描述: 在 C# 中,System.Threading.Thread 类用于线程的工作.它允许创建并访问多线程应用程序中的单个线程.进程中第一个被执行的线程称为主线程. 案例: static void ...
- 【开发笔记】-CentOS配置Java环境变量
如果开发java应用,经常需要配置JAVA_HOME路径,如果是通过yum安装的jdk(一般系统会自带open-jdk),下面讲述配置过程: A 定位JDK安装路径 1. 终端输入: which ja ...
- Scroller——startScroll、fling(惯性滑动)
Scroller主要用于平滑滚动,主要使用的滚动方法有:startScroll.fling. startScroll(int startX, int startY, int dx, int dy, i ...
- select用法 多并发处理
select默认最大检查套接口数量是1024,有定义 #define __NFDBITS (8 * sizeof(unsigned long)) #define __FD_SETSIZE 1024 # ...
- Android-----创建SQLite数据库
简单介绍一下Android系统内置轻便又功能强大的嵌入式数据库--SQLite. SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎,它是一款轻型的数据库,是遵守ACID的关 ...
- Centos 7 中的ulimit -n 65535 对进程的文件句柄限制不生效??
今日闲来无事,就看群里大佬吹牛逼了,偶然一条技术疑问提出来了,神奇啊,作为广大老司机技术交流群体竟然还有这么深入的研究? 大佬问:这个文件句柄限制怎么设置了/etc/security/limits.c ...
- 浅谈原子操作、volatile、CPU执行顺序
浅谈原子操作.volatile.CPU执行顺序 在计算机发展的鸿蒙年代,程序都是顺序执行,编译器也只是简单地翻译指令,随着硬件和软件的飞速增长,原来的工具和硬件渐渐地力不从心,也逐渐涌现出各路大神在原 ...
- MariaDB日志文件、备份与恢复
1. 数据库的6种日志 数据库有6种日志,分别是:查询日志.慢查询日志.错误日志.二进制日志.中继日志以及事务日志. 1> 查询日志 查询日志记录每一条sql语句,建议不开启,因为如果访问量较大 ...
- PHP写一个最简单的MVC框架
照网上看的.Framework.class.php文件是灵魂. <?php class Framework { public static function run() { //echo &qu ...
- 关于 " i=i++"是否等价于"i=i+1"的问题探讨 -Java版/C版
如题 (总结要点) 假设i=1 ,那么 i=i++ 的结果也就是2, 对吗? 不对,是1. -总结如下: 测试过程如下: 1.java版 public class Test { public stat ...