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的更多相关文章

  1. 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, ...

  2. PAT甲级1066. Root of AVL Tree

    PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...

  3. pat 甲级 1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. PAT 甲级 1066 Root of AVL Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...

  9. 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 ...

随机推荐

  1. [Tommas] dateadd() 函数用法

    DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法 DATEADD(datepart,number,date) date 参数是合法的日期表达式.number 是您希望添加的间隔数:对于 ...

  2. LoadRunner调用Java程序—性能测试

    为了充分利用LoadRunner的场景控制和分析器,帮助我们更好地控制脚本加载过程,从而展现更直观有效的场景分析图表.本次将重点讨论LoadRunner如何调用Java测试代码,完成压力测试. 通常我 ...

  3. 用opencv画矩形打上马赛克Mosaic

    /*----------------------------------------------------------------------------- *   *   版权声明: *   可以 ...

  4. SVN记住用户名和密码后如何修改

    今天遇到一个SVN检出代码用户验证问题.由于自己最近参与了好几个项目,一时间忙不过来.所以希望跟着自己的试用期的同事帮我测试一下刚修改完成的新功能是否有问题.但是该同事没有项目中权限,正好今天恰逢星期 ...

  5. Effect-Compiler Tool(fxc.exe)

    提前编译shader文件,提高运行时的效率. refer to http://msdn.microsoft.com/en-us/library/windows/desktop/bb509710%28v ...

  6. iOS开发——GCD多线程详解

    GCD多线程详解 1. 什么是GCD Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,简单来说,GCD就是iOS一套解决多线程的机制,使用GCD能够最大限度简化多线程 ...

  7. Tyvj P1729 文艺平衡树 Splay

    题目: http://tyvj.cn/p/1729 P1729 文艺平衡树 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 此为平衡树系列第二道:文艺平衡树 ...

  8. Storm系列(三)Topology提交过程

    提交示例代码: 1  ); // 设置一个ack线程 9      conf.setDebug(true); // 设置打印所有发送的消息及系统消息 10      StormSubmitter.su ...

  9. Codeforces 295C Greg and Friends

    BFS+DP.dp[i][j][0]表示有i个50kg,j个100kg的人在左岸,dp[i][j][1]表示有i个50kg,j个100kg的人在右岸.用BFS求最短路的时候记录到达该状态的可能情况. ...

  10. 【转】Android平台下利用zxing实现二维码开发

    http://www.cnblogs.com/dolphin0520/p/3355728.html 现在走在大街小巷都能看到二维码,而且最近由于项目需要,所以研究了下二维码开发的东西,开源的二维码扫描 ...