gitbook-way-to-algorithm
  • Introduction
  • Preface 前言
  • Content 目录
  • MathSymbolTable 数学符号表
  • Chapter-1 BasicKnowledge 第1章 基础知识
    • TimeComplexity 时间复杂度
    • Recursion 递归式
  • Chapter-2 Sort 第2章 排序
    • InsertSort 插入排序
    • BubbleSort 冒泡排序
    • QuickSort 快速排序
    • MergeSort 归并排序
  • Chapter-3 Search 第3章 搜索
    • BinarySearch 二分查找法(折半查找法)
    • AdditionMultiplicationPrinciple 加法乘法原理
    • BruteForce 暴力枚举
    • Recursion 递归
    • BreadthFirstSearch 广度优先搜索
    • BidirectionalBreadthSearch 双向广度搜索
    • AStarSearch A*搜索
    • DancingLink 舞蹈链
  • Chapter-4 DataStructure 第4章 数据结构
    • DisjointSet 并查集
    • PrefixTree(TrieTree) 前缀树
    • LeftistTree(LeftistHeap) 左偏树(左偏堆)
    • SegmentTree 线段树
    • FenwickTree(BinaryIndexedTree) 树状数组
    • BinarySearchTree 二叉查找树
    • AVLTree AVL平衡树
    • RedBlackTree 红黑树
    • SkipList 跳跃表
    • BPlusTree B+树
    • BMinusTree B-树
  • Chapter-5 DynamicProgramming 第5章 动态规划
    • Section-1 LinearDP 第1节 线性动规
      • LongestCommonSubsequence 最长公共子序列
      • LongestIncreasingSubsequence 最长递增子序列
      • BidirectionalSubsequence 双向子序列
      • MaximumContinuousSubsequenceSum 最大连续子序列和
      • LongestPalindromicSubsequence 最长回文子序列
    • Section-2 BagDP 第2节 背包问题
      • 01-Bag 01背包
      • CompleteBag 完全背包
      • TwoDimensionBag 二维背包
    • Section-3 RegionalDP 第3节 区域动规
      • MinimumMergeCost - 最小合并代价
      • UniquePath 唯一路径
      • TrianglePath 三角形路径
    • Section-4 TreeDP 第4节 树形动规
      • MaximumBinaryTree 最大二叉树
      • MaxMultipleTree 最大多叉树
      • MaximumBinaryTreeRadiusSum 最大二叉树和
  • Chapter-6 GraphTheory 第6章 图论
    • Section-1 Traverse 第1节 遍历
      • DepthFirstSearch 深度优先搜索
      • BreadthFirstSearch 广度优先搜索
      • TopologicalSort 拓扑排序
      • EulerCycle 欧拉回路
    • Section-2 MinSpanningTree 第2节 最小生成树
      • Kruskal Kruskal算法
      • Prim Prim算法
    • Section-3 ShortestPath 第3节 最短路径
      • BellmanFord BellmanFord算法
      • Dijkstra Dijkstra算法
      • FloydWarshall FloydWarshall算法
      • DifferentConstraints 差分约束
    • Section-4 StronglyConnectedComponents 第4节 强连通分支
      • Kosaraju Kosaraju算法
      • Tarjan Tarjan算法
      • 2-SAT 2-SAT问题
    • Section-5 NetworkFlow 第5节 网络流
      • EdmondsKarp EdmondsKarp算法(最短路径增广算法)
      • PushRelabel 压入与重标记算法
      • Dinic - Dinic算法
      • MinimumCostFlow - 最小费用流
      • MultipleSourceMultipleSinkMaxflow - 多源点多汇点最大流
    • Section-6 BinaryMatch 第6节 二分匹配
      • Hungarian 匈牙利算法
      • HopcroftKarp Hopcroft-Karp算法
      • MatchToMaxflow 二分匹配转化为最大流
      • KuhnMunkres Kuhn-Munkres算法
      • Introduction-Domination,Independent,Covering,Clique 介绍支配集、独立集、覆盖集和团
      • WeightedCoveringAndIndependentSet 最小点权覆盖和最大点权独立集
      • MinimumDisjointPathCovering 最小不相交路径覆盖
      • MinimumJointPathCovering 最小可相交路径覆盖
      • Coloring 染色问题
  • Chapter-7 CombinatorialMathematics 第7章 组合数学
    • FullPermutation 全排列
    • Combination 组合
    • Permutation 排列
    • PermutationGroup 置换群
  • Chapter-8 NumberTheory 第8章 数论
    • PrimeSieve 素数筛法
    • GreatestCommonDivisor 最大公约数
    • Euclid 欧几里得算法
    • ExtendedEuclid 扩展欧几里得算法
    • ChineseRemainerTheorem 中国剩余定理
    • ModularExponentiation 模幂运算
  • Chapter-9 LinearAlgebra 第9章 线性代数
    • Section-1 Matrix 第1节 矩阵
      • Strassen Strassen算法
      • GaussElimination 高斯消元法
      • LUP LUP分解
      • InverseMatrix 矩阵求逆
    • Section-2 LinearProgramming 第2节 线性规划
      • Simplex 单纯形算法
      • Dinkelback Dinkelback算法
  • Chapter-10 AnalyticGeometry 第10章 解析几何
    • Section-1 Polygon 第1节 多边形
      • Cross 向量叉积
      • SegmentIntersection 线段相交
      • Sweeping 扫除算法
      • ConvexPolygonArea 凸多边形面积
      • ConvexPolygonGravityCenter 凸多边形重心
      • NearestNeighbor 最近点对
    • Section-2 ConvexHull 第2节 凸包
      • GrahamScan Graham扫描算法
      • QuickHull 快速凸包算法
      • RotatingCalipers 旋转卡壳
  • Chapter-11 PatternMatch 第11章 文本匹配
    • SimpleMatch 简单匹配
    • AhoCorasickAutomata AC自动机
    • KnuthMorrisPratt KMP匹配算法
    • RabinKarp RabinKarp算法
    • BoyerMoore BoyerMoore算法
  • Chapter-12 GameTheory 第12章 博弈论
    • BashGame 巴什博弈
    • WythoffGame 威佐夫博弈
    • NimGame 尼姆博弈
Powered by GitBook
On this page
  • 特性
  • 旋转操作
  • 插入
  • 删除
  • AVL Tree
  • 源码
  • 测试
  1. Chapter-4 DataStructure 第4章 数据结构

AVLTree AVL平衡树

特性

AVL树是最早发明的一种自平衡二叉查找树,树中的任何节点的左右两个子树的高度最大差别为111,因此也称为高度平衡树。包含nnn个节点的AVL树其查找、插入、删除操作的平均时间复杂度都是O(log2⁡n)O(log_{2}⁡n)O(log2​⁡n)。AVL树高度为O(log2⁡n)O(log_{2}⁡n)O(log2​⁡n)。

引入二叉树上节点之间距离和高度的定义。一个叶子节点向上到达根节点所经过的跳数为这两个节点的距离,一个节点和自己的距离为000,将空节点的高度视作−1-1−1。根节点到达其所有叶子节点的最大距离即为根节点的高度,同时也是该子树的高度。显然对于任意节点都有

heightx=max(heightleft,heightright)+1height_{x} = max(height_{left}, height_{right}) + 1heightx​=max(heightleft​,heightright​)+1

一个节点的左右孩子的高度差为该节点的平衡因子

factor=heightleft−heightrightfactor = height_{left} - height_{right}factor=heightleft​−heightright​

当一个节点的∣factor∣≤1\lvert factor \rvert \leq 1∣factor∣≤1时称该节点所在的子树是平衡的,否则是不平衡的。

除了基本的二叉查找树属性,AVL树拥有以下属性:

(1)(1)(1) AVL树上所有节点都是平衡的,即平衡性;

(2)(2)(2) AVL树的高度为根节点的高度;

(3)(3)(3) AVL树的高度为height=O(log2n)height = O(log_2 n)height=O(log2​n);

旋转操作

AVL树的查询操作和二叉查找树一样,插入/删除操作也基本相同,首先通过二分查找找到合适插入的位置/要被删除的节点,然后做插入/删除操作。插入/删除会破坏AVL树的平衡性,LL(单向右旋平衡处理/左左)、RR(单向左旋平衡处理/右右)、LR(先左后右双向旋转平衡处理/左右)、RL(先右后左双向旋转平衡处理/右左)四种情况是所有需要调整的情况:

上面四种情况包含了所有从不平衡转化为平衡。通过节点的高度值、该节点是其父结点的左或者右,可以判断节点属于哪种情况,做相应的操作。

插入

对于下面这个AVL树,每个节点中上面的数字是节点下标,下面的数字是该节点的高度值heightheightheight。将181818从该AVL树的根节点开始,按照二分查找算法依次经过节点10→15→19→16→1710 \rightarrow 15 \rightarrow 19 \rightarrow 16 \rightarrow 1710→15→19→16→17,最后插入171717的右孩子节点;

新节点插入完成后,我们沿着父结点指针一路向上,检查每个节点是否平衡,若不平衡则进行旋转操作,再更新节点高度,直到根节点。

(1)(1)(1) 节点181818为叶子节点,因此高度值为height18=0height_{18} = 0height18​=0;

(2)(2)(2) 平衡因子为factor17=∣heightnil−height18∣=∣−1−0∣=1factor_{17} = \lvert height_{nil} - height_{18} \rvert = \lvert - 1 - 0 \rvert = 1factor17​=∣heightnil​−height18​∣=∣−1−0∣=1,不需要旋转,更新节点171717的高度值height17=max⁡(heightnil,heightnil)+1=max⁡(−1,0)+1=1height_{17} = max⁡(height_{nil},height_{nil}) + 1 = max⁡(-1,0) + 1 = 1height17​=max⁡(heightnil​,heightnil​)+1=max⁡(−1,0)+1=1;

(3)(3)(3) 平衡因子为factor16=∣heightnil−height17∣=∣−1−1∣=2>1factor_{16} = \lvert height_{nil} - height_{17} \rvert = \lvert - 1 - 1 \rvert = 2 \gt 1factor16​=∣heightnil​−height17​∣=∣−1−1∣=2>1,需要进行RR操作,旋转后节点161616的高度值为height16=0height_{16} = 0height16​=0,更新节点161616的高度值height16=max⁡(heightnil,height17)+1=max⁡(−1,1)+1=2height_{16} = max⁡(height_{nil},height_{17}) + 1 = max⁡(-1,1) + 1 = 2height16​=max⁡(heightnil​,height17​)+1=max⁡(−1,1)+1=2;

(4)(4)(4) 平衡因子为factor19=∣height17−height20∣=∣1−0∣=1factor_{19} = \lvert height_{17} - height_{20} \rvert = \lvert 1 - 0 \rvert = 1factor19​=∣height17​−height20​∣=∣1−0∣=1,更新节点191919的高度值height19=max⁡(height16,height20)+1=max⁡(1,0)+1=2height_{19} = max⁡(height_{16},height_{20}) + 1 = max⁡(1,0) + 1 = 2height19​=max⁡(height16​,height20​)+1=max⁡(1,0)+1=2;

(5)(5)(5) 平衡因子为factor15=∣height13−height19∣=∣1−2∣=1factor_{15} = \lvert height_{13} - height_{19} \rvert = \lvert 1 - 2 \rvert = 1factor15​=∣height13​−height19​∣=∣1−2∣=1,更新节点151515的高度值height15=max⁡(height13,height19)+1=max⁡(1,2)+1=3height_{15} = max⁡(height_{13},height_{19}) + 1 = max⁡(1,2) + 1 = 3height15​=max⁡(height13​,height19​)+1=max⁡(1,2)+1=3;

(6)(6)(6) 平衡因子为factor10=∣height5−height15∣=∣2−3∣=1factor_{10} = \lvert height_{5} - height_{15} \rvert = \lvert 2 - 3 \rvert = 1factor10​=∣height5​−height15​∣=∣2−3∣=1,更新节点101010的高度值height10=max⁡(height5,height15)+1=max⁡(2,3)+1=4height_{10} = max⁡(height_{5},height_{15}) + 1 = max⁡(2,3) + 1 = 4height10​=max⁡(height5​,height15​)+1=max⁡(2,3)+1=4;

删除

AVL树的删除操作和BinarySearchTree一样:

(1)(1)(1) 若xxx为叶子节点,既没有左孩子节点也没有右孩子节点,直接删除;

(2)(2)(2) 若xxx只有一个孩子节点yyy,则像链表一样将xxx从其父节点和yyy之间删除;

(3)(3)(3) 若xxx同时有左右孩子节点,按照中序遍历找出二叉树中比xxx大的下一个节点nextnextnext(中序遍历下的后继节点),用其值代替xxx,再继续用相同的规则递归的删除原始节点nextnextnext(实际上,中序遍历的后继节点不可能存在左孩子节点,要么无孩子节点,要么只有右孩子节点);

删除完成后从xxx的父节点开始依次向上,检查每个节点是否平衡,若不平衡则进行旋转操作,再更新节点高度,直到根节点。检查本身的时间复杂度为O(log2n)O(log_2 n)O(log2​n)。下图中删除节点151515,节点151515的中序遍历后继节点161616代替它,然后删除真正的161616。从新的161616开始检查每个节点是否平衡,直到根节点。本次删除结束。

AVL树的插入/删除操作的实际操作需要约O(2×log2n)O(2 \times log_2 n)O(2×log2​n)次,其中O(log2n)O(log_2 n)O(log2​n)花费在从根节点向下寻找合适的插入位置/要被删除的节点,O(log2n)O(log_2 n)O(log2​n)花费在插入/删除完成后向上对每个节点的检查以及旋转操作。

AVL Tree

源码

测试

PreviousBinarySearchTree 二叉查找树NextRedBlackTree 红黑树

Last updated 6 years ago

https://www.geeksforgeeks.org/avl-tree-set-1-insertion/
https://www.geeksforgeeks.org/avl-tree-set-2-deletion/
AvlTree.h
AvlTree.cpp
AvlTreeTest.cpp
AVLTree1.png
AVLTree2.png
AVLTree3.png
AVLTree4.png
AVLTree5.png
AVLTree6.png
AVLTree7.png
AVLTree8.png
AVLTree9.png
AVLTree10.png
AVLTree11.png
AVLTree12.png
AVLTree13.png