当前位置:   article > 正文

c++——map、set底层之AVL树(动图演示旋转)

c++——map、set底层之AVL树(动图演示旋转)

在上一篇博客里面我们提到了set和map的使用;这篇博客重点介绍他们的底层逻辑

c++之set和map——关联容器(非顺序容器——list、vector)


前言

我们之前学了过二叉搜索树,二这里的AVL也是一种搜索树,当我们二叉搜索树发生偏移时,搜索时间就会边长,就不树logN了,变成N;而AVL就修复了这一点。


一、底层结构

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

二、AVL 树

1.AVL树的概念

二叉搜索树虽可以缩短查找的效率,但如果数据有序或接近有序二叉搜索树将退化为单支树,查 找元素相当于在顺序表中搜索元素,效率低下。因此,两位俄罗斯的数学家G.M.Adelson-Velskii 和E.M.Landis在1962年

发明了一种解决上述问题的方法:当向二叉搜索树中插入新结点后,如果能保证每个结点的左右 子树高度之差的绝对值不超过1(需要对树中的结点进行调整),即可降低树的高度,从而减少平均 搜索长度。

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

它的左右子树都是AVL树

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

如果一棵二叉搜索树是高度平衡的,它就是AVL树。如果它有n个结点,其高度可保持在 $O(log_2 n)$,搜索时间复杂度O($log_2 n$)

2.AVL树节点的定义

代码如下(示例):

  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<T>* _pLeft; // 该节点的左孩子
  9. AVLTreeNode<T>* _pRight; // 该节点的右孩子
  10. AVLTreeNode<T>* _pParent; // 该节点的双亲
  11. T _data;
  12. int _bf; // 该节点的平衡因子
  13. };

3、AVL树的插入

AVL的插入和搜索二叉树一样,只不过我们需要处理旋转问题,控制这棵树不要一边高一边低

  1. template<class K, class V>
  2. class AVLTree
  3. {
  4. typedef AVLTreeNode<K, V> Node;
  5. public:
  6. // 1. 先按照二叉搜索树的规则将节点插入到AVL树中
  7. // ...
  8. // 2. 新节点插入后,AVL树的平衡性可能会遭到破坏,此时就需要更新平衡因子,并检测是否
  9. 破坏了AVL树
  10. // 的平衡性
  11. pCur插入后,pParent的平衡因子一定需要调整,在插入之前,pParent
  12. 的平衡因子分为三种情况:-10, 1, 分以下两种情况:
  13. 1. 如果pCur插入到pParent的左侧,只需给pParent的平衡因子-1即可
  14. 2. 如果pCur插入到pParent的右侧,只需给pParent的平衡因子+1即可
  15. 此时:pParent的平衡因子可能有三种情况:0,正负1, 正负2
  16. 1. 如果pParent的平衡因子为0,说明插入之前pParent的平衡因子为正负1,插入后被调整
  17. 0,此时满足
  18. AVL树的性质,插入成功
  19. 2. 如果pParent的平衡因子为正负1,说明插入前pParent的平衡因子一定为0,插入后被更
  20. 新成正负1,此
  21. 时以pParent为根的树的高度增加,需要继续向上更新
  22. 3. 如果pParent的平衡因子为正负2,则pParent的平衡因子违反平衡树的性质,需要对其进
  23. 行旋转处理
  24. bool Insert(const pair<K, V>& kv)
  25. {
  26. if (_root == nullptr)
  27. {
  28. _root = new Node(kv);
  29. return true;
  30. }
  31. Node* parent = nullptr;
  32. Node* cur = _root;
  33. while (cur)
  34. {
  35. if (cur->_kv.first < kv.first)
  36. {
  37. parent = cur;
  38. cur = cur->_right;
  39. }
  40. else if (cur->_kv.first > kv.first)
  41. {
  42. parent = cur;
  43. cur = cur->_left;
  44. }
  45. else
  46. {
  47. return false;
  48. }
  49. }
  50. cur = new Node(kv);
  51. if (parent->_kv.first > kv.first)
  52. {
  53. parent->_left = cur;
  54. }
  55. else
  56. {
  57. parent->_right = cur;
  58. }
  59. cur->_parent = parent;
  60. // 更新平衡因子
  61. while (parent)
  62. {
  63. if (cur == parent->_right)
  64. {
  65. parent->_bf++;
  66. }
  67. else
  68. {
  69. parent->_bf--;
  70. }
  71. if (parent->_bf == 1 || parent->_bf == -1)
  72. {
  73. // 继续更新
  74. parent = parent->_parent;
  75. cur = cur->_parent;
  76. }
  77. else if (parent->_bf == 0)
  78. {
  79. break;
  80. }
  81. else if (parent->_bf == 2 || parent->_bf == -2)
  82. {
  83. // 需要旋转处理 -- 1、让这颗子树平衡 2、降低这颗子树的高度
  84. if (parent->_bf == 2 && cur->_bf == 1)
  85. {
  86. RotateL(parent);
  87. }
  88. else if (parent->_bf == -2 && cur->_bf == -1)
  89. {
  90. RotateR(parent);
  91. }
  92. else if (parent->_bf == -2 && cur->_bf == 1)
  93. {
  94. RotateLR(parent);
  95. }
  96. else if (parent->_bf == 2 && cur->_bf == -1)
  97. {
  98. RotateRL(parent);
  99. }
  100. else
  101. {
  102. assert(false);
  103. }
  104. break;
  105. }
  106. else
  107. {
  108. assert(false);
  109. }
  110. }
  111. return true;
  112. }

三、 AVL树的旋转

如果在一棵原本是平衡的AVL树中插入一个新节点,可能造成不平衡,此时必须调整树的结构, 使之平衡化。根据节点插入位置的不同,AVL树的旋转分为四种:

1. 新节点插入较高左子树的左侧---左左:右单旋

在插入之前我们的左侧是比较高的,也就是左节点高,以为我们定义这个平衡因子时是将左边高定义为负,右边高定义为正;

当左边高再在左边插入节点时,就会让左边更高了,原本左边高,这里的高度差为-1,当再次向左侧插入节点时,高度差就为-2了,这时我们就需要进行旋转这颗树了;如何旋转呢?

代码实现: 

  1. void RotateR(Node* parent)
  2. {
  3. Node* subL = parent->_left;
  4. Node* subLR = subL->_right;
  5. parent->_left = subLR;
  6. if (subLR)
  7. subLR->_parent = parent;
  8. Node* ppnode = parent->_parent;
  9. subL->_right = parent;
  10. parent->_parent = subL;
  11. if (parent == _root)
  12. {
  13. _root = subL;
  14. _root->_parent = nullptr;
  15. }
  16. else
  17. {
  18. if (ppnode->_left == parent)
  19. {
  20. ppnode->_left = subL;
  21. }
  22. else
  23. {
  24. ppnode->_right = subL;
  25. }
  26. subL->_parent = ppnode;
  27. }
  28. subL->_bf = parent->_bf = 0;
  29. }

2. 新节点插入较高右子树的右侧---右右:左单旋

 左单旋就和右单旋反过来,但右边高时,在右边在插入节点,那么右边较左边的高度差就是2了,这个时候就需要进行左旋转;

代码实现:

  1. void RotateL(Node* parent)
  2. {
  3. Node* subR = parent->_right;
  4. Node* subRL = subR->_left;
  5. parent->_right = subRL;
  6. if (subRL)
  7. subRL->_parent = parent;
  8. Node* ppnode = parent->_parent;
  9. subR->_left = parent;
  10. parent->_parent = subR;
  11. if (ppnode == nullptr)
  12. {
  13. _root = subR;
  14. _root->_parent = nullptr;
  15. }
  16. else
  17. {
  18. if (ppnode->_left == parent)
  19. {
  20. ppnode->_left = subR;
  21. }
  22. else
  23. {
  24. ppnode->_right = subR;
  25. }
  26. subR->_parent = ppnode;
  27. }
  28. parent->_bf = subR->_bf = 0;
  29. }

3. 新节点插入较高左子树的右侧---左右:先左单旋再右单旋

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

  1. void RotateLR(Node* parent)
  2. {
  3. Node* subL = parent->_left;
  4. Node* subLR = subL->_right;
  5. int bf = subLR->_bf;
  6. RotateL(parent->_left);
  7. RotateR(parent);
  8. if (bf == 1)
  9. {
  10. parent->_bf = 0;
  11. subLR->_bf = 0;
  12. subL->_bf = -1;
  13. }
  14. else if (bf == -1)
  15. {
  16. parent->_bf = 1;
  17. subLR->_bf = 0;
  18. subL->_bf = 0;
  19. }
  20. else if (bf == 0)
  21. {
  22. parent->_bf = 0;
  23. subLR->_bf = 0;
  24. subL->_bf = 0;
  25. }
  26. else
  27. {
  28. assert(false);
  29. }
  30. }

4. 新节点插入较高右子树的左侧---右左:先右单旋再左单旋 

  1. 由于插入新结点后,可能并不会立即进行旋转操作,而可能是在更新其祖先结点的过程中出现了不平衡而进行的旋转操作,因此用长方形表示下面的子树。
  2. 动图中,在b子树当中新增结点,或是在c子树当中新增结点,均会引发右左双旋,动图中以在c子树当中新增结点为例。
  1. void RotateRL(Node* parent)
  2. {
  3. Node* subR = parent->_right;
  4. Node* subRL = subR->_left;
  5. int bf = subRL->_bf;
  6. RotateR(parent->_right);
  7. RotateL(parent);
  8. if (bf == 1)
  9. {
  10. subR->_bf = 0;
  11. parent->_bf = -1;
  12. subRL->_bf = 0;
  13. }
  14. else if (bf == -1)
  15. {
  16. subR->_bf = 1;
  17. parent->_bf = 0;
  18. subRL->_bf = 0;
  19. }
  20. else if (bf == 0)
  21. {
  22. subR->_bf = 0;
  23. parent->_bf = 0;
  24. subRL->_bf = 0;
  25. }
  26. else
  27. {
  28. assert(false);
  29. }
  30. }

 四、验证AVL树

  1. void Test_AVLTree1()
  2. {
  3. //int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
  4. int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
  5. AVLTree<int, int> t1;
  6. for (auto e : a)
  7. {
  8. /* if (e == 14)
  9. {
  10. int x = 0;
  11. }
  12. */
  13. t1.Insert(make_pair(e, e));
  14. cout << e << "插入:" << t1.IsBalance() << endl;
  15. }
  16. t1.InOrder();
  17. cout << t1.IsBalance() << endl;
  18. }

通过高度变化验证是否平衡

  1. int _Height(Node* root)
  2. {
  3. if (root == NULL)
  4. return 0;
  5. int leftH = _Height(root->_left);
  6. int rightH = _Height(root->_right);
  7. return leftH > rightH ? leftH + 1 : rightH + 1;
  8. }
  9. bool _IsBalance(Node* root)
  10. {
  11. if (root == NULL)
  12. {
  13. return true;
  14. }
  15. int leftH = _Height(root->_left);
  16. int rightH = _Height(root->_right);
  17. if (rightH - leftH != root->_bf)
  18. {
  19. cout << root->_kv.first << "节点平衡因子异常" << endl;
  20. return false;
  21. }
  22. return abs(leftH - rightH) < 2
  23. && _IsBalance(root->_left)
  24. && _IsBalance(root->_right);
  25. }

总结

假如以pParent为根的子树不平衡,即pParent的平衡因子为2或者-2,分以下情况考虑 1. pParent的平衡因子为2,说明pParent的右子树高,设pParent的右子树的根为pSubR 当pSubR的平衡因子为1时,执行左单旋 当pSubR的平衡因子为-1时,执行右左双旋 2. pParent的平衡因子为-2,说明pParent的左子树高,设pParent的左子树的根为pSubL 当pSubL的平衡因子为-1是,执行右单旋 当pSubL的平衡因子为1时,执行左右双旋 旋转完成后,原pParent为根的子树个高度降低,已经平衡,不需要再向上更新。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/956480
推荐阅读
相关标签
  

闽ICP备14008679号