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 ...
随机推荐
- [King.yue]Grid列赋值文本,隐藏Value
例:public string InputFormat 加扩展属性:public string InputFormatText 构造函数中根据Key取到Value的值: var data = Data ...
- selenium2.0 处理各种窗口问题解决方法
selenium2.0处理muti-Windows . Frames .Popup Dialogs selenium2.0处理多窗口,弹窗等,只需要调用WebDriver 嵌套类:TargetLoca ...
- 洛谷P1189 逃跑的拉尔夫(SEARCH)
洛谷1189 SEARCH 题目描述 年轻的拉尔夫开玩笑地从一个小镇上偷走了一辆车,但他没想到的是那辆车属于警察局,并且车上装有用于发射车子移动路线的装置. 那个装置太旧了,以至于只能发射关于那辆车的 ...
- 搭建Windows Azure开发环境-Azure虚拟机
概念 这一节是关于让设置了Azure的虚拟机,它包括的Visual Studio 2013 RC旗舰版和SQL Server 2012 Express的 . 在此动手实验中,您将探索建立使用Windo ...
- HW4.46
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 百度参投 Uber中国12亿美元融资已到账
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- php的fread函数的一个巨大的坑
先看看fread的manual,如下: http://php.net/manual/en/function.fread.php fread() reads up to length bytes fro ...
- XML与XHTML
什么是XML XML的基本格式 XML的定义文档 HTML5的文档定义 XHTML1.0的文档定义 XHTML1.0标记格式 12.1 什么是XML XML中文翻译为可扩展标记语言,顾名思义,它比HT ...
- Python学习(2)
python基础学习(二)2.1 python定义函数用def,没有返回类型?def myabs(x) if x>0: return x python定义的函数可以多个直接一起返回,这一点和ja ...
- VM启用ISO共享
在SCVMM中可以启用ISO共享,如下图: 如果不勾选共享镜像复选框,则vmm会把镜像文件通过网络复制到Hyper-v主机下的虚拟机配置文件夹中 配置步骤如下: 1.新建一个共享文件夹(存放ISO), ...