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 ...
随机推荐
- leetcode1008
class Solution: def __init__(self): self.root = None def construct(self,order,root,i): if i==len(ord ...
- nginx 代理跨域
跨域 nginx设置http{ add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X- ...
- 25_ajax请求_使用fetch
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- AJax知识介绍
参考:http://www.runoob.com/ajax/ajax-asp-php.html
- URL记录
http://orchome.com/5https://www.cnblogs.com/haozhengfei/p/2192231596ceb2ac4c22294dbd25a1ca.htmlhttps ...
- C#开发VS LUA开发
一个游戏公司,决定开始用U3D做一款新游戏,这个游戏类型从来没做过. 如果没有一个成熟的游戏框架,那么从头撸起. 是一开始就将LUA热更新考虑进来呢 还是先做成纯C#的框架呢? 考虑因素:游戏逻辑如果 ...
- Shell函数使用方法
Shell函数是一组命令集或语句组成一个可用块.利用函数可以简化脚本编写.函数要求先定义再使用,调用函数时直接使用函数名即可.这里主要介绍shell编程中函数定义.调用.获取函数参数以及获取函数返回值 ...
- MyBatis动态SQL中trim标签的使用
My Batis 官方文档 对 动态SQL中使用trim标签的场景及效果介绍比较少. 事实上trim标签有点类似于replace效果. trim 属性 prefix:前缀覆盖并增加其内容 suffix ...
- centos6.5下oracle自动备份删除指定天数的文件
第一步先做一个备份 #!/bin/sh export ORACLE_BASE=/home/oracle/app export ORACLE_HOME=/dbhome_1 export ORACLE_S ...
- (Unity4.7)assetbundle 坑爹总结
使用版本Unity4.7 一.关于依赖打包 1.当一个被打包的资源A引用了其他的资源B,并且没有被打成一个包时,要选用[BuildAssetBundleOptions.CollectDependenc ...