PAT1066(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 (<=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
给出一个插入的序列,最后输出AVL树的根
主要就是针对AVL树的失衡的几种情况,分别进行重新平衡
LL型:由于在A左子树根节点的左子树上插入结点,使A的平衡由1增至2.此时可以通过右旋来实现。所谓右旋就是将根节点作为其左子树的右子树。
若原左子树上有右结点,此时作为根节点的左子树。
RR型:由于在A右子树根节点的右子树上插入节点,使A的平衡由-1变成-2.此时可以通过左旋实现。所谓左旋就是将根节点作为其右子树的左子树。
若原右子树有左孩子,此时作为根节点的右孩子。
LR型:由于在A左子树根节点的右子树上插入节点,使A的平衡由1增至2.此时可现对root->left进行一次左旋,再对root进行一次右旋
RL型:由于在A右子树根节点的左子树上插入节点,使A的平衡由-1变成-2.此时可现对root->right进行一次右旋,在对root进行一次左旋。
具体代码如下:
#include<iostream>
#include<cstdio>
using namespace std;
struct Node
{
int val;
Node *left,*right;
};
Node* rotateright(Node *root) //右旋 LL型
{
Node *t=root->left;
root->left=t->right;
t->right=root;
return t;
}
Node* rotateleft(Node *root) //左旋 RR型
{
Node *t=root->right;
root->right=t->left;
t->left=root;
return t;
}
Node* rotateleftright(Node *root) //LR型
{
root->left=rotateleft(root->left);
return rotateright(root);
}
Node* rotaterightleft(Node *root)
{
root->right=rotateright(root->right);
return rotateleft(root);
}
int getHeight(Node *root)
{
if(root==NULL)
return ;
else
return max(getHeight(root->left),getHeight(root->right))+;
}
Node* build(Node *root,int val)
{
if(root==NULL)
{
root=new Node();
root->val=val;
root->left=root->right=NULL;
}
else if(val<root->val)//插入到左子树中
{
root->left=build(root->left,val);
if(getHeight(root->left)-getHeight(root->right)==)
{
root=val<root->left->val ?rotateright(root):rotateleftright(root); //如果是LL型,做右旋,否则先左旋后右旋
}
}
else
{
root->right=build(root->right,val);
if(getHeight(root->left)-getHeight(root->right)==-)
root=val>root->right->val ? rotateleft(root):rotaterightleft(root);
}
return root;
}
int main()
{
int n;
scanf("%d",&n);
Node *root=NULL;
for(int i=;i<=n;i++)
{
int val;
scanf("%d",&val);
root=build(root,val);
}
printf("%d\n",root->val);
}
PAT1066(AVL树)的更多相关文章
- 算法与数据结构(十一) 平衡二叉树(AVL树)
今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...
- AVL树原理及实现(C语言实现以及Java语言实现)
欢迎探讨,如有错误敬请指正 如需转载,请注明出处http://www.cnblogs.com/nullzx/ 1. AVL定义 AVL树是一种改进版的搜索二叉树.对于一般的搜索二叉树而言,如果数据恰好 ...
- AVL树
AVL树 在二叉查找树(BST)中,频繁的插入操作可能会让树的性能发生退化,因此,需要加入一些平衡操作,使树的高度达到理想的O(logn),这就是AVL树出现的背景.注意,AVL树的起名来源于两个发明 ...
- AVL树的平衡算法(JAVA实现)
1.概念: AVL树本质上还是一个二叉搜索树,不过比二叉搜索树多了一个平衡条件:每个节点的左右子树的高度差不大于1. 二叉树的应用是为了弥补链表的查询效率问题,但是极端情况下,二叉搜索树会无限接近 ...
- 【数据结构】平衡二叉树—AVL树
(百度百科)在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增 ...
- 数据结构图文解析之:AVL树详解及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- 数据结构之平衡二叉树(AVL树)
平衡二叉树(AVL树)定义如下:平衡二叉树或者是一棵空树,或者是具有以下性质的二叉排序树: (1)它的左子树和右子树的高度之差绝对值不超过1: (2)它的左子树和右子树都是平衡二叉树. AVL树避免了 ...
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...
- 论AVL树与红黑树
首先讲解一下AVL树: 例如,我们要输入这样一串数字,10,9,8,7,15,20这样一串数字来建立AVL树 1,首先输入10,得到一个根结点10 2,然后输入9, 得到10这个根结点一个左孩子结点9 ...
随机推荐
- 【ASP.NET 进阶】Flv视频文件在线播放示例
最近要做个播放Flv文件的东东,网上找到一个合适的,效果如下(GIF录制软件不太好,差不多就是这样子,不过在浏览器上很流畅,具体代码可以看源代码): 源代码:FlvVideoSee.zip 问题说明: ...
- 用代码检查Windows程序的位数
方法就是通过读取程序文件的头部来判断,具体代码如下: #include <stdio.h> #include <windows.h> int CrnGetImageFileMa ...
- visio交叉线不凸起
使用visio作图时,经常会遇到交叉线在相交时会形成一个弯曲弓形,这有时十分影响视图效果.可以采用下面的方法消除弓形. 1.visio2003:只需要选中该交叉线,选择“格式”->“行为”,在打 ...
- ---github git clone 加速
https://www.zhihu.com/question/27159393/answer/35528173 git config --global http.postBuffer 52428800 ...
- C++中几种测试程序运行时间的方法<转>
转的地址:https://www.cnblogs.com/silentteen/p/7532855.html 1.GetTickCount()函数 原理: GetTickCount()是获取系统启动后 ...
- R-CNN 学习记录
CNN是一个运用卷积神经网络进行图片分类的开山之作.RCNN是第一个把图片分类和目标检测连接起来的作品. RCNN主要解决的问题是: 1.怎样用深度神经网络进行目标定位:2.怎样用小批量的标注数据来训 ...
- LeetCode 题解 56. Merge Intervals
题目大意:给出一组区间,合并他们. 首先是排序,首先看start,start小的在前面.start相同的话,end小的在前面. 排序以后,要合并了. 我自己的笨方法,说实在的问题真的很多.提交了好几次 ...
- Python基本数据类型以及字符串
基本数据类型 数字 int ,所有的功能,都放在int里 a1 = 123 a1 = 456 ...
- idea 执行maven 命令
如果当前账号不是超级管理员,这边需要执行系统用户变量, 输入安装文件bin路径 参考:https://blog.csdn.net/qq_19167629/article/details/7958490 ...
- cordova-config.xml配置应用图标
1. <icon src="res/icon/ios/browser.png"/> 2.规格: iphone平台一般要求3种规格的图片:1x.2x.3x,也是就Icon ...