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)的更多相关文章

  1. 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树

    平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...

  2. 平衡二叉树(Balanced Binary Tree)

    平衡二叉树(Balanced Binary Tree)/AVL树:

  3. [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 ...

  4. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  5. LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

  6. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  7. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

  8. 110. Balanced Binary Tree - LeetCode

    Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  10. 110.Balanced Binary Tree Leetcode解题笔记

    110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

随机推荐

  1. loj#2305. 「NOI2017」游戏 2-sat

    链接 https://loj.ac/problem/2305 https://www.luogu.org/problemnew/show/P3825 思路 3-sat神马的就不要想了,NP问题 除去x ...

  2. 深入浅出JavaScript(一)

    数据类型 1. 六种数据类型 JavaScript包含六种数据类型:基本类型number.string.boolean.null.undefined和对象类型Object.其中,对象类型包含方法fun ...

  3. 比赛总结——牛客网 NOIP赛前集训营提高组模拟第一场

    第一场打的很惨淡啊 t1二分+前缀最小值没想出来,20分的暴力也挂了,只有10分 t2数位dp,调了半天,结果因为忘了判0的特殊情况WA了一个点,亏死 t3emmmm.. 不会 imone说是DSU ...

  4. Unity3D学习笔记(二十九):AssetBundle

    AssetBundle 什么是AssetBundle? AssetBundle是把一些资源文件或场景文件,以某种方式保存在一个文件中.一个AssetBundle可以包含模型.材质.图片或场景等.但是A ...

  5. insert into table (a,b,c) select

    本文为博主原创,转载请注明出处: 在项目中,需要统计数据,从基础表中的数据进行统计,并插入到汇总 表中, (1)语句形式为:Insert into Table2(field1,field2,...) ...

  6. 将一个符合URL格式的字符串变成链接

    function replaceURLWithHTMLLinks(text) { /* Example: >>> GateOne.Utils.replaceURLWithHTMLLi ...

  7. 【译】第5节---Code First约定

    原文:http://www.entityframeworktutorial.net/code-first/code-first-conventions.aspx 我们在上一节中已经看到了EF Code ...

  8. 【RFT】【环境配置】Mac

    参考地址:http://blog.csdn.net/u010044052/article/details/70858015?utm_source=itdadao&utm_medium=refe ...

  9. _battleground

    战场控制表 bgName 战场名字 bgTypeId 战场类型索引,请勿修改 cf 0 - 关闭混排:1 - 开启混排 limitHP 进入战场的最低血量,血量低于该值无法排此战场 maxRes 阿拉 ...

  10. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...