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 ...
随机推荐
- [记录] Ubuntu 配置Apache虚拟站点
版本 Ubuntu 16.04 1 . 首先找到Apapche配置文件夹 /etc/apache2/ apache2.conf conf-enabled magic mods-enabled sit ...
- Dubbo 暴露服务
1. 引入dubbo依赖 dubbo 依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId&g ...
- MongoDB 全部笔记
1. MongoDB: 是NOSQL的一种, 特长是分布式用的,用于处理爬虫数据 2. mongoDB 与 redis mongoDB是最像关系型的非关系型数据,更加适用于大数据,redis则更倾向于 ...
- html to docx
public static void main(String[] args) throws Exception{ //创建 POIFSFileSystem 对象 POIFSFileSystem poi ...
- wed开发基础--练习题
一.HTML部分 本小节重点: 熟练使用div+span布局,知道div和span的语义化的意思 熟悉对div.ul.li.span.a.img.table.form.input标签有深刻的认知, 初 ...
- postman 的基础使用
https://blog.csdn.net/fxbin123/article/details/80428216
- U3D中可以直接使用GL!!!
https://blog.csdn.net/u013172864/article/details/78860624
- 钩子函数mounted:
1.钩子函数 钩子函数是Windows消息处理机制的一部分,通过设置“钩子”,应用程序可以在系统级对所有消息.事件进行过滤,访问在正常情况下无法访问的消息.钩子的本质是一段用以处理系统消息的程序,通过 ...
- http协议以及http1.0和http1.1的区别
header响应头信息: HTTP/1.1 302 FOUND Content-Length: 0 Set-Cookie: sessionid=n3gozvqbjba1zckr7v0ccj6yn7v9 ...
- centos 卸载和安装软件
rpm -qa 列出所有已安装软件包 rpm -e packagename 删除软件包 rpm -e --nodeps packagename 强制删除软件和依赖包 rpm -q 包名 查 ...