• 数据结构 - 平衡二叉树(AVL树)概念 | 插入与平衡调整


    目录

     

    前言 

    AVL树的概念

    AVL树结点的定义

    AVL树的插入

    AVL树的平衡调整(重点)

    LL型不平衡(需右单旋转)

    RR型不平衡(需左单旋转)

    LR型不平衡(需左右双旋转)

    RL型不平衡(需右左双旋转)

    AVL树简单实现以及验证-插入

     完整代码实现:


    前言 

    前面对 map/multimap/set/multiset 进行了简单的介绍,在其文档介绍中发现,这几个容器有个
    共同点是: 其底层都是按照二叉搜索树来实现的 ,但是二叉搜索树有其自身的缺陷,假如往树中插入的元素有序或者接近有序,二叉搜索树就会退化成单支树,时间复杂度会退化成O(N) ,因此map、 set 等关联式容器的底层结构是对二叉树进行了平衡处理,即采用平衡树来实现。

    AVL树的概念

    二叉搜索树虽可以缩短查找的效率,但如果数据有序或接近有序二叉搜索树将退化为单支树,查找元素相当于在顺序表中搜索元素,效率低下。因此,两位俄罗斯的数学家G.M.Adelson-Velskii 和E.M.Landis在1962年 发明了一种解决上述问题的方法:当向二叉搜索树中插入新结点后,如果能保证每个结点的左右子树高度之差的 绝对值 不超过1(需要对树中的结点进行调整),即可降低树的高度,从而减少平均 搜索长度。

    一棵AVL树或者是空树,或者是具有以下性质的二叉搜索树:

    • 它的左右子树都是AVL树
    • 左右子树高度之差(简称平衡因子)的绝对值不超过1(-1/0/1)

    如果一棵二叉搜索树是高度平衡的,它就是AVL树。如果它有n个结点,其高度可保持在

    O(log2_ n),搜索时间复杂度O(log2_ n)

    AVL树结点的定义

    我们这里直接实现KV模型的AVL树,为了方便后续的操作,这里将AVL树中的结点定义为三叉链结构,并在每个结点当中引入平衡因子(右子树高度-左子树高度)。除此之外,还需编写一个构造新结点的构造函数,由于新构造结点的左右子树均为空树,于是将新构造结点的平衡因子初始设置为0即可。

    1. template<class T>
    2. struct AVLTreeNode
    3. {
    4. AVLTreeNode(const T& data)
    5. : _pLeft(nullptr), _pRight(nullptr), _pParent(nullptr)
    6. , _data(data), _bf(0)
    7. {}
    8. AVLTreeNode* _pLeft; // 该节点的左孩子
    9. AVLTreeNode* _pRight; // 该节点的右孩子
    10. AVLTreeNode* _pParent; // 该节点的双亲
    11. T _data;
    12. int _bf; // 该节点的平衡因子
    13. };

    注意: 

    • 给每个结点增加平衡因子并不是必须的,只是实现AVL树的一种方式,不引入平衡因子也可以实现AVL树,只不过会麻烦一点 
    • 树中最大元素一定是无左子树,因为最大元素如果存在左子树,中序遍历就不可能是降序序列
    • 插入时,AVL树最多只需要旋转两次

    AVL树的插入

    AVL树插入结点时有以下三个步骤:

    1. 按照二叉搜索树的插入方法,找到待插入位置。
    2. 找到待插入位置后,将待插入结点插入到树中。
    3. 更新平衡因子,如果出现不平衡,则需要进行旋转。

    因为AVL树本身就是一棵二叉搜索树,因此寻找结点的插入位置是非常简单的,按照二叉搜索树的插入规则:

    1. 待插入结点的key值比当前结点小就插入到该结点的左子树。
    2. 待插入结点的key值比当前结点大就插入到该结点的右子树。
    3. 待插入结点的key值与当前结点的key值相等就插入失败。

    如此进行下去,直到找到与待插入结点的key值相同的结点判定为插入失败,或者最终走到空树位置进行结点插入。

    AVL树的平衡调整(重点)

    向AVL树中插入新数据

    • 和BST一样,首先在叶节点插入数据
    • 若插入导致不平衡,则需对以插入路径上离插入节点最近的平衡因子绝对值大于1的节点为根的树进行调整
       

    LL型不平衡(需右单旋转)

    • 在某个子树的左子树的左子树上插入新节点,导致根节点的平衡因子由1变为2

    右单旋的步骤如下:

    1. 让y的右子树作为x的左子树。
    2. 让x作为y的右子树。
    3. 让y作为整个子树的根。
    4. 更新平衡因子。

    右单旋后满足二叉搜索树的性质:

    1. y的右子树当中结点的值本身就比x的值小,因此可以作为x的左子树。
    2. x及其右子树当中结点的值本身就比y的值大,因此可以作为y的右子树。

     右单选代码实现

    1. void RotateR(Node* pParent)
    2. {
    3. Node* pSubL = pParent->_pLeft;
    4. Node* pSubLR = pSubL->_pRight;
    5. // 改变pParent和pSubL孩子的指向
    6. pParent->_pLeft = pSubLR;
    7. if (pSubLR)
    8. pSubLR->_pParent = pParent;
    9. pSubL->_pRight = pParent;
    10. // 更新pParent和pSubL的双亲
    11. Node* pPParent = pParent->_pParent;
    12. pParent->_pParent = pSubL;
    13. pSubL->_pParent = pPParent;
    14. // 更新原pParent双亲的左||右指针域指向
    15. if (nullptr == pPParent)
    16. {
    17. _pRoot = pSubL;
    18. }
    19. else
    20. {
    21. if (pParent == pPParent->_pLeft)
    22. pPParent->_pLeft = pSubL;
    23. else
    24. pPParent->_pRight = pSubL;
    25. }
    26. pParent->_bf = pSubL->_bf = 0;
    27. }

    RR型不平衡(需左单旋转)

    • 在某个子树的右子树的右子树上插入新节点,导致根节点的平衡因子由-1变为-2 


    左单旋的步骤如下:

    1. 让y的左子树作为x的右子树。
    2. 让x作为y的左子树。
    3. 让y作为整个子树的根。
    4. 更新平衡因子。

    左单旋后满足二叉搜索树的性质:

    1. y的左子树当中结点的值本身就比x的值大,因此可以作为x的右子树。
    2. x及其左子树当中结点的值本身就比y的值小,因此可以作为y的左子树。

     左单旋代码实现

    1. void RotateL(Node* pParent)
    2. {
    3. Node* pSubR = pParent->_pRight;
    4. Node* pSubRL = pSubR->_pLeft;
    5. pParent->_pRight = pSubRL;
    6. if (pSubRL)
    7. pSubRL->_pParent = pParent;
    8. pSubR->_pLeft = pParent;
    9. Node* pPParent = pParent->_pParent;
    10. pParent->_pParent = pSubR;
    11. pSubR->_pParent = pPParent;
    12. if (nullptr == pPParent)
    13. {
    14. _pRoot = pSubR;
    15. }
    16. else
    17. {
    18. if (pParent == pPParent->_pLeft)
    19. pPParent->_pLeft = pSubR;
    20. else
    21. pPParent->_pRight = pSubR;
    22. }
    23. pParent->_bf = pSubR->_bf = 0;
    24. }

    LR型不平衡(需左右双旋转)

    • 在某个子树的左子树的右子树上插入新节点,导致根节点的平衡因子由1变为2

    左右双旋的步骤如下:

    1. 以y为旋转点进行左单旋。
    2. 以x为旋转点进行右单旋。
    3. 更新平衡因子。

    左右双旋后满足二叉搜索树的性质:
    左右双旋后,实际上就是让z的左子树和右子树,分别作为y和x的右子树和左子树,再让y和x分别作为z的左右子树,最后让z作为整个子树的根(结合图理解)。

    1. z的左子树当中的结点本身就比y的值大,因此可以作为y的右子树。
    2. z的右子树当中的结点本身就比x的值小,因此可以作为x的左子树。
    3. 经过步骤1/2后,y及其子树当中结点的值都就比z的值小,而x及其子树当中结点的值都就比z的值大,因此它们可以分别作为z的左右子树。

     左右双旋转代码实现

    1. void RotateRL(Node* pParent)
    2. {
    3. Node* pSubR = pParent->_pRight;
    4. Node* pSubRL = pSubR->_pLeft;
    5. int bf = pSubRL->_bf;
    6. RotateR(pParent->_pRight);
    7. RotateL(pParent);
    8. // 更新部分节点的平衡因子
    9. if (bf == -1)
    10. pSubR->_bf = 1;
    11. else if (bf == 1)
    12. pParent->_bf = -1;
    13. }

    RL型不平衡(需右左双旋转)

    • 在某个子树的右子树的左子树上插入新节点,导致根节点的平衡因子由-1变为-2

    右左双旋的步骤如下:

    1. 以y为旋转点进行右单旋。
    2. 以x为旋转点进行左单旋。
    3. 更新平衡因子。

    右左双旋后满足二叉搜索树的性质:
    右左双旋后,实际上就是让z的左子树和右子树,分别作为x和y的右子树和左子树,再让x和y分别作为x的左右子树,最后让z作为整个子树的根(结合图理解)。

    1. z的左子树当中的结点本身就比x的值大,因此可以作为x的右子树。
    2. z的右子树当中的结点本身就比y的值小,因此可以作为y的左子树。
    3. 经过步骤1/2后,x及其子树当中结点的值都就比z的值小,而y及其子树当中结点的值都就比z的值大,因此它们可以分别作为z的左右子树。

    右左双旋转代码实现

    1. void RotateLR(Node* pParent)
    2. {
    3. Node* pSubL = pParent->_pLeft;
    4. Node* pSubLR = pSubL->_pRight;
    5. int bf = pSubLR->_bf;
    6. RotateL(pParent->_pLeft);
    7. RotateR(pParent);
    8. if (-1 == bf)
    9. pParent->_bf = 1;
    10. else if (1 == bf)
    11. pSubL->_bf = -1;
    12. }

    AVL树简单实现以及验证-插入

    函数接口

    1. template<class T>
    2. struct AVLTreeNode
    3. {
    4. AVLTreeNode(const T& data = T())
    5. : _pLeft(nullptr)
    6. , _pRight(nullptr)
    7. , _pParent(nullptr)
    8. , _data(data)
    9. , _bf(0)
    10. {}
    11. AVLTreeNode* _pLeft;
    12. AVLTreeNode* _pRight;
    13. AVLTreeNode* _pParent;
    14. T _data;
    15. int _bf; // 节点的平衡因子
    16. };
    17. // AVL: 二叉搜索树 + 平衡因子的限制
    18. template<class T>
    19. class AVLTree
    20. {
    21. typedef AVLTreeNode Node;
    22. public:
    23. AVLTree()
    24. : _pRoot(nullptr)
    25. {}
    26. // 在AVL树中插入值为data的节点
    27. bool Insert(const T& data);
    28. // AVL树的验证
    29. bool IsAVLTree()
    30. {
    31. return _IsAVLTree(_pRoot);
    32. }
    33. private:
    34. // 根据AVL树的概念验证pRoot是否为有效的AVL树
    35. bool _IsAVLTree(Node* pRoot);
    36. size_t _Height(Node* pRoot);
    37. // 右单旋
    38. void RotateR(Node* pParent);
    39. // 左单旋
    40. void RotateL(Node* pParent);
    41. // 右左双旋
    42. void RotateRL(Node* pParent);
    43. // 左右双旋
    44. void RotateLR(Node* pParent);
    45. private:
    46. Node* _pRoot;
    47. };

     完整代码实现:

    1. template<class T>
    2. struct AVLTreeNode
    3. {
    4. AVLTreeNode(const T& data = T())
    5. : _pLeft(nullptr)
    6. , _pRight(nullptr)
    7. , _pParent(nullptr)
    8. , _data(data)
    9. , _bf(0)
    10. {}
    11. AVLTreeNode* _pLeft;
    12. AVLTreeNode* _pRight;
    13. AVLTreeNode* _pParent;
    14. T _data;
    15. int _bf; // 节点的平衡因子
    16. };
    17. // AVL: 二叉搜索树 + 平衡因子的限制
    18. template<class T>
    19. class AVLTree
    20. {
    21. typedef AVLTreeNode Node;
    22. public:
    23. AVLTree()
    24. : _pRoot(nullptr)
    25. {}
    26. bool Insert(const T& data)
    27. {
    28. // 先按照二叉搜索树的方式进行插入
    29. if (nullptr == _pRoot)
    30. {
    31. _pRoot = new Node(data);
    32. return true;
    33. }
    34. // 按照二叉搜索树特性:找待插入节点在树中的位置
    35. // 并保存其双亲
    36. Node* pCur = _pRoot;
    37. Node* pParent = nullptr;
    38. while (pCur)
    39. {
    40. pParent = pCur;
    41. if (data < pCur->_data)
    42. pCur = pCur->_pLeft;
    43. else if (data > pCur->_data)
    44. pCur = pCur->_pRight;
    45. else
    46. return false;
    47. }
    48. // 插入新节点
    49. pCur = new Node(data);
    50. if (data < pParent->_data)
    51. pParent->_pLeft = pCur;
    52. else
    53. pParent->_pRight = pCur;
    54. pCur->_pParent = pParent;
    55. while (pParent)
    56. {
    57. // 更新pParent的平衡因子
    58. if (pCur == pParent->_pLeft)
    59. pParent->_bf--;
    60. else
    61. pParent->_bf++;
    62. if (0 == pParent->_bf)
    63. break;
    64. else if (-1 == pParent->_bf || 1 == pParent->_bf)
    65. {
    66. pCur = pParent;
    67. pParent = pCur->_pParent;
    68. }
    69. else
    70. {
    71. // pParent->_bf: 2 || -2
    72. // pParent的节点失去平衡
    73. if (2 == pParent->_bf)
    74. {
    75. // 右子树高-->最后必须左单旋
    76. if (1 == pCur->_bf)
    77. RotateL(pParent);
    78. else
    79. RotateRL(pParent);
    80. }
    81. else
    82. {
    83. if (-1 == pCur->_bf)
    84. RotateR(pParent);
    85. else
    86. RotateLR(pParent);
    87. }
    88. break;
    89. }
    90. }
    91. return true;
    92. }
    93. void InOrder()
    94. {
    95. _InOrder(_pRoot);
    96. }
    97. Node* LeftMost()
    98. {
    99. if (nullptr == _pRoot)
    100. return nullptr;
    101. Node* pCur = _pRoot;
    102. while (pCur->_pLeft)
    103. pCur = pCur->_pLeft;
    104. return pCur;
    105. }
    106. Node* RightMost()
    107. {
    108. if (nullptr == _pRoot)
    109. return nullptr;
    110. Node* pCur = _pRoot;
    111. while (pCur->_pRight)
    112. pCur = pCur->_pRight;
    113. return pCur;
    114. }
    115. bool IsAVLTree()
    116. {
    117. return _IsAVLTree(_pRoot);
    118. }
    119. private:
    120. // 验证二叉搜索树是否为有效的
    121. bool _IsAVLTree(Node* pRoot)
    122. {
    123. if (nullptr == pRoot)
    124. return true;
    125. int leftHeight = _Height(pRoot->_pLeft);
    126. int rightHeight = _Height(pRoot->_pRight);
    127. if (abs(rightHeight - leftHeight) > 1 ||
    128. rightHeight - leftHeight != pRoot->_bf)
    129. return false;
    130. return _IsAVLTree(pRoot->_pLeft) && _IsAVLTree(pRoot->_pRight);
    131. }
    132. size_t _Height(Node* pRoot)
    133. {
    134. if (nullptr == pRoot)
    135. return 0;
    136. size_t leftHeight = _Height(pRoot->_pLeft);
    137. size_t rightHeight = _Height(pRoot->_pRight);
    138. return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
    139. }
    140. void RotateR(Node* pParent)
    141. {
    142. Node* pSubL = pParent->_pLeft;
    143. Node* pSubLR = pSubL->_pRight;
    144. // 改变pParent和pSubL孩子的指向
    145. pParent->_pLeft = pSubLR;
    146. if (pSubLR)
    147. pSubLR->_pParent = pParent;
    148. pSubL->_pRight = pParent;
    149. // 更新pParent和pSubL的双亲
    150. Node* pPParent = pParent->_pParent;
    151. pParent->_pParent = pSubL;
    152. pSubL->_pParent = pPParent;
    153. // 更新原pParent双亲的左||右指针域指向
    154. if (nullptr == pPParent)
    155. {
    156. _pRoot = pSubL;
    157. }
    158. else
    159. {
    160. if (pParent == pPParent->_pLeft)
    161. pPParent->_pLeft = pSubL;
    162. else
    163. pPParent->_pRight = pSubL;
    164. }
    165. pParent->_bf = pSubL->_bf = 0;
    166. }
    167. void RotateL(Node* pParent)
    168. {
    169. Node* pSubR = pParent->_pRight;
    170. Node* pSubRL = pSubR->_pLeft;
    171. pParent->_pRight = pSubRL;
    172. if (pSubRL)
    173. pSubRL->_pParent = pParent;
    174. pSubR->_pLeft = pParent;
    175. Node* pPParent = pParent->_pParent;
    176. pParent->_pParent = pSubR;
    177. pSubR->_pParent = pPParent;
    178. if (nullptr == pPParent)
    179. {
    180. _pRoot = pSubR;
    181. }
    182. else
    183. {
    184. if (pParent == pPParent->_pLeft)
    185. pPParent->_pLeft = pSubR;
    186. else
    187. pPParent->_pRight = pSubR;
    188. }
    189. pParent->_bf = pSubR->_bf = 0;
    190. }
    191. void RotateRL(Node* pParent)
    192. {
    193. Node* pSubR = pParent->_pRight;
    194. Node* pSubRL = pSubR->_pLeft;
    195. int bf = pSubRL->_bf;
    196. RotateR(pParent->_pRight);
    197. RotateL(pParent);
    198. // 更新部分节点的平衡因子
    199. if (bf == -1)
    200. pSubR->_bf = 1;
    201. else if (bf == 1)
    202. pParent->_bf = -1;
    203. }
    204. void RotateLR(Node* pParent)
    205. {
    206. Node* pSubL = pParent->_pLeft;
    207. Node* pSubLR = pSubL->_pRight;
    208. int bf = pSubLR->_bf;
    209. RotateL(pParent->_pLeft);
    210. RotateR(pParent);
    211. if (-1 == bf)
    212. pParent->_bf = 1;
    213. else if (1 == bf)
    214. pSubL->_bf = -1;
    215. }
    216. void _InOrder(Node* pRoot)
    217. {
    218. if (pRoot)
    219. {
    220. _InOrder(pRoot->_pLeft);
    221. cout << pRoot->_data << " ";
    222. _InOrder(pRoot->_pRight);
    223. }
    224. }
    225. private:
    226. Node* _pRoot;
    227. };

  • 相关阅读:
    Hadoop如何启动HttpFS服务?
    LeetCode-662-二叉树最大宽度
    liunx 搭建 zookeeper
    商城免费搭建之java商城 开源java电子商务Spring Cloud+Spring Boot+mybatis+MQ+VR全景+b2b2c
    Go重写Redis中间件 - Go实现内存数据库
    常用脚本学习手册——Bat脚本
    eventlet 协程
    el-table相同的值合并单元格+多级表头
    wx:for-item wx:for-index wx:for-key
    【论文笔记】Perception, Planning, Control, and Coordination for Autonomous Vehicles
  • 原文地址:https://blog.csdn.net/qq_61386381/article/details/126409881