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 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 分)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)
题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...
- 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 ...
- 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 ...
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- pat1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
随机推荐
- flask 根路由在蓝图中
- centos7不能远程登陆的方案
网上找了很多,就算百度经验写的都是坑,代码如下: BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INI ...
- sql查询某个时间内的数据
hour) 七天之前的数据 SELECT * FROM commodity_order where create_time <= (now()-INTERVAL 7 DAY) order by ...
- 使用命令将ipa包上传到蒲公英
参考:官文文档 请根据开发者自己的账号,将其中的 uKey 和 _api_key 的值替换为相应的值. curl -F "file=@/Users/chenpeisong/Desktop ...
- ReplicatorLayer 复制图层
使用文档介绍: #import <QuartzCore/CALayer.h> NS_ASSUME_NONNULL_BEGIN CA_CLASS_AVAILABLE (10.6, 3.0, ...
- Flink容错机制(checkpoint)
checkpoint是Flink容错的核心机制.它可以定期地将各个Operator处理的数据进行快照存储( Snapshot ).如果Flink程序出现宕机,可以重新从这些快照中恢复数据. 1. ch ...
- OpenCV2.4.8 + CUDA7.5 + VS2013 配置
配置过程主要参考:https://initialneil.wordpress.com/2014/09/25/opencv-2-4-9-cuda-6-5-visual-studio-2013/ 1.为什 ...
- vscode 配置 golang开发环境
如果你使用golang,那么强烈建议你采用vscode作为IDE. 1. 首先在vscode 当中安装go插件,如上图 2. 配置 %AppData%\Code\User\settings.json ...
- Spring Cloud的简单介绍
参考地址: https://mp.weixin.qq.com/s/wG4CTLORnvemkjUsZ7YP6Q 从一个例子开始 对于这样的"大"问题,通常需要拆解成小问题来回答.要 ...
- django-2-目录结构
django是MVC或者叫MTV框架