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. Java笔记(八)……数组

    数组的概念 同一种类型数据的集合.其实数组就是一个容器. 数组的好处 可以自动给数组中的元素从0开始编号,方便操作这些元素. 数组的格式 元素类型[] 数组名 = new 元素类型[个数]; int[ ...

  2. hibernate4.0+版本和3.0+版本的区别总结

    自己总结下hibernate4.1版本中的新特性和hibernate3.3做区别 1.数据库方言设置 <property name=”dialect”>org.hibernate.dial ...

  3. java中String、StringBuilder、StringBuffer三者的区别

    在Java项目开发中,字符串是最长使用的数据类型,而有关字符串的String.StringBuilder.StringBuffer三者又常常让人分不清楚什么时候该使用哪个. 特此整理一下. Strin ...

  4. Linux的read命令

    对于写bash脚本的朋友,read命令是不可或缺的,需要实践一下就可以了解read命令的大致用途: 编写一个脚本: #!/bin/bash # hao32 test read echo -e &quo ...

  5. 最简单实现跨域的方法:用 Nginx 反向代理

    本文作者: 伯乐在线 - 良少 .未经作者许可,禁止转载!欢迎加入伯乐在线 专栏作者. 什么是跨域 跨域,指的是浏览器不能执行其他网站的脚本.它是由浏览器的同源策略造成的,是浏览器对javascrip ...

  6. [四]JFreeChart实践三之饼图

    饼图pie 原理总结 1.准备好要显示的数据放入dataset 2.调用ChartFactory将dataset作为参数传递进去,生成chart 3.掉Servlet工具类将chart作为参数传入生成 ...

  7. 解决org.apache.subversion.javahl.ClientException: svn: E200007: Runner for 'org.tmatesoft.svn.core.wc2.SvnMerge' command have not been found

    最近使用svn的merge功能合并代码,一直出现一个错误: org.apache.subversion.javahl.ClientException: svn: E200007: Runner for ...

  8. SweetAlert 使用

    $(".delete").click(function(){ var work_name = $(this).data('name'); var item_id = $(this) ...

  9. MSSQLSERVER数据库- 判断全局临时表是否存在

    写一下今天遇到的一个问题. 今天因为一些作用域的问题,我使用了全局临时表,然后我在存储过程里使用了这么一段语句,想判断全局临时表是否存在,如果不存在,则将他DROP掉. 可是这段语句没用. if ex ...

  10. linux命令之cat

    转自:http://www.cnblogs.com/peida/archive/2012/10/30/2746968.html cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容, ...