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 ...
随机推荐
- Core + Vue 后台管理基础框架2——认证
1.前言 这块儿当时在IdentityServer4和JWT之间犹豫了一下,后来考虑到现状,出于3个原因,暂时放弃了IdentityServer4选择了JWT: (1)目前这个前端框架更适配JWT: ...
- JVM笔记-运行时内存区域划分
1. 概述 Java 虚拟机在执行 Java 程序的过程中会把它管理的内存划分为若干个不同的数据区域.它们各有用途,有些随着虚拟机进程的启动一直存在(堆.方法区),有些则随着用户线程的启动和结束而建立 ...
- 简说Python之flask-SQLAlchmey的web应用
目录 原生语句操作MySQL数据库 1.安装MySQL 2.MySQL设置用户和权限 3.用PyMySQL操纵MySQL数据库 4. CRUD增,删,改,查 使用SQLAlchemy 1.安装SQLA ...
- MQTT抓包分析
1. 概述 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(Publish/Subscribe)模式的轻量级通讯协议,该 ...
- 读源码 - stagesepx
目录 背景 一.概述 stagesepx 二.源码中优雅的用法 优雅的赋值/返回 递归方法新建多级目录 更简洁的日志输出 格式化输出的另一种姿势 基于生成器读取多个文件 sorted进阶 入参类型限制 ...
- 038.集群网络-K8S网络实现
一 Kubernetes网络实现 1.1 Kubernetes网络优势 在实际的业务场景中,业务组件之间的关系十分复杂,微服务的理念更是让应用部署的粒度更加细小和灵活.为了支持业务应用组件的通信,Ku ...
- 如何使用Postman编写Testlink测试用例
Postman2Testlink 通过Postman快速操作testlink测试用例.测试套件.测试计划.添加关键词.添加自定义字段等等. 工具地址 https://github.com/liyinc ...
- Journal of Proteome Research | SAAVpedia: identification, functional annotation, and retrieval of single amino acid variants for proteogenomic interpretation | SAAV的识别、功能注释和检索 | (解读人:徐洪凯)
文献名:SAAVpedia: identification, functional annotation, and retrieval of single amino acid variants fo ...
- [Bugku]Web题解
bugku地址链接:https://ctf.bugku.com 1.web2 浏览器就显示一堆动态笑脸,时间长了密集恐惧症了. 解法1: F12查看源码 解法2: 地址栏输入: view-source ...
- ASP.NET Core去掉HTTPS配置和SSL证书
如果你的项目一不小心配置了https 右击项目=>属性=>调试=>启用SSL=>选择去掉 测试