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, ...
随机推荐
- newcoder 筱玛的迷阵探险(搜索 + 01字典树)题解
题目描述 筱玛是个快乐的男孩子. 寒假终于到了,筱玛决定请他的朋友们一起来玩迷阵探险. 迷阵可以看做一个n×nn×n的矩阵A,每个格子上有一个有一个数Ai,j. 入口在左上角的(1,1)处,出口在右下 ...
- R read.tabe line 5 did not have 2 elements
R read.tabe line 5 did not have 2 elements Reason: there are special characters such as # in file o ...
- (转)How Hash Algorithms Work
本文转自:http://www.metamorphosite.com/one-way-hash-encryption-sha1-data-software Home Posted: Novembe ...
- Vue.extend提供自定义组件的构造器
Vue.extend 返回的是一个“扩展实例构造器”,也就是预设了部分选项的Vue实例构造器.经常服务于Vue.component用来生成组件,可以简单理解为当在模板中遇到该组件名称作为标签的自定义元 ...
- Java中常用的方法
枚举: 1:DemoEnum.valueOf(String str) 从枚举类中中找字符串,如果有则返回对应枚举值 2:DemoEnum.values() 获得枚举集合 3:DemoEnum.prop ...
- RabbitMQ.client消息队列
doc 介绍 分类&&典型应用 中文文档 使用
- Vim-编辑器之神
几点声明: 作者只是一位小小的 \(OIer\) ,并不会什么过于神仙的东西,我这篇文章只是帮助人入门的而已. 若有人在 OI-Wiki上见过了原文章,原作者是我 \(......\) ,真不是抄 \ ...
- There is no Action mapped for namespace / and action name .解答
做struts2登陆检验的时候遇到了一个问题: 输入http://localhost/login_validation/的目的是想显示 文件夹下的文件列表,无奈,使用struts框架,web.xml设 ...
- Oracle Initialzation error instantclient_11_2 32位客户端问题
本地安装完oracle 用PLSql 连接是一堆的问题,折腾一上午终于解决了 下载一个 instantclient_11_2 32位的oracle客户端.放在oracle安装目录product下,在把 ...
- memset()函数用法及其作用
memset()函数原型是: extern void *memset(void *buffer, int c, int count) //buffer:为指针或是数组, //c:是赋给buffer的值 ...