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. codeforces 652E Pursuit For Artifacts 边双连通分量

    题意:n个点,m条边的无向图,有的边上有标记,每条边只能走一次 给你一个起点,一个终点,询问是否能找到从起点到终点的路径,这条路径至少包含一条含有标记的边 分析:然后边双缩点 下面介绍一下边双的性质 ...

  2. mac 卸载 XCode

    http://blog.csdn.net/songques/article/details/7244144

  3. PHP字符串操作常用函数

    /* * 确定字符串长度 * int strlen(string str) * 比较两个字符串 * 1.strcmp函数对两个字符串进行二进制安全的比较,并区分大小写 * int strcmp(str ...

  4. ubuntu下git安装及连接github

    1.安装 sudo apt-get install git git-core git-gui git-doc git-svn git-cvs gitweb gitk git-email git-dae ...

  5. HDOJ-ACM1018(JAVA)

    题意: 求n!的位数,0<n<10^7 思路:log10(1)+log10(2)+···+log10(n) = log10(n!)   [题目的考点就在这吧] 解题: import jav ...

  6. jquery实现公告上下滚动显示

    js: // JavaScript Documentfunction b(){ t = parseInt(x.css('top')); y.css('top','19px'); x.animate({ ...

  7. js关键字

    这已经是我第二次遇到这个问题了..关于关键字,用来做函数的命名,,,在高清上没问题,标清上秒死...页面都出不来...wtf...做个记录.. js关键字http://www.itxueyuan.or ...

  8. js基础一

    1.声明提升:变量的声明提升,函数的声明提升,但函数赋值表达式不会提升: foo(); // 正常运行,因为foo在代码运行前已经被创建 function foo() {} foo(); // 出错: ...

  9. TOR的使用

    使用步骤: 1.配置,该计算机是否需要通过代理访问互联网?选否 2.该计算机的防火墙是否仅允许特定端口的互联网连接?选否 3.互联网服务提供商(ISP)是否对Tor网络连接进行了封锁或审查?选是 4. ...

  10. 自助用户选择VM Network

    在VMM中为用户所属角色分配“作者VM网络”权限后,用户才可以在部署虚机的选择不同的VM Network,否则用户只能使用模板上所使用的VM Network,无法进行选择