-
Notifications
You must be signed in to change notification settings - Fork 0
/
RBTree.h
521 lines (424 loc) · 13.8 KB
/
RBTree.h
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//
// Created by yulan on 2021/3/1.
//
#ifndef RBTREE_RBTREE_H
#define RBTREE_RBTREE_H
#include <iostream>
namespace yulan {
template<typename T>
class RBTree {
private:
enum NodeColor {
Red, Black
};
// 节点类型
struct TreeNode {
T key;
TreeNode *l_child;
TreeNode *r_child;
TreeNode *parent;
NodeColor color;
explicit TreeNode(T key_, TreeNode *l_child_ = nullptr,
TreeNode *r_child_ = nullptr, TreeNode *parent_ = nullptr)
: key(key_),
l_child(l_child_),
r_child(r_child_),
parent(parent_),
color(Red) {}
};
private:
TreeNode *root;
private:
static inline bool is_red(TreeNode *node) {
return node && node->color == Red;
}
static inline bool is_black(TreeNode *node) {
return !node || node->color == Black;
}
static inline void set_black(TreeNode *node) {
node->color = Black;
}
static inline void set_red(TreeNode *node) {
node->color = Red;
}
/**
* 把src的颜色染给dest
* @param dest
* @param src
*/
static inline void copy_color(TreeNode *dest, TreeNode *src) {
dest->color = src->color;
}
static inline NodeColor get_color(TreeNode *node) {
return node->color;
}
/**
* 值复制
* @param dest
* @param src
*/
static inline void copy_element(TreeNode *dest, TreeNode *src) {
dest->key = src->key;
}
/**
* 中序遍历打印辅助函数
* @param node
*/
void inOrderPrint_(TreeNode *node);
/**
* 查找key
* @param key
* @return 有key的指针,key不存在返回空
*/
TreeNode *search(T key);
/**
* 右旋
* @param node
*/
void rRotate(TreeNode *node);
/**
* 左旋
* @param node
*/
void lRotate(TreeNode *node);
/**
* 更新时,双红修正
* @param node
*/
void updateFixup(TreeNode *node);
/**
* 删除时,双黑修正
* @param node 替代被删除节点的节点
* @param parent node的父亲
*/
void deleteFixup(TreeNode *node, TreeNode *parent);
/**
* 先序遍历打印树
* @param node
*/
void travelPrintTree(TreeNode *node);
void destroy(TreeNode *node);
public:
RBTree() : root(nullptr) {}
~RBTree() {
destroy(root);
}
public:
bool exist(T key);
/**
* 插入key,若key存在则什么也不做
* @param key
*/
void insert(T key);
/**
* 删除key
* @param key
*/
void remove(T key);
/**
* 中序遍历打印
*/
void inOrderPrint();
/**
* 打印树
*/
void printTree() {
travelPrintTree(root);
}
};
template<typename T>
void RBTree<T>::inOrderPrint_(RBTree::TreeNode *node) {
if (!node) return;
inOrderPrint_(node->l_child);
std::cout << node->key << " ";
inOrderPrint_(node->r_child);
}
template<typename T>
void RBTree<T>::inOrderPrint() {
inOrderPrint_(root);
std::cout << std::endl;
}
template<typename T>
typename RBTree<T>::TreeNode *RBTree<T>::search(T key) {
TreeNode *p = root;
while (p) {
if (p->key == key)
return p;
else if (key < p->key)
p = p->l_child;
else
p = p->r_child;
}
return nullptr;
}
template<typename T>
bool RBTree<T>::exist(T key) {
return search(key) != nullptr;
}
template<typename T>
void RBTree<T>::lRotate(RBTree::TreeNode *node) {
TreeNode *rChild = node->r_child;
node->r_child = rChild->l_child;
if (rChild->l_child)
rChild->l_child->parent = node;
rChild->l_child = node;
if (node == root)
root = rChild;
else {
if (node->parent->l_child == node)
node->parent->l_child = rChild;
else
node->parent->r_child = rChild;
}
rChild->parent = node->parent;
node->parent = rChild;
}
template<typename T>
void RBTree<T>::rRotate(RBTree::TreeNode *node) {
TreeNode *lChild = node->l_child;
node->l_child = lChild->r_child;
if (lChild->r_child)
lChild->r_child->parent = node;
lChild->r_child = node;
if (node == root)
root = lChild;
else {
if (node->parent->l_child == node)
node->parent->l_child = lChild;
else
node->parent->r_child = lChild;
}
lChild->parent = node->parent;
node->parent = lChild;
}
template<typename T>
void RBTree<T>::updateFixup(RBTree::TreeNode *node) {
if (node == root) {
set_black(node);
return;
}
TreeNode *parent;
TreeNode *gran_parent;
TreeNode *uncle;
while ((parent = node->parent) && is_red(parent)) {
gran_parent = parent->parent; // parent为红,一定不是root
// 父亲是祖父的左孩子
if (parent == gran_parent->l_child) {
uncle = gran_parent->r_child;
// 三红
if (is_red(uncle)) {
set_black(parent);
set_black(uncle);
set_red(gran_parent);
// 以祖父为当前节点继续递归
node = gran_parent;
continue;
}
// 红红黑
else {
// 左右情形,需要先进行一次左旋
if (node == parent->r_child) {
lRotate(parent);
parent = node;// parent会被染成黑色
}
rRotate(gran_parent);
// 染色
set_red(gran_parent);
set_black(parent);
break;
}
}
// 父亲是祖父的右孩子
else {
uncle = gran_parent->l_child;
// 三红
if (is_red(uncle)) {
set_black(parent);
set_black(uncle);
set_red(gran_parent);
// 以祖父为当前节点继续递归
node = gran_parent;
continue;
}
// 红红黑
else {
// 右左情形,需要先进行一次右旋
if (node == parent->l_child) {
rRotate(parent);
parent = node;// parent会被染成黑色
}
lRotate(gran_parent);
// 染色
set_red(gran_parent);
set_black(parent);
break;
}
}
}
set_black(root);
}
template<typename T>
void RBTree<T>::insert(T key) {
// 空树的处理
if (!root) {
root = new TreeNode(key);
set_black(root);
return;
}
// 寻找插入位置
TreeNode *node = root;
TreeNode *parent;
while (node) {
parent = node;
if (node->key == key)
return;
else if (key < node->key)
node = node->l_child;
else
node = node->r_child;
}
if (key < parent->key) {
parent->l_child = new TreeNode(key, nullptr, nullptr, parent);
updateFixup(parent->l_child);
} else {
parent->r_child = new TreeNode(key, nullptr, nullptr, parent);
updateFixup(parent->r_child);
}
}
template<typename T>
void RBTree<T>::remove(T key) {
TreeNode *node = search(key);
if (node == nullptr)
return;
TreeNode *parent;
TreeNode *replacer;
// 有两个孩子
if (node->l_child != nullptr && node->r_child != nullptr) {
// 找到直接后继
TreeNode *successor;
successor = node->r_child;
while (successor->l_child)
successor = successor->l_child;
// 值复制
copy_element(node, successor);
node = successor;
}
// 单子节点或无子节点
if (node->l_child) {
replacer = node->l_child;
} else
replacer = node->r_child;
// 把父节点续上,隔离出node
parent = node->parent;
if (parent) {
if (node == parent->l_child)
parent->l_child = replacer;
else
parent->r_child = replacer;
} else {
root = replacer;
}
if (replacer)
replacer->parent = parent;
if (is_black(node))
deleteFixup(replacer, parent);
delete node;
}
template<typename T>
void RBTree<T>::deleteFixup(RBTree::TreeNode *node, RBTree::TreeNode *parent) {
while ((node != root) && is_black(node)) {
if (parent->l_child == node) {
TreeNode *brother = parent->r_child; // 被删除的节点是黑节点才会进入此函数,黑节点一定有兄弟
// 情形1,兄弟是红色
if (is_red(brother)) {
set_red(parent);
set_black(brother);
lRotate(parent);
brother = parent->r_child;
}
// 旋转前brother是红色,其子节点一定是黑色,而旋转后node的兄弟是brother的左儿子
// 故经过选择之后的brother一定是黑色
// 情形2,brother是黑色,且其两个子节点均为黑色
if (is_black(brother->l_child) && is_black(brother->r_child)) {
set_red(brother);
node = parent;
if (node)
parent = node->parent;
} else {
// 情形3,brother是黑色,且brother的儿子左红右黑
if (is_red(brother->l_child) && is_black(brother->r_child)) {
set_black(brother->l_child);
set_red(brother);
rRotate(brother);
brother = parent->r_child;
}
// 转换后brother的右儿子一定是红色
// 情形4,brother是黑色,且其右儿子是红色
copy_color(brother, parent);
set_black(parent); // 交换parent与brother颜色
set_black(brother->r_child);
lRotate(parent);
break;
}
} else {
// 对称
TreeNode *brother = parent->l_child; // 被删除的节点是黑节点才会进入此函数,黑节点一定有兄弟
// 情形1,
if (is_red(brother)) {
set_red(parent);
set_black(brother);
rRotate(parent);
brother = parent->l_child;
}
// 情形2,
if (is_black(brother->l_child) && is_black(brother->r_child)) {
set_red(brother);
node = parent;
if (node)
parent = node->parent;
} else {
// 情形3,
if (is_red(brother->r_child) && is_black(brother->l_child)) {
set_black(brother->r_child);
set_red(brother);
lRotate(brother);
brother = parent->l_child;
}
// 情形4,
copy_color(brother, parent);
set_black(parent);
set_black(brother->l_child);
rRotate(parent);
break;
}
}
}
if (node)
set_black(node);
}
template<typename T>
void RBTree<T>::travelPrintTree(RBTree::TreeNode *node) {
if (node) {
if (node == root) // tree是根节点
std::cout << node->key << "(B) is root";
else // tree是分支节点
std::cout << node->key << (is_red(node) ? "(R)" : "(B)");
std::cout << "\t左孩子:" << ((node->l_child) ? node->l_child->key : 0)
<< (is_red(node->l_child) ? "(R)" : "(B)");
std::cout << "\t右孩子:" << ((node->r_child) ? node->r_child->key : 0)
<< (is_red(node->r_child) ? "(R)" : "(B)");
std::cout << std::endl;
travelPrintTree(node->l_child);
travelPrintTree(node->r_child);
}
}
template<typename T>
void RBTree<T>::destroy(RBTree::TreeNode *node) {
if (node == nullptr)
return;
destroy(node->l_child);
destroy(node->r_child);
delete node;
}
}
#endif //RBTREE_RBTREE_H