AVL平衡二叉树的各种问题(Balanced Binary Tree)
AVL树或者是一棵空树,或者是具有以下性质的非空二叉搜索树:
1. 任一结点的左、右子树均为AVL树;
2.根结点左、右子树高度差的绝对值不超过1.
1.声明
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
typedef int ElementType;
typedef struct AVLNode * AVLTree; //AVL树类型
struct AVLNode{
ElementType Data; //结点数据
AVLTree Left; //左子树
AVLTree Right; //右子树
int Height; //树高
};
2.获取高度
int GetHeight(AVLTree T){
if(T) return max(GetHeight(T->Left ),GetHeight(T->Right )) + ;
else return ;
}
3.左单旋LL
AVLTree SingleLeftRotation(AVLTree A){
// 注意:A 必须有一个左子结点 B
// 将 A 与 B 左单选,更新 A 与 B 的高度,返回新的根结点 B
AVLTree B = A->Left ;
A->Left = B->Right ;
B->Right = A;
A->Height = max(GetHeight(A->Left ), GetHeight(A->Right )) + ;
B->Height = max(GetHeight(B->Left ),A->Height ) + ;
return B;
}
4.右单旋RR
AVLTree SingleRightRotation(AVLTree A){
AVLTree B = A->Right ;
A->Right = B->Left ;
B->Left = A;
A->Height = max(GetHeight(A->Left ), GetHeight(A->Right )) + ;
B->Height = max(GetHeight(B->Right ),A->Height ) + ;
return B;
}
5.左-右双旋LR
AVLTree DoubleLeftRightRotation(AVLTree A){
// 注意:A必须有一个左子结点 B,且 B必须有一个右子结点 C
// 将 A、B 与 C 做两次单旋,返回新的根结点 C //将 B 与 C 做右单旋,C被返回
A->Left = SingleRightRotation(A->Left );
//将 A 与 C 做左单旋,C被返回
return SingleLeftRotation(A);
}
6.右-左双旋RL
AVLTree DoubleRightLeftRotation(AVLTree A){
A->Right = SingleLeftRotation(A->Right );
return SingleRightRotation(A);
}
7.AVL树的插入
AVLTree Insert(AVLTree T, ElementType X){
//将 X 插入AVL树 T 中,并且返回调整后的AVL树
if(! T){ //若插入空树,则新建包含一个结点的树
T = (AVLTree)malloc(sizeof(struct AVLNode));
T->Data = X;
T->Height = ;
T->Left = T->Right = NULL;
}
else if(X < T->Data ){
// 插入 T 的左子树
T->Left =Insert(T->Left , X);
// 如果需要左旋
if(GetHeight(T->Left ) - GetHeight(T->Right )== )
if(X <T->Left ->Data)
T = SingleLeftRotation(T); //左单旋
else
T = DoubleLeftRightRotation(T); //左-右双旋
}
else if(X > T->Data ){
// 插入 T 的右子树
T->Right = Insert(T->Right , X);
// 如果需要右旋
if(GetHeight(T->Left ) - GetHeight(T->Right )== -)
if(X > T->Right ->Data)
T = SingleRightRotation(T); //右单选
else
T = DoubleRightLeftRotation(T); //右-左双旋
}
// else X==T->Data 无需插入 //更新树高
T->Height = max(GetHeight(T->Left ),GetHeight(T->Right )) + ;
return T;
}
完整测试:
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
typedef int ElementType;
typedef struct AVLNode * AVLTree;
struct AVLNode{
ElementType Data;
AVLTree Left;
AVLTree Right;
int Height;
};
int GetHeight(AVLTree T){
if(T) return max(GetHeight(T->Left ),GetHeight(T->Right )) + ;
else return ;
}
AVLTree SingleLeftRotation(AVLTree A){
AVLTree B = A->Left ;
A->Left = B->Right ;
B->Right = A;
A->Height = max(GetHeight(A->Left ), GetHeight(A->Right )) + ;
B->Height = max(GetHeight(B->Left ),A->Height ) + ;
return B;
}
AVLTree SingleRightRotation(AVLTree A){
AVLTree B = A->Right ;
A->Right = B->Left ;
B->Left = A;
A->Height = max(GetHeight(A->Left ), GetHeight(A->Right )) + ;
B->Height = max(GetHeight(B->Right ),A->Height ) + ;
return B;
}
AVLTree DoubleLeftRightRotation(AVLTree A){
A->Left = SingleRightRotation(A->Left );
return SingleLeftRotation(A);
}
AVLTree DoubleRightLeftRotation(AVLTree A){
A->Right = SingleLeftRotation(A->Right );
return SingleRightRotation(A);
}
AVLTree Insert(AVLTree T, ElementType X){
if(! T){
T = (AVLTree)malloc(sizeof(struct AVLNode));
T->Data = X;
T->Height = ;
T->Left = T->Right = NULL;
}
else if(X < T->Data ){
T->Left =Insert(T->Left , X);
if(GetHeight(T->Left ) - GetHeight(T->Right )== )
if(X <T->Left ->Data)
T = SingleLeftRotation(T);
else
T = DoubleLeftRightRotation(T);
}
else if(X > T->Data ){
T->Right = Insert(T->Right , X);
if(GetHeight(T->Left ) - GetHeight(T->Right )== -)
if(X > T->Right ->Data)
T = SingleRightRotation(T);
else
T = DoubleRightLeftRotation(T);
}
T->Height = max(GetHeight(T->Left ),GetHeight(T->Right )) + ;
return T;
}
void LevelorderTravelsal(AVLTree BT){
queue<AVLTree> q;
AVLTree T;
if(!BT) return;
q.push(BT);
while(!q.empty()){
T=q.front();
q.pop();
cout<<T->Data <<" ";
if(T->Left ) q.push(T->Left );
if(T->Right ) q.push(T->Right );
}
}
int main(){
int n; cin>>n;
AVLTree T = NULL;
for(int i=;i<n;i++){
int x; cin>>x;
T = Insert(T, x);
}
LevelorderTravelsal(T);
}
AVL平衡二叉树的各种问题(Balanced Binary Tree)的更多相关文章
- 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树
平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...
- 平衡二叉树(Balanced Binary Tree)
平衡二叉树(Balanced Binary Tree)/AVL树:
- [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树
4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
随机推荐
- SP10707 COT2 - Count on a tree II 莫队
链接 https://vjudge.net/problem/SPOJ-COT2 https://www.luogu.org/problemnew/show/SP10707 思路 dfs欧拉序转化为普通 ...
- Nacos整合Spring Cloud Gateway实践
Spring Cloud Gateway官网:http://spring.io/projects/spring-cloud-gateway Eureka1.0的问题和Nacos对比:https://w ...
- Win10下Java开发环境配置
首先下载符合操作系统版本的jdk,比如64位的JDK8: 下载链接:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-down ...
- Katana的WebAPI集成Swagger 解决方案
这位大哥写的博客很清楚了,我就不重复了. http://www.cnblogs.com/caodaiming/p/4156476.html 错误解决 http://blog.csdn.net/gold ...
- Qt打包
先在Qt Creator里release一遍,打开Qt下载时自带的像命令控制台一样的东西,比如我在创建项目时选用的是MinGw编译器, 然后在文件管理器里找到release生成的exe,cd进exe所 ...
- SAP 汇率处理总结
SAP 汇率处理总结 OB08 http://blog.sina.com.cn/s/blog_a440b7ee0101mvpd.html 分类: FI.GL 1.能想到的几种Currency:lo ...
- 【Python】【Web开发】
# [[Web开发]] ''' 最早的软件都是运行在大型机上的,软件使用者通过“哑终端”登陆到大型机上去运行软件.后来随着PC机的兴起,软件开始主要运行在桌面上,而数据库这样的软件运行在服务器端,这种 ...
- 蚂蚁金服 Service Mesh 渐进式迁移方案|Service Mesh Meetup 实录
小蚂蚁说: 本文是基于在 Service Mesher Meetup 上海站的主题分享<蚂蚁金服 Service Mesh 渐进式迁移方案>内容整理,完整的分享 PPT 获取方式见文章底部 ...
- Servlet中web.xml的配置
引言:这是一个采用原生Servlet开发的项目的一个简要配置,在这里记录一下,以便以后用到了 可以直接copy,如又侵权,请联系本博主. <?xml version="1.0" ...
- Django模板操作
进行加减运算 def index(request): a = request.GET['a'] b = request.GET['b'] c = int(a) + int(b) return Http ...