• 红黑树(思维导图详解版)


    目录

    资源已上传

    实现代码

    测试代码


    资源已上传

    部分图片,网页版可下载

    实现代码

    注意判断是否为红黑树的代码实现,实现代码中红黑树的删除

    1. #pragma once
    2. #include
    3. using namespace std;
    4. enum Color_Type
    5. {
    6. Red,
    7. Black
    8. };
    9. template<class K,class V>
    10. struct RBTreeNode
    11. {
    12. pair _kv;
    13. RBTreeNode* _parent;
    14. RBTreeNode* _left;
    15. RBTreeNode* _right;
    16. Color_Type _color;
    17. RBTreeNode(const pair kv)
    18. : _kv(kv)
    19. , _parent(nullptr)
    20. , _left(nullptr)
    21. , _right(nullptr)
    22. , _color(Red)
    23. {}
    24. };
    25. template<class K, class V>
    26. class RBTree
    27. {
    28. typedef RBTreeNode Node;
    29. private:
    30. Node* _root;
    31. public:
    32. RBTree()
    33. :_root(nullptr)
    34. {}
    35. bool Insert(const pair& kv)
    36. {
    37. if (_root == nullptr)
    38. {
    39. _root = new Node(kv);
    40. _root->_color = Black;
    41. return true;
    42. }
    43. Node* cur = _root;//指向当前节点的位置
    44. Node* parent = nullptr;//当前节点的父节点
    45. //找到插入位置
    46. while (cur)
    47. {
    48. if (cur->_kv.first < kv.first) {
    49. parent = cur;
    50. cur = cur->_right;
    51. }
    52. else if (cur->_kv.first > kv.first) {
    53. parent = cur;
    54. cur = cur->_left;
    55. }
    56. else {
    57. return false;
    58. }
    59. }
    60. cur = new Node(kv);
    61. cur->_color = Red;
    62. if (cur->_kv.first < parent->_kv.first) {
    63. parent->_left = cur;
    64. cur->_parent = parent;
    65. }
    66. else
    67. {
    68. parent->_right = cur;
    69. cur->_parent = parent;
    70. }
    71. //父节点为红色,插入后还需做调整
    72. while (parent && parent->_color == Red)
    73. {
    74. Node* grandfather = parent->_parent;
    75. if (parent == grandfather->_left)
    76. {
    77. Node* uncle = grandfather->_right;
    78. if (uncle && uncle->_color == Red)
    79. {
    80. // 变色
    81. parent->_color = uncle->_color = Black;
    82. grandfather->_color = Red;
    83. // 继续向上处理
    84. cur = grandfather;
    85. parent = cur->_parent;
    86. }
    87. else//uncle不存在或uncle为黑
    88. {
    89. if (cur == parent->_left)
    90. {
    91. // g
    92. // p
    93. //c
    94. rotateR(grandfather);
    95. parent->_color = Black;
    96. grandfather->_color = Red;
    97. }
    98. else
    99. {
    100. // g
    101. //p
    102. // c
    103. rotateL(parent);
    104. rotateR(grandfather);
    105. cur->_color = Black;
    106. grandfather->_color = Red;
    107. }
    108. break;
    109. }
    110. }
    111. else
    112. {
    113. Node* uncle = grandfather->_left;
    114. // u存在且为红
    115. if (uncle && uncle->_color == Red)
    116. {
    117. // 变色
    118. parent->_color = uncle->_color = Black;
    119. grandfather->_color = Red;
    120. // 继续向上处理
    121. cur = grandfather;
    122. parent = cur->_parent;
    123. }
    124. else
    125. {
    126. if (cur == parent->_right)
    127. {
    128. // g
    129. // p
    130. // c
    131. rotateL(grandfather);
    132. grandfather->_color = Red;
    133. parent->_color = Black;
    134. }
    135. else
    136. {
    137. // g
    138. // p
    139. // c
    140. rotateR(parent);
    141. rotateL(grandfather);
    142. cur->_color = Black;
    143. grandfather->_color = Red;
    144. }
    145. break;
    146. }
    147. }
    148. }
    149. _root->_color = Black;
    150. return true;
    151. }
    152. bool IsBalance()
    153. {
    154. return _IsBalance(_root);
    155. }
    156. int Height()
    157. {
    158. return _Height(_root);
    159. }
    160. bool checkColor(Node* root, int blacknum, int basenum)
    161. {
    162. if (root == nullptr)
    163. {
    164. if (blacknum != basenum)
    165. {
    166. return false;
    167. }
    168. return true;
    169. }
    170. if (root->_color == Black)
    171. {
    172. ++blacknum;
    173. }
    174. if (root->_parent && root->_parent->_color == Red && root->_color == Red)
    175. {
    176. cout << root->_kv.first << "出现连续红色节点" << endl;
    177. return false;
    178. }
    179. return checkColor(root->_left, blacknum, basenum) && checkColor(root->_right, blacknum, basenum);
    180. }
    181. private:
    182. void rotateL(Node* parent)
    183. {
    184. Node* subR = parent->_right;
    185. Node* subRL = subR->_left;
    186. Node* pparent = parent->_parent;
    187. //1.建立subRL与parent之间的关系
    188. //左子树滑动
    189. parent->_right = subRL;
    190. if (subRL) {
    191. subRL->_parent = parent;
    192. }
    193. //2.建立subR和parent之间的关系
    194. //更新右子树的左子树
    195. subR->_left = parent;
    196. parent->_parent = subR;
    197. //3.建立pparent和subR之间的关系
    198. //与上一个节点建立连接
    199. if (parent == _root)
    200. {
    201. _root = subR;
    202. subR->_parent == nullptr;
    203. }
    204. else
    205. {
    206. subR->_parent = pparent;
    207. if (parent = pparent->_left) {
    208. pparent->_left = subR;
    209. }
    210. else {
    211. pparent->_right = subR;
    212. }
    213. }
    214. }
    215. void rotateR(Node* parent)
    216. {
    217. Node* subL = parent->_left;
    218. Node* subLR = subL->_right;
    219. Node* pparent = parent->_parent;
    220. //滑动
    221. parent->_left = subLR;
    222. if (subLR) {
    223. subLR->_parent = parent;
    224. }
    225. //更新左子树的右子树
    226. subL->_right = parent;
    227. parent->_parent = subL;
    228. if (parent == _root) {
    229. _root = subL;
    230. subL->_parent = nullptr;
    231. }
    232. else {
    233. subL->_parent == pparent;
    234. if (parent = pparent->_left) {
    235. pparent->_left = subL;
    236. }
    237. else {
    238. pparent->_right = subL;
    239. }
    240. }
    241. }
    242. int _Height(Node* root)
    243. {
    244. if (!root) {
    245. return 0;
    246. }
    247. int left = _Height(root->_left);
    248. int right = _Height(root->_right);
    249. return left > right ? left + 1 : right + 1;
    250. }
    251. bool _IsBalance(Node* root)
    252. {
    253. if (root == nullptr)
    254. {
    255. return true;
    256. }
    257. if (root->_color != Black)
    258. {
    259. return false;
    260. }
    261. int basenum = 0;
    262. Node* cur = _root;
    263. while (cur)
    264. {
    265. if (cur->_color == Black) {
    266. ++basenum;
    267. }
    268. cur = cur->_left;
    269. }
    270. return checkColor(root, 0, basenum);
    271. }
    272. };

    测试代码

    1. #include "RB_tree.h"
    2. #include
    3. int main()
    4. {
    5. const int N = 10;
    6. vector<int> v;
    7. v.reserve(N);
    8. srand(time(0));
    9. for (size_t i = 0; i < N; i++)
    10. {
    11. v.push_back(i);
    12. }
    13. for (auto i : v)
    14. {
    15. cout << i << " ";
    16. }
    17. cout << endl;
    18. RBTree<int, int> rbt;
    19. for (auto e : v)
    20. {
    21. rbt.Insert(make_pair(e, e));
    22. cout << "Insert:" << e << endl;
    23. }
    24. cout << rbt.Height() << endl;
    25. if (rbt.IsBalance())
    26. {
    27. cout << "ok" << endl;
    28. }
    29. return 0;
    30. }

  • 相关阅读:
    ChatGPT批量写作文章软件
    数据分析常用指标解析及其适用场景
    如何免费pdf全部转化为word版
    mp4封装格式各box类型讲解及IBP帧计算
    nginx安装(离线安装,新增--with-http_ssl_module、--with-stream模块,离线升级)
    python之 ffmpeg+opencv绿幕抠图,蒙版绿幕抠图,透明化处理,PIL检测图片是否包含透明通道
    如何通过提升客户体验带来更大的增长、更好的客户留存率?
    【高等数学】一些零碎知识点
    通过windows画图软件(工具)确定图像中指定区域的坐标范围、知道坐标范围就可以批量对图像进行裁剪(crop)操作
    WebRTC系列--track的set_enabled详解
  • 原文地址:https://blog.csdn.net/qq_73881574/article/details/132922815