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. express中 使用session与cookie

    1.express如何使用session与cookie : https://www.jianshu.com/p/1839e482274e  或  https://www.cnblogs.com/chy ...

  2. php使用curl实现get和post请求的方法,数据传输urldecode和json

    PHP支持CURL库,利用URL语法规定来传输文件和数据的工具,支持很多协议,包括HTTP.FTP.TELNET等. 优点:是可以通过灵活的选项设置不同的HTTP协议参数,并且支持HTTPS.CURL ...

  3. 北风设计模式课程---UML类图各符号含义

    北风设计模式课程---UML类图各符号含义 一.总结 一句话总结: 用脑子,挺好记的:实线关系肯定比虚线重,箭头.三角形.菱形的关系肯定依次加重,三角形是继承和实现, 1.UML类图中 线+箭头 表示 ...

  4. iphone-命令行编译之--xcodebuild

    参考 : https://www.cnblogs.com/xiaodao/archive/2012/03/01/2375609.html

  5. linux下载文件到本地命令

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/mengda_lei/article/de ...

  6. 【Web API]无法添加AttributeRoutes的解决方案

    1.按照微软官方文档,如果要使用AttributeRoutes,需要在APP_START里的WebApiConfig.cs的Register方法中添加一行:config.MapHttpAttribut ...

  7. 把 MongoDB 当成是纯内存数据库来使用(Redis 风格)

    基本思想 将MongoDB用作内存数据库(in-memory database),也即,根本就不让MongoDB把数据保存到磁盘中的这种用法,引起了越来越多的人的兴趣.这种用法对于以下应用场合来讲,超 ...

  8. ZwQueryDirectoryFile用法

    1. 当返回值为STATUS_SUCCESS时,返回的字节数保存在IoStatusBlock.Information字段中: 2. 如果FileName字段被指定了,那么对于同时指定的FileHand ...

  9. 如何获得一个干净的 gnome 开发环境?

    下载 stage3-amd64-systemd-xxxxxxxx.tar.bz2 eselect profile set default/linux/amd64/17.0/desktop/gnome/ ...

  10. 将QTP运行时的错误截图上传到QC

    Class QCImageErrorCapture Sub Class_Terminate() 'Check if the current test has failed. If failed the ...