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 ...
随机推荐
- kubernetes国内镜像拉取
因国内访问不到goole服务器,只能拉取国内的镜像,这里以阿里云为例. 安装minikube时报failed to pull image "k8s.gcr.io/kube-apiserver ...
- window.location.search的用法 和 地址栏的的javsscript编码与解码
ocation.search是从当前URL的?号开始的字符串 如:http://www.51js.com/viewthread.php?tid=22720 它的search就是?tid=22720 e ...
- oracle 内连接(inner join)、外连接(outer join)、全连接(full join)
转自:https://premier9527.iteye.com/blog/1659689 建表语句: create table EMPLOYEE(EID NUMBER,DEPTID NUMBER,E ...
- RabbitMq(2) 简单消息队列
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client </ar ...
- VC 字符串转化和分割
原文:点击这里. 备忘:为了适用于Unicode环境,要养成使用_T()宏的习惯 1.格式化字符串 CString s;s.Format(_T("The num is %d."), ...
- Django--ORM(模型层)-重点
一.ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库, 通过简单的配置就可以轻松更换数据库,这极大的减轻了开发 ...
- Spark 调优(转)
Spark 调优 返回原文英文原文:Tuning Spark Because of the in-memory nature of most Spark computations, Spark pro ...
- Java动态代理的两种实现方法
注:文章转载自:https://blog.csdn.net/m0_38039437/article/details/77970633 一.代理的概念 动态代理技术是整个java技术中最重要的一个技术, ...
- Redis进阶实践之三如何在Windows系统上安装安装Redis(转载)
Redis进阶实践之三如何在Windows系统上安装安装Redis 一.Redis的简介 Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括 ...
- 记Dagger2使用过程中的一个BUG--compileGoogleDebugJavaWithJavac
项目编译可以通过,不过没有生成Dagger2的类,导致无法运行项目.. 错误提示 Error:(14, 41) 错误: 找不到符号 符号: 类 DaggerAppComponent 位置: 程序包 c ...