-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRBTree
More file actions
309 lines (283 loc) · 5.69 KB
/
RBTree
File metadata and controls
309 lines (283 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#pragma once
#include<iostream>
#include<utility>
using namespace std;
//节点颜色
enum Color
{
RED,
BLACK
};
//节点定义
template<class K,class V>
struct RBTreeNode
{
std::pair<K,V> _val;//值
RBTreeNode<K, V>* _left;//左孩子
RBTreeNode<K, V>* _right;//右孩子
RBTreeNode<K, V>* _parent;//双亲节点
Color _col;//颜色
//构造函数
RBTreeNode(const pair<K,V>& kv)
:_val(kv)
, _left(nullptr), _right(nullptr), _parent(nullptr)
, _col(RED)
{}
};
template<class K,class V>
class RBTree
{
typedef RBTreeNode<K, V> Node;
private:
Node* _root = nullptr;//根节点
public:
//构造函数---使用默认即可
//插入
pair<Node*, bool> insert(const pair<K,V>& node)
{
//申请节点
Node* newNode = new Node(node);
//如果根节点为空,直接插入为根节点
if (_root == nullptr)
{
_root = newNode;
_root->_col = BLACK;
return make_pair(_root,true);
}
//找插入位置进行插入
Node* cur = _root;
Node* parent = nullptr;
while (cur)
{
if (cur->_val.first > newNode->_val.first)
{
//插入的节点比cur小,左插
parent = cur;
cur = cur->_left;
}
else if (cur->_val.first < newNode->_val.first)
{
//插入的节点比cur大,右插
parent = cur;
cur = cur->_right;
}
else
{
//插入的节点存在,直接返回该节点的val
return make_pair(cur,false);
}
}
//找到了插入位置,进行插入
if (parent->_val.first > newNode->_val.first)
{
//插入到parent的左边
parent->_left = newNode;
newNode->_parent = parent;
}
else
{
//插入到parent的右边
parent->_right = newNode;
newNode->_parent = parent;
}
//插入成功,对树进行调整
cur = newNode;
parent = cur->_parent;
//新插入节点的父节点是红色的才需要调整---走到这里,新插入的节点父节点肯定存在
while (parent && parent->_col == RED)
{
//走到这里grandParent节点必然是黑色的
Node* grandParent = parent->_parent;
Node* uncle = nullptr;
if (grandParent->_left == parent)
{
uncle = grandParent->_right;
}
else
{
uncle = grandParent->_left;
}
//情况1:新插入节点的叔叔节点存在且为红
if (uncle && uncle->_col == RED)
{
//将父节点和叔叔节点变成黑色,爷爷节点变成红色
uncle->_col = BLACK;
parent->_col = BLACK;
grandParent->_col = RED;
//继续迭代
cur = grandParent;
parent = cur->_parent;
}
else
{
//新插入节点的叔叔节点不存在或者新插入节点的叔叔节点为黑色
if (grandParent->_left == parent)
{
if (parent->_left == cur)
{
//右单旋
RotateR(grandParent);
//调整颜色
parent->_col = BLACK;
grandParent->_col = RED;
}
else
{
//左右双旋
RotateL(parent);
RotateR(grandParent);
//调整颜色
cur->_col = BLACK;
grandParent->_col = RED;
}
}
else
{
if (parent->_right == cur)
{
//左单旋
RotateL(grandParent);
parent->_col = BLACK;
grandParent->_col = RED;
}
else
{
//右左双旋
RotateR(parent);
RotateL(grandParent);
//调整颜色
cur->_col = BLACK;
grandParent->_col = RED;
}
}
break;
}
}
//在调整过程中,有可能将根节点变成了红色节点,因此需要将根节点调整成黑色的
_root->_col = BLACK;
return make_pair(newNode,true);
}
//右单旋
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
Node* parentParent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
_root->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subL;
}
else
{
parentParent->_right = subL;
}
subL->_parent = parentParent;
}
}
//左单旋
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
{
subRL->_parent = parent;
}
subR->_left = parent;
Node* parentParent = parent->_parent;
parent->_parent = subR;
if (_root == parent)
{
_root = subR;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subR;
}
else
{
parentParent->_right = subR;
}
}
subR->_parent = parentParent;
}
static void _inOrder(Node* root)
{
if (root == nullptr)
return;
_inOrder(root->_left);
std::cout << root->_val.first << " ";
_inOrder(root->_right);
}
//中序遍历
void inOrder()
{
_inOrder(_root);
std::cout << endl;
}
bool RedNode(Node* root)
{
if (root == nullptr)
{
return true;
}
if (root->_col == RED)
{
//判断父节点是否为红色
if (root->_parent && root->_parent->_col == RED)
{
return false;
}
}
//判断左右子树
return RedNode(root->_left) && RedNode(root->_right);
}
bool BlackNodeNum(Node* root,int blackNum,int num)
{
//检查是否每条路径上的黑色节点的个数都相同
if (root == nullptr)
{
return blackNum == num;
}
if (root->_col == BLACK)
{
blackNum++;
}
return BlackNodeNum(root->_left, blackNum, num) && BlackNodeNum(root->_right, blackNum, num);
}
//检查红黑树
bool check()
{
if (_root && _root->_col == RED)
{
return false;
}
//求出一条路径上黑色节点的个数
int num = 0;
Node* cur = _root;
while (cur)
{
if (cur->_col == BLACK)
{
num++;
}
cur = cur->_left;
}
return RedNode(_root) && BlackNodeNum(_root, 0,num);
}
};