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. jlink的SWD与JTAG下载模式的对应接线方法

     参考博客:http://blog.csdn.net/qq_26093511/article/details/59484249 (1)如果用jtag模式下载的话需要接线:          jlink ...

  2. OAuth的MVC实现(微软)

    LoginController中: 第三方登陆 public ActionResult LogOn() { string liveUrl = string.Format( "https:// ...

  3. 测试常用的sql语句总结

    测试中常用的sql语句,排名部分先后 1. 查询 SELECT * FROM 表名称 SELECT COUNT(DISTINCT column_name) FROM table_name 指定列的不同 ...

  4. CodeChef - FNCS Chef and Churu(分块)

    https://vjudge.net/problem/CodeChef-FNCS 题意: 思路: 用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所 ...

  5. border:none和border:0的区别

    C:当定义border:none时,表示无边框样式,浏览器并不会对边框进行渲染,也就没有实际的宽度:  D:定义边框时,除了设置宽度外,还必须设置边框的样式才能显示出来.     border:0;浏 ...

  6. File类文件的常见操作

    boolean exists() 判断文件或者目录是否存在 boolean isFile()  判断是否是文件 boolean isDirectory() 判断是否是目录 String getPath ...

  7. mutect/mutsig/gistic官网汇总

    http://software.broadinstitute.org/software/cprg/

  8. 【Django】【三】模型

    [基本数据访问] 1. models.py写模型 2. 数据库迁移 guest> python manage.py makemigrations sign guest> python ma ...

  9. Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树+树链剖分+线段树

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  10. Python使用win32com实现的模拟浏览器功能

    # -*- coding:UTF- -*- #!/user/bin/env python ''' Created on -- @author: chenzehe ''' import win32com ...