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 ...
随机推荐
- mybatis源码分析之01环境搭建
直接使用maven搭建一个mybatis的运行环境 1. pom.xml <?xml version="1.0" encoding="UTF-8"?> ...
- LOJ 2978 「THUSCH 2017」杜老师——bitset+线性基+结论
题目:https://loj.ac/problem/2978 题解:https://www.cnblogs.com/Paul-Guderian/p/10248782.html 第 i 个数的 bits ...
- LR之分析
1.Errors(错误统计) 每秒错误数:数值越小越好,通过这个图可以知道,当负载增加的时候,定位何时系统在负载下开始不稳定甚至出错. 2.Transaction(事务) average transa ...
- JS基础入门篇(四)—this的使用,模拟单选框,选项卡和复选框
1.this的使用 this js中的关键字 js内部已经定义好了,可以不声明 直接使用 this的指向问题 1. 在函数外部使用 this指向的是window 2. 在函数内部使用 有名函数 直接调 ...
- jquery 自定义类
jQuery自定义类封装: (function ($) { $.DragField = function (arg) { var name = "你好"; //这个是私有变量,外部 ...
- CSS-inline-block 间隙
间隙产生的原因是`inline-block`对外是`inline`,对内是`block`.`inline`会将连续的空白符解析为一个空格(如:下面示例的两个`li`之间的后面的换行空格).取消间隙的方 ...
- 102.kaldi 斯坦福语音识别工具的编译
接着上一节,在编译完了openFST有限状态机之后,便开始了最重要部分,语音识别插件的编译过程 首先看目录是如下所示的 1.首先添加openBLAS的支持,这是一个矩阵运算库,个人觉得这个矩阵运算库 ...
- Transition 过渡/转场动画(一)
UIViewController 的转场效果 当viewController通过push 或 present 进行转场时, 系统自带的动画是从右侧push进来一个新的viewControler (或从 ...
- PAT_A1077#Kuchiguse
Source: PAT A1077 Kuchiguse (20 分) Description: The Japanese language is notorious for its sentence ...
- checkBox的全选与全不选
//初始加载 $(function () { LoadCart(); }); var RMB = 0; var Index = 0; var shu = 0; var ResultStr = &q ...