-
Notifications
You must be signed in to change notification settings - Fork 7
/
RedBlackTree.cpp
executable file
·481 lines (391 loc) · 10.4 KB
/
RedBlackTree.cpp
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
#include "RedBlackTree.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
static RBNode RBNode_Nil = {RB_Black, 0, 0, 0, 0};
RBNode* RBTree_nil()
{
return &RBNode_Nil;
}
void RBTree_print(RBTree tree, int her)
{
int i;
RBNode* node = tree;
assert(node);
if (node != &RBNode_Nil) {
for (i = 0; i < her; i++) {
printf(" ");
}
printf("第 %d 层, %d(%c)\n",
her, node->key, node->color == RB_Black ? 'B' : 'R');
if (node->leftChild != &RBNode_Nil) {
RBTree_print(node->leftChild, her + 1);
}
if (node->rightChild != &RBNode_Nil) {
RBTree_print(node->rightChild, her + 1);
}
}
}
// 最小关键字元素
RBNode* RBTree_minimum(RBNode* node)
{
assert(node);
RBNode* temp = node;
while (temp->leftChild != &RBNode_Nil) {
temp = temp->leftChild;
}
return temp;
}
// 最大关键字元素
RBNode* RBTree_maximum(RBNode* node)
{
assert(node);
RBNode* temp = node;
while (temp->rightChild != &RBNode_Nil) {
temp = temp->rightChild;
}
return temp;
}
// 中序遍历中的前驱
RBNode* RBTree_predecessor(RBNode* node)
{
assert(node);
RBNode* child = node->leftChild;
// 没有左孩子,返回自身
if (child == &RBNode_Nil) {
return node;
}
// 只有左孩子,则左孩子是其直接前驱
else if (child->rightChild == &RBNode_Nil) {
return child->leftChild;
}
// 左右孩子均有,则右孩子树中最大的元素为其直接前驱
else {
return RBTree_maximum(child->rightChild);
}
}
// 中序遍历中的后继
RBNode* RBTree_successor(RBNode* node)
{
// 有右孩子,则右孩子树中最小的元素为其直接后继
if (node->rightChild != &RBNode_Nil) {
return RBTree_minimum(node->rightChild);
}
// 没有右孩子,向上找到的第一个左分支节点为其直接后继,
// 即 node 为其直接后继的左孩子树中的最大元素。
RBNode* temp = node;
RBNode* parent = temp->parent;
while (parent != &RBNode_Nil && temp == parent->rightChild) {
temp = parent;
parent = temp->parent;
}
return parent;
}
RBNode* RBTree_search(const RBTree tree, int key)
{
RBNode* node = tree;
while (node != &RBNode_Nil) {
if (node->key == key) {
return node;
}
else if (node->key < key) {
node = node->rightChild;
}
else {
node = node->leftChild;
}
}
return &RBNode_Nil;
}
// 左旋
// node right
// / \ / \
// a right --> node c
// / \ / \
// b c a b
//
void RBTree_left_rotate(RBTree* tree, RBNode* node)
{
assert(node->rightChild && (*tree)->parent == &RBNode_Nil);
RBNode* right = node->rightChild;
// set b
node->rightChild = right->leftChild;
if (right->leftChild != &RBNode_Nil) {
right->leftChild->parent = node;
}
right->parent = node->parent;
if (node->parent == &RBNode_Nil) {
*tree = right;
}
else if (node->parent->leftChild == node) {
node->parent->leftChild = right;
}
else {
node->parent->rightChild = right;
}
right->leftChild = node;
node->parent = right;
}
// 右旋
// node left
// / \ / \
// left c --> a node
// / \ / \
// a b b c
//
void RBTree_right_rotate(RBTree* tree, RBNode* node)
{
assert(node->leftChild && (*tree)->parent == &RBNode_Nil);
RBNode* left = node->leftChild;
// set b
node->leftChild = left->rightChild;
if (left->rightChild != &RBNode_Nil) {
left->rightChild->parent = node;
}
left->parent = node->parent;
if (node->parent == &RBNode_Nil) {
*tree = left;
}
else if (node->parent->leftChild == node) {
node->parent->leftChild = left;
}
else {
node->parent->rightChild = left;
}
left->rightChild = node;
node->parent = left;
}
// 插入调整
void RBTree_insert_fixup(RBTree* tree, RBNode* node)
{
assert(tree && node);
RBNode* parent = NULL;
RBNode* uncle = NULL;
RBNode* grand = NULL;
RBNode* temp = NULL;
parent = node->parent;
while (parent->color == RB_Red) {
// 根据红黑树性质,以及 node 的父亲的颜色为红色,
// 可以肯定 node 的祖父节点一定存在
grand = parent->parent;
// 确定叔父节点
if (parent == grand->leftChild) {
uncle = grand->rightChild;
// case 1: 叔父节点为红色
// grand(B) new node -> grand(R)
// / \ / \
// parent(R) uncle(R) --> node(B) uncle(B)
// / \ / \ / \ / \
// a node(R) d e parent node(R) d e
// / \ / \
// b c b c
//
if (uncle->color == RB_Red) {
parent->color = RB_Black;
uncle->color = RB_Black;
grand->color = RB_Red;
node = grand;
parent = node->parent;
}
// case 2, case 3:叔父节点为黑色
// case 2 ---> case 3 --> done
// parent is as new node
// grand(B) grand(B) node(B)
// / \ / \ / \
// parent(R) d node(R) d parent(R) grand(R)
// / \ / \ / \ / \
// a node(R) parent(R) c a b c d
// / \ / \
// b c a b
//
else {
// 将 case 2 装换成 case 3
// 注意:转换到 case 3之后, parent 为case 2中的 node,
// node 为 case 2 中的 parent
if (parent->rightChild == node) {
RBTree_left_rotate(tree, parent);
temp = parent;
parent = node;
node = temp;
}
// case 3
parent->color = RB_Black;
grand->color = RB_Red;
RBTree_right_rotate(tree, grand);
}
}
else {
// 与上面的情况对称
uncle = grand->leftChild;
if (uncle->color == RB_Red) {
parent->color = RB_Black;
uncle->color = RB_Black;
grand->color = RB_Red;
node = grand;
parent = node->parent;
}
else {
// 将 case 2 装换成 case 3
if (parent->leftChild == node) {
RBTree_right_rotate(tree, parent);
temp = parent;
parent = node;
node = temp;
}
// case 3
parent->color = RB_Black;
grand->color = RB_Red;
RBTree_left_rotate(tree, grand);
}
}
}
(*tree)->color = RB_Black;
}
// 将节点 node 插入树 tree 内,然后将 node 着色为红色,此时,树可能不再
// 满足红黑树性质,因此调用 RBTree_insert_fixup 来对节点重新着色调整。
void RBTree_insert(RBTree* tree, RBNode* node)
{
assert(tree && node);
RBNode* parent = &RBNode_Nil;
RBNode* temp = *tree;
// 像二叉树一样,在树中查找适当的位置插入
while (temp != &RBNode_Nil) {
parent = temp;
if (node->key < temp->key) {
temp = temp->leftChild;
}
else {
temp = temp->rightChild;
}
}
node->parent = parent;
// 树为空
if (parent == &RBNode_Nil) {
*tree = node;
}
else if (node->key < parent->key) {
parent->leftChild = node;
}
else {
parent->rightChild = node;
}
// 为节点着色
node->leftChild = &RBNode_Nil;
node->rightChild = &RBNode_Nil;
node->color = RB_Red;
// 调整树使之满足红黑树性质
RBTree_insert_fixup(tree, node);
}
// 删除调整
void RBTree_delete_fixup(RBTree* tree, RBNode* node)
{
RBNode* brother = NULL;
RBNode* parent = NULL;
while (node != *tree && node->color == RB_Black) {
parent = node->parent;
// 确定兄弟节点
if (node == parent->leftChild) {
brother = parent->rightChild;
// case 1: 兄弟节点为红色
if (brother->color == RB_Red) {
brother->color = RB_Black;
parent->color = RB_Red;
RBTree_left_rotate(tree, parent);
brother = node->parent->rightChild;
}
// case 2: 兄弟节点的两孩子均为黑色
if (brother->leftChild->color == RB_Black
&& brother->rightChild->color == RB_Black) {
brother->color = RB_Red;
node = parent;
}
else {
// case 3: 兄弟节点的左孩子为红色,右孩子为黑色
if (brother->rightChild->color == RB_Black) {
brother->leftChild->color = RB_Black;
brother->color = RB_Red;
RBTree_right_rotate(tree, brother);
brother = node->parent->rightChild;
}
// case 4:兄弟节点的右孩子为红色
brother->color = parent->color;
parent->color = RB_Black;
brother->rightChild->color = RB_Black;
RBTree_left_rotate(tree, parent);
node = *tree;
}
}
else {
brother = parent->leftChild;
// case 1: 兄弟节点为红色
if (brother->color == RB_Red) {
brother->color = RB_Black;
parent->color = RB_Red;
RBTree_right_rotate(tree, parent);
brother = parent->leftChild;
}
// case 2: 兄弟节点的两孩子均为黑色
if (brother->leftChild->color == RB_Black
&& brother->rightChild->color == RB_Black) {
brother->color = RB_Red;
node = parent;
}
else {
// case 3: 兄弟节点的左孩子为红色,右孩子为黑色
if (brother->rightChild->color == RB_Black) {
brother->leftChild->color = RB_Black;
brother->color = RB_Red;
RBTree_left_rotate(tree, brother);
brother = parent->rightChild;
}
// case 4:兄弟节点的右孩子为红色
brother->color = parent->color;
parent->color = RB_Black;
brother->leftChild->color = RB_Black;
RBTree_right_rotate(tree, parent);
node = *tree;
}
}
}
node->color = RB_Black;
}
// 删除
RBNode* RBTree_delete(RBTree* tree, RBNode* node)
{
RBNode* successor = NULL;
RBNode* temp = NULL;
// 确定后继结点
if (node->leftChild == &RBNode_Nil || node->rightChild == &RBNode_Nil) {
successor = node;
}
else {
successor = RBTree_successor(node);
}
if (successor->leftChild != &RBNode_Nil) {
temp = successor->leftChild;
}
else {
temp = successor->rightChild;
}
// 用后继替换节点,然后删除后继结点
temp->parent = successor->parent;
if (successor->parent == &RBNode_Nil) {
*tree = temp;
}
else {
if (successor == successor->parent->leftChild) {
successor->parent->leftChild = temp;
}
else {
successor->parent->rightChild = temp;
}
}
if (successor != node) {
node->key = successor->key;
}
// 如果删除的后继节点是黑色的,则不满足红黑树性质,需要调整
if (successor->color == RB_Black) {
RBTree_delete_fixup(tree, temp);
}
return successor;
}