• AVL树实现


    目录

    ​编辑

    一,AVL树的概念

    二,实现AVL树(部分)

    1.AVL树的节点

    2.AVL数的插入

    1.当根节点为nullptr时要执行如下代码:

    2.当根节点不为nullptr时

    1.当parent的_bf变为0时,parent之前的_bf的大小就是-1或者1如下:

    2.当parent的平衡因子被更新为1或者-1时:

    3.当parent的平衡因子更新后变成-2或者2时

    三,一些其他函数

    四,全部代码


    一,AVL树的概念

    首先我们得先知道AVL树是个啥,AVL树是个啥呢?百度一下你就知道:

    在百度上的概念如上。根据AVL树的概念我们可以总结出AVL树有如下特点:

    1.本身首先是一棵二叉搜索树。

    2.带有平衡条件:每个结点的左右子树的高度之差的绝对值(平衡因子)最多为1。

    二,实现AVL树(部分)

    1.AVL树的节点

     首先我们根据AVL树的特点可以想到的AVL树节点成员有:

    1.因为AVL树还是一棵二叉树,所以AVL树中必有指针left与right,而且还有一个变量表示节点值。

    2.因为二AVL树需要记录高度差,所以在节点内就会有一个变量来记录高度差,这个变量便是平衡因子。并且这个变量的类型是int。

    3.最后为了方便后续操作,我们的节点内还会存放一个指针叫做parent,这个节点指向当前节点的父节点。

    所以写出的节点代码如下:

    1. template <class K,class V>
    2. struct AVL_Node//节点
    3. {
    4. AVL_Node(pair& key)
    5. :parent(nullptr)
    6. ,_left(nullptr)
    7. ,_right(nullptr)
    8. ,_bf(0)
    9. ,_key(key)
    10. {}
    11. AVL_Node* parent;//父节点指针
    12. AVL_Node* _left;//左孩子指针
    13. AVL_Node* _right;//右孩子指针
    14. int _bf;//平衡因子
    15. pair_key;//val
    16. };

    2.AVL数的插入

    AVL树的插入是我们这次实现的重头戏,在插入时要理解的重点内容便是:

    1.插入时的旋转。

    2.平衡因子的调整。

    现在先来写在旋转前的代码:

    1.当根节点为nullptr时要执行如下代码:
    1. if (_root == nullptr)//当根节点为空时
    2. {
    3. _root = new AVL_Node(key);
    4. }
    2.当根节点不为nullptr时

    1.先找到插入位置:

    1. AVL_Node* cur = _root;
    2. AVL_Node* parent = cur;
    3. while (cur)
    4. {
    5. if (key < cur->_key)
    6. {
    7. parent = cur;
    8. cur = cur->_left;
    9. }
    10. else if (key > cur->_key)
    11. {
    12. parent = cur;
    13. cur = cur->_right;
    14. }
    15. else
    16. {
    17. return false;
    18. }
    19. }

    这段寻找插入位置的代码和二叉搜索树的代码相同。

    2.找到插入位置以后便开始进行插入

    1. cur = new AVL_Node(key);
    2. if (key.first > parent->_key.first)
    3. {
    4. parent->_right = cur;
    5. }
    6. else
    7. {
    8. parent->_left = cur;
    9. }
    10. cur->_parent = parent;//更新父节点

    3.调整上一个节点的平衡因子。依据:_bf = 右节点的高度--左节点的高度

    所以当我插入左边时:

    1. if (cur == parent->_left)//左边添加节点,parent的_bf减1
    2. {
    3. parent->_bf--;
    4. }

    当我插入右边时:

    1. else if (cur == parent->_right)//右边添加节点,parntt的高度加1
    2. {
    3. parent->_bf++;
    4. }

    4.根据父节点的_bf的大小来决定是否需要往上调整

    1.当parent的_bf变为0时,parent之前的_bf的大小就是-1或者1如下:

    其实我的parent变成0以后AVL树的高度下降,所以对我的其它节点的平衡因子的大小是没有什么影响的,所以在这种情况下我便可以直接退出:

    1. if (parent->_bf == 0)//子树高度不变不影响整棵树高度
    2. {
    3. break;
    4. }
    2.当parent的平衡因子被更新为1或者-1时:

    能变更新成这种情况,那我之前的平衡因子一定为0了。这时候,我整棵树的高度肯定是增加了,所以平衡因子必须改变所以代码如下:

    1. else if (parent->_bf == 1 || parent->_bf == -1)//子树高度变了,但是在接受范围内,节点的平衡因子要做调整
    2. {
    3. cur = parent;
    4. parent = parent->_parent;
    5. }

    这段代码是不全的,这里只展示了节点向上调整的操作,如果想看整段代码请往后看。

    3.当parent的平衡因子更新后变成-2或者2时

    这个时候就发生异常了,这个时候就要开始旋转了。这个时候的旋转又分为四种:

    1.单纯的右边高

    如以下情况:

    可以看到在插入新的节点以后,树的高度发生了变化并且都是右边的高度高且出现了异常情况。所以这个时候就要发生左旋来调整高度,示意图如下:

    根据这个过程写出如下代码:

    1. void Leftuniflex(AVL_Node* parent)
    2. {
    3. //记录要旋转的节点
    4. AVL_Node* subR = parent->_right;
    5. AVL_Node* subRL = subR->_left;
    6. //旋转
    7. parent->_right = subRL;
    8. subR->_left = parent;
    9. //更新父节点
    10. AVL_Node* ppNode = parent->_parent;
    11. parent->_parent = subR;
    12. if (subRL)
    13. subRL->_parent = parent;
    14. if (parent == _root)
    15. {
    16. _root = subR;
    17. }
    18. subR->_parent = ppNode;
    19. //更新平衡因子
    20. parent->_bf = subR->_bf = 0;
    21. }

    因为在示意图里没有显示父节点的更新,所以在这里要注意对于父节点的更新。

    2.单纯的左边高

    如下情况:

    看到这个示意图是否有一种似曾相识的感觉呢?其实单纯的左边高的旋转操作方法和单纯右边高的操作方法一样,只不过旋转的方向改变了。根据示意图写出代码如下:
     

    1. void Rightuniflex(AVL_Node* parent)
    2. {
    3. //旋转
    4. AVL_Node* subL = parent->_left;
    5. AVL_Node* subLR = subL->_right;
    6. parent->_left = subLR;
    7. subL->_right = parent;
    8. //更新父节点
    9. AVL_Node* ppNode = parent->_parent;
    10. parent->_parent = subL;
    11. if (subLR)
    12. subLR->_parent = parent;
    13. if (parent == _root)
    14. {
    15. _root = subL;
    16. }
    17. subL->_parent = ppNode;
    18. //更新平衡因子
    19. parent->_bf = subL->_bf = 0;
    20. }

    3.右边整体高但是局部矮

    解决方法:1.将整棵AVL树通过右单旋调整成单纯的右边高然后再将整棵树通过左单旋调整成平衡的AVL树。比如如以下情况:

    一,新增节点插入到60的右节点b处,此时60的_bf为-1

    在这种情况下,首先便要以90这个节点为parent进行右单旋让整棵树变成单纯的右边高。如下图所示:

    在转化为单纯的右边高以后,便可以执行第二步了。第二步的操作便是和左旋操作是一样的,如图所示:

    二,新增节点插入到60的右节点c处,此时60的_bf为1

    画出示意图如下:

    然后还是一样的操作将这棵树变成单纯的右边高:

    然后继续执行第二步,也是单纯的左旋操作:

    三,当h为0时,新增节点为60。此时60的_bf = 0

    在这个时候还是一样的操作:先变成单纯的右边高

    然后再操作:

    再右边整体高,局部左边高的节点插入情况便是以上三种。其实这些情况的旋转操作都是一样的但是对应的平衡因子_bf的更新是不一样的。

    根据以上情况写出代码如下:

    1. //右左双旋
    2. void RLuniflex(AVL_Node* parent)
    3. {
    4. AVL_Node* subR = parent->_right;
    5. AVL_Node* subRL = subR->_left;
    6. int bf = subRL->_bf;
    7. Rightuniflex(subR);//先局部右旋
    8. Leftuniflex(parent);//再整体左旋
    9. //更新平衡因子
    10. if (bf == 0)//subRL自己就是新增
    11. {
    12. parent->_bf = subR->_bf = subRL->_bf = 0;
    13. }
    14. else if (bf == -1)
    15. {
    16. subR->_bf = 1;
    17. parent->_bf = subRL->_bf = 0;
    18. }
    19. else if (bf == 1)
    20. {
    21. parent->_bf = -1;
    22. subRL->_bf = subR->_bf = 0;
    23. }
    24. else
    25. {
    26. assert(false);
    27. }
    28. }

    4.当左边整体高但是右边局部高时:

    在这种情况下的分析方法和《当右边整体高但是左边局部高》的分析方法相同。也分为三步:

    1.现局部左旋让AVL树变成单纯的左边高。

    2.再整体右旋调整树的平衡。

    3.调整平衡因子。

    分析方法和上述代码相同,在这里就直接写代码了:

    1. //左右双旋(狗日的平衡因子,让我搞了半天,哭了)
    2. void LRuniflex(AVL_Node* parent)
    3. {
    4. AVL_Node* subL = parent->_left;
    5. AVL_Node* subLR = subL->_right;
    6. int bf = subLR->_bf;
    7. Leftuniflex(subL);//先局部左旋
    8. Rightuniflex(parent);//再整体右旋
    9. //更新平衡因子
    10. if (bf == 0)//subRL自己就是新增
    11. {
    12. parent->_bf = subL->_bf = subLR->_bf = 0;
    13. }
    14. else if (bf == -1)
    15. {
    16. subL->_bf = 0;
    17. parent->_bf = 1;
    18. subLR->_bf = 0;
    19. }
    20. else if (bf == 1)
    21. {
    22. parent->_bf = 0;
    23. subLR->_bf = 0;
    24. subL->_bf = -1;
    25. }
    26. else
    27. {
    28. assert(false);
    29. }
    30. }

    兄弟们在写代码时千万不要和我一样粗心,找bug找了好久!!!!

    三,一些其他函数

    1.中序遍历函数

    1. void Inorder()
    2. {
    3. _Inorder(_root);
    4. }
    1. void _Inorder(AVL_Node* root)
    2. {
    3. if (root == nullptr)
    4. {
    5. return;
    6. }
    7. _Inorder(root->_left);
    8. cout << root->_key.first << " ";
    9. _Inorder(root->_right);
    10. }

    记住,AVL树的中序遍历也是一个有序的数列。

    2.判断平衡的函数

    1. int Height(AVL_Node* root)
    2. {
    3. if (root == nullptr)
    4. {
    5. return 0;
    6. }
    7. int L = Height(root->_left);
    8. int R = Height(root->_right);
    9. return L > R ? L + 1 : R + 1;
    10. }
    11. bool _Isblance(AVL_Node* root)
    12. {
    13. if (root == nullptr)
    14. {
    15. return true;
    16. }
    17. int R = Height(root->_right);
    18. int L = Height(root->_left);
    19. int bf = root->_bf;
    20. if (R - L!=bf)
    21. {
    22. cout << "R-L:" << R - L << endl;
    23. cout << root->_key.first << "->" << root->_bf <<"平衡因子异常" << endl;
    24. return false;
    25. }
    26. return (abs(R-L)<2)&& _Isblance(root->_left) && _Isblance(root->_right);
    27. }

    四,全部代码

    1. #include
    2. #include
    3. using namespace std;
    4. using std::pair;
    5. namespace AVL
    6. {
    7. template <class K,class V>
    8. struct AVL_Node//节点
    9. {
    10. AVL_Node(pair& key)
    11. :_parent(nullptr)
    12. ,_left(nullptr)
    13. ,_right(nullptr)
    14. ,_bf(0)
    15. ,_key(key)
    16. {}
    17. AVL_Node* _parent;
    18. AVL_Node* _left;
    19. AVL_Node* _right;
    20. int _bf;
    21. pair_key;
    22. };
    23. template <class K, class V>
    24. class AVL_Tree
    25. {
    26. public:
    27. bool insert( pair key)
    28. {
    29. if (_root == nullptr)//当根节点为空时
    30. {
    31. _root = new AVL_Node(key);
    32. }
    33. else//当根节点不为空时
    34. {
    35. AVL_Node* cur = _root;
    36. AVL_Node* parent = cur;
    37. while (cur)
    38. {
    39. if (key.first < cur->_key.first)
    40. {
    41. parent = cur;
    42. cur = cur->_left;
    43. }
    44. else if (key.first > cur->_key.first)
    45. {
    46. parent = cur;
    47. cur = cur->_right;
    48. }
    49. else
    50. {
    51. return false;
    52. }
    53. }
    54. cur = new AVL_Node(key);
    55. if (key.first > parent->_key.first)
    56. {
    57. parent->_right = cur;
    58. }
    59. else
    60. {
    61. parent->_left = cur;
    62. }
    63. cur->_parent = parent;
    64. while (parent)
    65. {
    66. if (cur == parent->_left)//左边添加节点,parent的高度减1
    67. {
    68. parent->_bf--;
    69. }
    70. else if (cur == parent->_right)//右边添加节点,parent的高度加1
    71. {
    72. parent->_bf++;
    73. }
    74. if (parent->_bf == 0)//子树高度不变不影响整棵树高度
    75. {
    76. break;
    77. }
    78. else if (parent->_bf == 1 || parent->_bf == -1)//子树高度变了,但是在接受范围内,节点的平衡因子要做调整
    79. {
    80. cur = cur->_parent;
    81. parent = parent->_parent;
    82. }
    83. else if (parent->_bf == 2 || parent->_bf == -2)
    84. {
    85. //Inorder();
    86. //出现异常该旋转了
    87. //单纯右边高,发生左单旋
    88. if (parent->_bf == 2 && cur->_bf == 1)
    89. {
    90. Leftuniflex(parent);
    91. }
    92. //单纯左边高,发生右单旋
    93. else if (parent->_bf == -2 && cur->_bf == -1)
    94. {
    95. Rightuniflex(parent);
    96. }
    97. //右边整体高,局部矮
    98. else if (parent->_bf == 2 && cur->_bf == -1)
    99. {
    100. RLuniflex(parent);
    101. }
    102. //左边整体高,局部矮
    103. else if (parent->_bf == -2 && cur->_bf == 1)
    104. {
    105. LRuniflex(parent);
    106. }
    107. else
    108. {
    109. assert(false);
    110. }
    111. break;
    112. }
    113. else
    114. {
    115. assert(false);
    116. }
    117. }
    118. }
    119. return true;
    120. }
    121. void Leftuniflex(AVL_Node* parent)
    122. {
    123. AVL_Node* subR = parent->_right;
    124. AVL_Node* subRL = subR->_left;
    125. parent->_right = subRL;
    126. subR->_left = parent;
    127. //更新父节点
    128. AVL_Node* ppNode = parent->_parent;
    129. parent->_parent = subR;
    130. if (subRL)
    131. subRL->_parent = parent;
    132. if (parent == _root)
    133. {
    134. _root = subR;
    135. }
    136. else
    137. {
    138. if (parent == ppNode->_left)
    139. {
    140. ppNode->_left = subR;
    141. }
    142. else
    143. {
    144. ppNode->_right = subR;
    145. }
    146. }
    147. subR->_parent = ppNode;
    148. //更新平衡因子
    149. parent->_bf = subR->_bf = 0;
    150. }
    151. void Rightuniflex(AVL_Node* parent)
    152. {
    153. //旋转
    154. AVL_Node* subL = parent->_left;
    155. AVL_Node* subLR = subL->_right;
    156. parent->_left = subLR;
    157. subL->_right = parent;
    158. //更新父节点
    159. AVL_Node* ppNode = parent->_parent;
    160. parent->_parent = subL;
    161. if (subLR)
    162. subLR->_parent = parent;
    163. if (parent == _root)
    164. {
    165. _root = subL;
    166. }
    167. else
    168. {
    169. if (parent == ppNode->_left)
    170. {
    171. ppNode->_left = subL;
    172. }
    173. else
    174. {
    175. ppNode->_right = subL;
    176. }
    177. }
    178. subL->_parent = ppNode;
    179. //更新平衡因子
    180. parent->_bf = subL->_bf = 0;
    181. }
    182. //右左双旋
    183. void RLuniflex(AVL_Node* parent)
    184. {
    185. AVL_Node* subR = parent->_right;
    186. AVL_Node* subRL = subR->_left;
    187. int bf = subRL->_bf;
    188. Rightuniflex(subR);//先局部右旋
    189. Leftuniflex(parent);//再整体左旋
    190. //更新平衡因子
    191. if (bf == 0)//subRL自己就是新增
    192. {
    193. parent->_bf = subR->_bf = subRL->_bf = 0;
    194. }
    195. else if (bf == -1)
    196. {
    197. subR->_bf = 1;
    198. parent->_bf = subRL->_bf = 0;
    199. }
    200. else if (bf == 1)
    201. {
    202. parent->_bf = -1;
    203. subRL->_bf = subR->_bf = 0;
    204. }
    205. else
    206. {
    207. assert(false);
    208. }
    209. }
    210. //左右双旋(狗日的平衡因子,让我搞了半天,哭了)
    211. void LRuniflex(AVL_Node* parent)
    212. {
    213. AVL_Node* subL = parent->_left;
    214. AVL_Node* subLR = subL->_right;
    215. int bf = subLR->_bf;
    216. Leftuniflex(subL);//先局部左旋
    217. Rightuniflex(parent);//再整体右旋
    218. //更新平衡因子
    219. if (bf == 0)//subRL自己就是新增
    220. {
    221. parent->_bf = subL->_bf = subLR->_bf = 0;
    222. }
    223. else if (bf == -1)
    224. {
    225. subL->_bf = 0;
    226. parent->_bf = 1;
    227. subLR->_bf = 0;
    228. }
    229. else if (bf == 1)
    230. {
    231. parent->_bf = 0;
    232. subLR->_bf = 0;
    233. subL->_bf = -1;
    234. }
    235. else
    236. {
    237. assert(false);
    238. }
    239. }
    240. void Inorder()
    241. {
    242. _Inorder(_root);
    243. }
    244. void Isblance()
    245. {
    246. _Isblance(_root);
    247. }
    248. private:
    249. void _Inorder(AVL_Node* root)
    250. {
    251. if (root == nullptr)
    252. {
    253. return;
    254. }
    255. _Inorder(root->_left);
    256. cout << root->_key.first << " ";
    257. _Inorder(root->_right);
    258. }
    259. int Height(AVL_Node* root)
    260. {
    261. if (root == nullptr)
    262. {
    263. return 0;
    264. }
    265. int L = Height(root->_left);
    266. int R = Height(root->_right);
    267. return L > R ? L + 1 : R + 1;
    268. }
    269. bool _Isblance(AVL_Node* root)
    270. {
    271. if (root == nullptr)
    272. {
    273. return true;
    274. }
    275. int R = Height(root->_right);
    276. int L = Height(root->_left);
    277. int bf = root->_bf;
    278. if (R - L!=bf)
    279. {
    280. cout << "R-L:" << R - L << endl;
    281. cout << root->_key.first << "->" << root->_bf <<"平衡因子异常" << endl;
    282. return false;
    283. }
    284. return (abs(R-L)<2)&& _Isblance(root->_left) && _Isblance(root->_right);
    285. }
    286. AVL_Node* _root = nullptr;
    287. };
    288. }

     

  • 相关阅读:
    海康威视综合安防管理平台任意文件上传
    Python的输入输出(来自菜鸟教程)
    你不知道的浏览器页面渲染机制
    Multi-granularity Correspondence Learning from Long-term Noisy Videos--论文笔记
    【vue3-element-admin】Husky + Lint-staged + Commitlint + Commitizen + cz-git 配置 Git 提交规范
    资源 | Python可视化系列文章资源(源码+数据)
    【PTA题目】6-20 使用函数判断完全平方数 分数 10
    HTTP 之 options预请求 nginx 解决跨域 postman调试跨域问题
    nvm详解(mac环境nvm安装步骤及踩坑问题)
    外滩大会今日开幕 近20位“两院”院士、诺贝尔奖和图灵奖得主齐聚
  • 原文地址:https://blog.csdn.net/qq_41934502/article/details/134450149