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, ...
随机推荐
- Markdown语法参考
参考博客: https://www.jianshu.com/p/f3147a804368 https://www.jianshu.com/p/191d1e21f7ed https://www.jian ...
- sql 指定数据库中的信息操作
查是否有该表名 SELECT * FROM sys.objects WHERE name='表名'查表字段的信息select * from syscolumns where id=Object_Id( ...
- P4822 [BJWC2012]冻结
思路 和p4568类似的分层图最短路 从上一层向下一层连边权/2的边即可 代码 #include <cstdio> #include <algorithm> #include ...
- 论文笔记:Tracking by Natural Language Specification
Tracking by Natural Language Specification 2018-04-27 15:16:13 Paper: http://openaccess.thecvf.com/ ...
- Bytom设计结构解读
一.引文 设计Bytom 数据结构,组合了许多技术点,如 patricia tree,utxo, bvm, account model,protobuf,sql,memcache 等.本文会对一些技术 ...
- 剥开比原看代码08:比原的Dashboard是怎么做出来的?
作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...
- 剥开比原看代码03:比原是如何监听p2p端口的
作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...
- 在WPF中调用另存为对话框
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = "User ...
- 远程主动读取数据 RFC_READ_TABLE
IF IM_UDATE1 <> IM_UDATE2."get data from bw CLEAR IT_SEL_TAB. IT_SEL_TAB = '( /BI ...
- uint8_t / uint16_t / uint32_t /uint64_t数据类型详解
uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型? 在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型 ...