1066. Root of AVL Tree
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 (<=20) 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 ythe 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 =====================================================
简单的平衡树创建,值得注意的地方是 在插入的时候 为了保证树的平衡而进行的旋转 ---------------src--------------------
#include <cstdio>
#include <stdlib.h> #define max(a,b) ((a>b)?(a):(b))
typedef struct AvlNode
{
int data ;
struct AvlNode *left ;
struct AvlNode *right ;
int height ; }AvlNode ; int height ( AvlNode *t )
{
return t == NULL ? - : t->height;
} void LLRotate ( AvlNode *& t ) //左左 对应的情况是 旋转节点的左孩子 代替传入节点,即 传入节点的左子树上面 有新增节点 为了保持平衡 需要向右单旋转
{
AvlNode *tmp = t->left ;
t->left = tmp->right ;
tmp->right = t ;
tmp->height = max(height(tmp->left) , height(tmp->right) )+;
t->height = max (height(t->left) , height(t->right )) + ;
t = tmp ;
}
void RRRotate ( AvlNode *& t )//右右 对应的情况是 传入节点的右孩子 在旋转之后 代替传入节点, 即 传入节点的右子树上面有新增节点 需要 向左单旋转
{
AvlNode *tmp = t->right ; t->right = tmp->left ;
tmp->left = t ; tmp->height = max ( height ( tmp->left) , height ( tmp->right ) ) + ;
t->height = max ( height(t->left) , height(t->right ) )+ ; t = tmp ;
} void RLRotate ( AvlNode *& t )// 对应 传入节点的 右孩子的 左子树 有新增节点,先将 右孩子向右单向旋转,使右孩子的左右子树平衡,然后 向左单向旋转 传入节点 是传入节点的左右子树达到平衡
{
LLRotate( t->right) ; RRRotate( t ) ;
} void LRRotate ( AvlNode *& t )//对应传入节点 的左孩子的 右子树上面 有新增节点, 先将 左孩子 向左 单向旋转,是的左孩子的左右子树平衡,
//然后 向右单方向旋转 传入节点 使得 传入节点 的左右子树达到平衡
{
RRRotate( t->left) ;
LLRotate( t ) ; } //由于 生成AVL 树的时候 , 需要动态生成, 所以 保证 传入的指针参数所指向的实体 在 函数中的变化是 被记录的,所以 需要使用引用符号 ‘&’
void insert ( const int &x , AvlNode *&t )
{
if ( t == NULL )
{
t = (AvlNode*)malloc(sizeof(AvlNode)) ;
t->data = x ;
t->height = ;
t->left = t->right = NULL ;
}
else if ( x < t->data )
{
insert ( x , t->left ) ; if ( height( t->left ) - height( t->right ) == )
if ( x < t->left->data )
LLRotate( t ) ;
else
LRRotate( t ) ; } else if ( t->data < x )
{
insert ( x , t->right ) ; if ( height( t->right ) - height ( t->left) == )
if ( x > t->right->data )
RRRotate( t ) ;
else
RLRotate( t ) ; } else
; t->height = max( height ( t->left ) , height(t->right)) + ;
} int main ( void )
{
AvlNode *root = NULL ; int N ;
int i ;
int num[] ; scanf("%d", &N) ; for ( i = ; i < N ; i++ )
{
scanf("%d", &(num[i] )) ;
} for ( i = ; i < N ; i++ )
{
insert( num[i] , root ) ;
} printf("%d" , root->data) ;
return ;
}
1066. Root of AVL Tree的更多相关文章
- PAT 1066 Root of AVL Tree[AVL树][难]
1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 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 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***
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分)
PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...
- PTA (Advanced Level) 1066 Root of AVL Tree
Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...
- 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 ...
随机推荐
- HDU 2682
思路:由于题目对能相连的点有限制,必须将这些点处理,能相连的点合并到一个集合中,最后查看是否所有点都在一个集合里,若都在说明是一个连通图,存在最小生成树,否则图不连通,不存在最小花费. #includ ...
- “System.Exception”类型的异常在 NHibernate.dll 中发生,但未在用户代码中进行处理
“System.Exception”类型的异常在 NHibernate.dll 中发生,但未在用户代码中进行处理 其他信息: OCIEnvCreate 失败,返回代码为 -,但错误消息文本不可用. 如 ...
- .Net 程序的运行
1. 用.Net开发的程序运行的某台机器上必须安装.Net FrameWork 2. .Net FrameWork向下兼容的实现 在安装4.0的时候,会把3.5,2.0等低版本的都装上,从而实现向下兼 ...
- (Step by Step)How to setup IP Phone Server(VoIP Server) for free.
You must have heard about IP Phone and SIP (Software IP Phone).Nowadays standard PSTN phone are bein ...
- SIP SDP RTSP RTP RTCP webrtc
rfc1889 rfc2326 rfc3261 rfc3550 rfc3856 rfc6120. SIP SDP RTSP RTP RTCP,就像他们出现的顺序一样,他们在实际应用中的启用 ...
- X265编码效率仍然低
本次测试软件环境:Intel Celeron双核 2.60 Ghz CPU; 4GB 内存:安装 Ubuntu 13.04 hzsx@hzsx-server:~$ lsb_release -a No ...
- hdoj 3861 The King’s Problem【强连通缩点建图&&最小路径覆盖】
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- php-fpm正在生成页面时,浏览器刷新后,php-fpm会退出吗?
好久没写博客了,因为没有啥可写. 之所以有此疑问,是因为看了一篇大牛的文章:PHP升级导致系统负载过高问题分析.看完后,其中有些文字触发了我这个想法,也想验证一下. 方案,用tcpdump抓包,用st ...
- Jquery JS 正确比较两个数字大小的方法
if(2 > 10) { alert("不正确!"); } 此比较不会是想要的结果:它相当于2 >1,把10的第一位取出来比较. ——————————————————— ...
- Away3D 的实体收集器Bug
最近在改Away3D源码的时候遇到个很郁闷的问题,发现创建的Mesh 释放不掉. 分析源码发现 EntityListItemPool 类中逻辑Bug在getItem()函数中发现_poolSize 对 ...