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 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 (≤) 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
题目分析:写一个AVL树 背课文是好的 但也要理解清除AVL树的原理 之后还要学习各种平衡树 如红黑树什么的
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
typedef struct TNode* Tree;
struct TNode {
int Data;
Tree TL;
Tree TR;
int Height; //要初始化为-1;
};
int GetHeight(Tree T) {
if (T)
return T->Height;
else
return -;
}
Tree singleLeftRotate(Tree T) {
Tree TL = T->TL;
T->TL = TL->TR;
TL->TR = T;
T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + ;
TL->Height = max(GetHeight(TL->TL), GetHeight(TL->TR)) + ;
return TL;
}
Tree singleRightRotate(Tree T) {
Tree TR = T->TR;
T->TR = TR->TL;
TR->TL = T;
T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + ;
TR->Height = max(GetHeight(TR->TL), GetHeight(TR->TR)) + ;
return TR;
}
Tree doubleLeftRightRotate(Tree T) {
T->TL = singleRightRotate(T->TL);
return singleLeftRotate(T);
}
Tree doubleRightLeftRotate(Tree T) {
T->TR = singleLeftRotate(T->TR);
return singleRightRotate(T);
}
Tree Insert(Tree T,int data) {
if (!T)
{
T = new TNode();
T->Data = data;
T->Height = ;
T->TL = T->TR = NULL;
}
else if (data > T->Data) {
T->TR = Insert(T->TR, data);
T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + ;
if (GetHeight(T->TR) - GetHeight(T->TL) == ) {
if (data > T->TR->Data)
T=singleRightRotate(T);
else
T=doubleRightLeftRotate(T);
}
}
else {
T->TL = Insert(T->TL, data);
T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + ;
if (GetHeight(T->TL) - GetHeight(T->TR) == ) {
if (data < T->TL->Data)
T=singleLeftRotate(T);
else
T=doubleLeftRightRotate(T);
}
}
return T;
} int main()
{
int N;
Tree T = NULL;
int data;
cin >> N;
for (int i = ; i < N; i++)
{
cin >> data;
T = Insert(T, data);
}
cout << T->Data;
}
1066 Root of AVL Tree (25分)(AVL树的实现)的更多相关文章
- 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 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 ...
- 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 ...
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- 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 ...
- pat1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 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 ...
随机推荐
- Android开发进阶 -- 通用适配器 CommonAdapter
在Android开发中,我们经常会用到ListView 这个组件,为了将ListView 的内容展示出来,我们会去实现一个Adapter来适配,将Layout中的布局以列表的形式展现到组件中. ...
- php获取当前周的第一天与最后一天
1 2 3 4 5 6 7 8 9 10 // 当前日期 $sdefaultDate = date("Y-m-d"); // $first =1 表示每周星期一为开始日期 ...
- 看逐浪CMS技术小哥做SVG动画(附使用Bodymovin和Lottie将Adobe After Effects(AE)程式转为 HTML5/Android/iOS原生的动画全过程-即AE转svg\canvas\html5动画)
名词解解释 adobe After Effects AE:adobe After Effects,adobe公司的专业视频制作软件. Bodymovin插件预览 Bodymovin:是一个AE的插 ...
- django验证码框架captcha
1.安装 2.在settings.py 安装app中添加 3.添加url 4.运行makemigrations和migrate 5.运用 在form表单中定义 view中返回form表单 在前端htm ...
- drf呼啦圈
呼啦圈 1.1 表结构设计 不会经常变化的值放在内存:choices形式,避免跨表性能低. 分表:如果表中列太多/大量内容可以选择水平分表 表自关联 from django.db import mod ...
- H5页面,输入框的光标,如果页面上下滑动光标停留在页面上,除了输入框外,松手过了一段时间才跑回输入框里面
有点类似这种情况 其中一个博主描述得比较详细,主要还有图 我是直接在App.vue主文件那里添加一下代码,主要是添加一个监听器,如果touchmove的时候就会触发让其失焦,就会消失那个光标,需要再次 ...
- webstorm 开新项目 setting 设置@目录别名 add @ (languages & Framewors - Javascript - Webpack 4. setting eslint enable
webstorm 开新项目 setting 设置@目录别名 add @ (languages & Framewors - Javascript - Webpack 4. setting esl ...
- 039.集群网络-Pod和SVC网络实践
一 Pod和SVC网络 1.1 实践准备及原理 Docker实现了不同的网络模式,Kubernetes也以一种不同的方式来解决这些网络模式的挑战.本完整实验深入剖析Kubernetes在网络层是如何实 ...
- 太赞了!阿里几位工程师重写了 《Java 并发编程》
事情是这样的,前些日子和得知一个读者在准备阿里的面试,我蛮有兴趣的跟他聊了起来,随着话题越来越深入,我发现这位读者有意思,他和几位阿里的工程师之前编写了一本 concurrent.redspider. ...
- 李飞飞团队最新论文:基于anchor关键点的类别级物体6D位姿跟踪
6-PACK: Category-level 6D Pose Tracker with Anchor-Based Keypoints 论文地址: 6-PACK: Category-level 6D P ...