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

我的答案:
 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h> typedef int ElementType;
typedef struct AVLNode *Position;
typedef Position AVLTree; struct AVLNode {
ElementType Data;
AVLTree Left;
AVLTree Right;
int Height;
}; int Max(int a, int b)
{
return a>b?a:b;
} int GetHeight(AVLTree A)
{
int MaxH, HR, HL;
if(A) {
HL = GetHeight(A->Left);
HR = GetHeight(A->Right);
MaxH = (HL>HR)?HL:HR;
return MaxH+;
}
return -;
} AVLTree SingleLeftRotation(AVLTree A)
{
AVLTree B = A->Left;
A->Left = B->Right;
B->Right = A;
A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + ;
B->Height = Max(GetHeight(B->Left), A->Height) + ; return B;
} AVLTree SingleRightRotation(AVLTree A)
{
AVLTree B = A->Right;
A->Right = B->Left;
B->Left = A;
A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + ;
A->Height = Max(GetHeight(B->Right), A->Height) + ; return B;
} AVLTree DoubleLeftRightRotation(AVLTree A)
{
A->Left = SingleRightRotation(A->Left); return SingleLeftRotation(A);
} AVLTree DoubleRightLeftRotation(AVLTree A)
{
A->Right = SingleLeftRotation(A->Right); return SingleRightRotation(A);
} AVLTree Insert(AVLTree T, ElementType X)
{
if(!T) {
T = (AVLTree)malloc(sizeof(struct AVLNode));
T->Data = X;
T->Height = ;
T->Left = T->Right = NULL;
} else if(X < T->Data) {
T->Left = Insert(T->Left, X);
if(GetHeight(T->Left) - GetHeight(T->Right) == ) {
if(X < T->Left->Data)
T = SingleLeftRotation(T);
else
T = DoubleLeftRightRotation(T);
}
} else if(X > T->Data) {
T->Right = Insert(T->Right, X);
if(GetHeight(T->Left) - GetHeight(T->Right) == -) {
if(X > T->Right->Data)
T = SingleRightRotation(T);
else
T = DoubleRightLeftRotation(T);
}
} T->Height = Max(GetHeight(T->Left), GetHeight(T->Right)) + ;
return T;
} int main()
{
int N, i;
ElementType data;
AVLTree T; scanf("%d\n", &N);
for(i=;i<N;i++) {
scanf("%d", &data);
T = Insert(T, data);
}
if(T)
printf("%d", T->Data);
return ;
}

04-树5 Root of AVL Tree(25 分)的更多相关文章

  1. PTA 04-树5 Root of AVL Tree (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree   (25分) An AVL tree ...

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

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

  4. 1066 Root of AVL Tree (25分)(AVL树的实现)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

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

  6. 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)

    题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...

  7. 04-树4. Root of AVL Tree (25)

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

  8. pat04-树4. Root of AVL Tree (25)

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

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

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

  10. pat1066. Root of AVL Tree (25)

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

随机推荐

  1. Angular JS - 3 - Angular JS 双向数据绑定

    一 .数据绑定 1. 数据绑定: 数据从一个地方A转移(传递)到另一个地方B, 而且这个操作由框架来完成2. 双向数据绑定: 数据可以从View(视图层)流向Model(模型,也就是数据), 也可以从 ...

  2. MaxCompute studio FAQ

    1. 官方文档地址 https://help.aliyun.com/document_detail/50889.html 2. Show Table Detail 中文乱码 原因是Intellij A ...

  3. loadRunner函数之web_add_header

    web_add_header 功能:用于添加指定的报文头到下一次HTTP请求 格式:web_add_header( const char *Header, const char *Content ), ...

  4. 【Linux】清理缓存buffer/cache

    运行sync将dirty的内容写回硬盘 sync 通过修改proc系统的drop_caches清理free的cache echo 3 > /proc/sys/vm/drop_caches ech ...

  5. JavaScript 中的dispatchEvent是怎么用的

    https://zhidao.baidu.com/question/1859896201945858587.html https://www.cnblogs.com/playerlife/archiv ...

  6. Scribd每月共有超过两亿个访客、累积数亿篇以上的文件档案,Alexa全球排名200以内

    目前已登上世界300大网站,每月共有超过两亿个访客.累积数亿篇以上的文件档案.透过Flash介面的阅读器-iPaper,使用者可以在网站内浏览各种文件,由于该网站是一个文件分享平台,所有的文件都是由使 ...

  7. drf:restful概念,类继承关系,drf请求封装,drf请求流程,版本控制组件,认证组件(token),权限组件

    1.restful规范 resfful规范的概念最重要: 是一套规范,规则,用于程序之间进行数据交换的约定. 他规定了一些协议,对我们感受最直接的就是,以前写增删改查的时候需要些四个视图寒素,rest ...

  8. python 装饰器 第八步:使用类来作为装饰器参数

    #第八步:使用类作为装饰器参数 #装饰器使用的操作类 class Wish: #祈求方法 def before(): print('饭前洗洗手') #还愿方法 def after(): print(' ...

  9. Python多进程、多线程和协程简介

    一.进程和线程 进程是一个执行中的程序.每个进程都拥有自己的地址空间.内存.数据栈以及其他用于跟踪执行的辅助数据.在单核CPU系统中的多进程,内存中可以有许多程序,但在给定一个时刻只有一个程序在运行: ...

  10. Oracle数据库(一)--Oracle简介及安装

    一.Oracle简介 Oracle是美国一家著名的软件公司,也是世界上排名前三的软件公司(微软,Oracle,Adobe).Oracle数据库是一个大型的关系型数据库,在一些大型的企业之中使用的会比较 ...