-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRedBlackTree.cs
More file actions
538 lines (422 loc) · 13.9 KB
/
RedBlackTree.cs
File metadata and controls
538 lines (422 loc) · 13.9 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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
using System;
namespace AlgorithmsAndDataStructures.DataStructures.RbTree;
public class RedBlackTree
{
#region Fields
private RedBlackTreeNode root;
#endregion
#region Properties
public int Height => Math.Max(HeightInternal(root) - 1, 0);
#endregion
#region Constructors
public RedBlackTree(RedBlackTreeNode root)
{
this.root = root;
}
public RedBlackTree()
{
}
#endregion
#region Insert
public void Insert(int value)
{
var newNode = new RedBlackTreeNode
{
IsRed = true,
Value = value
};
if (root == null)
{
root = newNode;
root.IsRed = false;
return;
}
InsertInternal(root, newNode);
CheckInsert(newNode);
}
private void InsertInternal(RedBlackTreeNode rootNode, RedBlackTreeNode toInsert)
{
if (toInsert.Value > rootNode.Value)
{
if (rootNode.Right.IsLeafNode)
{
rootNode.Right = toInsert;
toInsert.Parent = rootNode;
return;
}
InsertInternal(rootNode.Right, toInsert);
}
else if (toInsert.Value < rootNode.Value)
{
if (rootNode.Left.IsLeafNode)
{
rootNode.Left = toInsert;
toInsert.Parent = rootNode;
return;
}
InsertInternal(rootNode.Left, toInsert);
}
}
private void CheckInsert(RedBlackTreeNode node)
{
if (node == root || node?.Parent == null) return;
//Check if there are two consequitive red nodes in a tree
if (node.IsRed && node.Parent.IsRed) CorrectInsert(node);
CheckInsert(node.Parent);
}
private void CorrectInsert(RedBlackTreeNode node)
{
if (IsUncleRed(node))
{
node.Parent.Parent.Left.IsRed = false;
node.Parent.Parent.Right.IsRed = false;
if (node.Parent.Parent != root) node.Parent.Parent.IsRed = true;
return;
}
if (IsUncleBlack(node)) Rotate(node);
}
private static bool IsUncleRed(RedBlackTreeNode node)
{
var grandparent = node.Parent.Parent;
if (grandparent == null) return false;
if (node.Parent.IsLeft) return grandparent.Right.IsRed;
return grandparent.Left.IsRed;
}
private static bool IsUncleBlack(RedBlackTreeNode node)
{
var grandparent = node.Parent.Parent;
if (grandparent == null) return false;
return node.Parent.IsLeft ? grandparent.Right.IsBlack : grandparent.Left.IsBlack;
}
private void Rotate(RedBlackTreeNode node)
{
if (node.IsLeft && node.Parent.IsLeft)
{
RotateRight(node.Parent.Parent);
node.IsRed = true;
node.Parent.IsRed = false;
if (!node.Parent.Right.IsLeafNode)
node.Parent.Right.IsRed = true;
}
else if (node.IsRight && node.Parent.IsRight)
{
RotateLeft(node.Parent.Parent);
node.IsRed = true;
node.Parent.IsRed = false;
if (!node.Parent.Left.IsLeafNode)
node.Parent.Left.IsRed = true;
}
else if (node.IsRight && node.Parent.IsLeft)
{
RotateLeftRight(node.Parent.Parent);
node.IsRed = false;
node.Right.IsRed = true;
node.Left.IsRed = true;
}
else if (node.IsLeft && node.Parent.IsRight)
{
RotateRightLeft(node.Parent.Parent);
node.IsRed = false;
node.Left.IsRed = true;
node.Right.IsRed = true;
}
}
#endregion
#region Delete
public void Delete(int value)
{
root = DeleteInternal(root, value, out var doubleBlackNode);
if (doubleBlackNode != null) FixDelete(doubleBlackNode);
}
private RedBlackTreeNode DeleteInternal(RedBlackTreeNode node, int value, out RedBlackTreeNode doubleBlackNode)
{
if (node.IsLeafNode)
{
doubleBlackNode = null;
return node;
}
if (node.Value > value)
{
node.Left = DeleteInternal(node.Left, value, out doubleBlackNode);
}
else if (node.Value < value)
{
node.Right = DeleteInternal(node.Right, value, out doubleBlackNode);
}
else
{
// Leaf Node.
if (node.Left.IsLeafNode && node.Right.IsLeafNode)
{
if (node.IsRed)
{
doubleBlackNode = null;
return null;
}
doubleBlackNode = node.Left;
doubleBlackNode.Parent = node.Parent;
return doubleBlackNode;
}
// One Children.
if (node.Left.IsLeafNode || node.Right.IsLeafNode)
{
var nonNullChild = node.Left.IsLeafNode ? node.Right : node.Left;
if (node.IsRed)
{
nonNullChild.Parent = node.Parent;
doubleBlackNode = null;
return nonNullChild;
}
if (nonNullChild.IsRed)
{
nonNullChild.IsRed = false;
doubleBlackNode = null;
nonNullChild.Parent = node.Parent;
return nonNullChild;
}
doubleBlackNode = nonNullChild;
doubleBlackNode.Parent = node.Parent;
return doubleBlackNode;
}
// Two children. Convert to one children case.
var minNode = FindInOrderSuccessor(node.Right);
node.Value = minNode.Value;
node.Right = DeleteInternal(node.Right, minNode.Value, out doubleBlackNode);
}
return node;
}
private static RedBlackTreeNode FindInOrderSuccessor(RedBlackTreeNode right)
{
var current = right;
while (!current.Left.IsLeafNode) current = current.Left;
return current;
}
private void FixDelete(RedBlackTreeNode node)
{
if (node == root)
{
node.IsRed = false;
return;
}
if (IsCase2(node)) FixCase2(node);
if (IsCase3(node))
{
FixCase3(node);
FixDelete(node.Parent);
return;
}
if (IsCase4(node))
{
FixCase4(node);
return;
}
if (IsCase5(node))
{
FixCase5(node);
return;
}
if (IsCase6(node)) FixCase6(node);
}
#endregion
#region Check Delete Cases
private static bool IsCase2(RedBlackTreeNode node)
{
var parent = node.Parent;
var sibling = node.IsLeft ? parent.Right : parent.Left;
var isSiblingChildrenBlack = (sibling.Left.IsLeafNode || sibling.Left.IsBlack) &&
(sibling.Right.IsLeafNode || sibling.Right.IsBlack);
return parent.IsBlack && sibling.IsRed && isSiblingChildrenBlack;
}
private static bool IsCase3(RedBlackTreeNode node)
{
var parent = node.Parent;
var sibling = node.IsLeft ? parent.Right : parent.Left;
var isSiblingChildrenBlack = (sibling.Left.IsLeafNode || sibling.Left.IsBlack) &&
(sibling.Right.IsLeafNode || sibling.Right.IsBlack);
return node.Parent.IsBlack && sibling.IsBlack && isSiblingChildrenBlack;
}
private static bool IsCase4(RedBlackTreeNode node)
{
var parent = node.Parent;
var sibling = node.IsLeft ? parent.Right : parent.Left;
var isSiblingChildrenBlack = (sibling.Left.IsLeafNode || sibling.Left.IsBlack) &&
(sibling.Right.IsLeafNode || sibling.Right.IsBlack);
return node.Parent.IsRed && sibling.IsBlack && isSiblingChildrenBlack;
}
private static bool IsCase5(RedBlackTreeNode node)
{
var parent = node.Parent;
var sibling = node.IsLeft ? parent.Right : parent.Left;
var isSiblingChildrenRedBlack = node.IsLeft
? sibling.Left.IsRed && (sibling.Right.IsLeafNode || sibling.Right.IsBlack)
: (sibling.Left.IsLeafNode || sibling.Left.IsBlack) && sibling.Right.IsRed;
return sibling.IsBlack && isSiblingChildrenRedBlack;
}
private static bool IsCase6(RedBlackTreeNode node)
{
var parent = node.Parent;
var sibling = node.IsLeft ? parent.Right : parent.Left;
var isSiblingRightChildRed = node.IsLeft
? sibling.Right.IsRed
: sibling.Left.IsRed;
return sibling.IsBlack && isSiblingRightChildRed;
}
#endregion
#region Fix Delete Cases
private void FixCase2(RedBlackTreeNode node)
{
var parent = node.Parent;
var originalParentColor = node.Parent.IsRed;
var sibling = node.IsLeft ? node.Parent.Right : node.Parent.Left;
var originalSiblingColor = sibling.IsRed;
if (node.IsLeft)
RotateLeft(node.Parent);
else
RotateRight(node.Parent);
parent.IsRed = originalSiblingColor;
sibling.IsRed = originalParentColor;
}
private static void FixCase3(RedBlackTreeNode node)
{
var parent = node.Parent;
var sibling = node.IsLeft ? parent.Right : parent.Left;
sibling.IsRed = true;
}
private static void FixCase4(RedBlackTreeNode node)
{
node.Parent.IsRed = false;
if (node.IsLeft)
node.Parent.Right.IsRed = true;
else
node.Parent.Left.IsRed = true;
}
private void FixCase5(RedBlackTreeNode node)
{
var parent = node.Parent;
if (node.IsLeft)
{
RotateRight(parent.Right);
if (parent.Right.Left != null) parent.Right.Left.IsRed = false;
parent.Right.IsRed = true;
}
else
{
RotateLeft(parent.Left);
if (parent.Left.Right != null) parent.Left.Right.IsRed = false;
parent.Left.IsRed = true;
}
FixCase6(node);
}
private void FixCase6(RedBlackTreeNode node)
{
if (node.IsLeft)
{
var originalColor = node.Parent.IsRed;
var rightSibling = node.Parent.Right;
RotateLeft(node.Parent);
rightSibling.IsRed = originalColor;
rightSibling.Right.IsRed = false;
rightSibling.Left.IsRed = false;
}
else
{
var originalColor = node.Parent.IsRed;
var leftSibling = node.Parent.Left;
RotateRight(node.Parent);
leftSibling.IsRed = originalColor;
leftSibling.Right.IsRed = false;
leftSibling.Left.IsRed = false;
}
}
#endregion
#region Rotations
private void RotateRightLeft(RedBlackTreeNode node)
{
RotateRight(node.Right);
RotateLeft(node);
}
private void RotateLeftRight(RedBlackTreeNode node)
{
RotateLeft(node.Left);
RotateRight(node);
}
private void RotateRight(RedBlackTreeNode node)
{
var parent = node.Parent;
var originalNodePosition = node.IsRight;
var leftChild = node.Left;
node.Left = leftChild.Right;
if (node.Left != null) node.Left.Parent = node;
leftChild.Right = node;
node.Parent = leftChild;
if (parent == null)
{
root = leftChild;
root.Parent = null;
return;
}
leftChild.Parent = parent;
if (originalNodePosition)
parent.Right = leftChild;
else
parent.Left = leftChild;
}
private void RotateLeft(RedBlackTreeNode node)
{
var parent = node.Parent;
var originalPositionIsRight = node.IsRight;
var rightChild = node.Right;
node.Right = rightChild.Left;
if (node.Right != null)
node.Right.Parent = node;
rightChild.Left = node;
node.Parent = rightChild;
if (parent == null)
{
root = rightChild;
root.Parent = null;
return;
}
rightChild.Parent = parent;
if (originalPositionIsRight)
parent.Right = rightChild;
else
parent.Left = rightChild;
}
#endregion
#region Tree Validation
public void CheckTreeValidity()
{
if (root == null) return;
if (root.IsRed) throw new Exception("Root is red.");
CheckBlackNodesBalance(root);
CheckNoConsequentRedNodes(root);
}
private static void CheckNoConsequentRedNodes(RedBlackTreeNode node)
{
if (node.IsLeafNode) return;
if (node.IsRed && (node.Left.IsRed || node.Right.IsRed))
{
#pragma warning disable HAA0601 // Value type to reference type conversion causing boxing allocation
throw new Exception(
$"Consequent red nodes. Node value: {node.Value}. Left: {node.Left.Value}. Right {node.Right.Value}");
#pragma warning restore HAA0601 // Value type to reference type conversion causing boxing allocation
}
}
private int CheckBlackNodesBalance(RedBlackTreeNode node)
{
if (node == null || node.IsLeafNode) return 1;
var leftHeight = CheckBlackNodesBalance(node.Left);
var rightHeight = CheckBlackNodesBalance(node.Right);
if (leftHeight != rightHeight) throw new Exception("Tree is unbalanced.");
return node.IsRed ? leftHeight : leftHeight + 1;
}
private int HeightInternal(RedBlackTreeNode node)
{
if (node == null || node.IsLeafNode) return 0;
var leftHeight = HeightInternal(node.Left) + 1;
var rightHeight = HeightInternal(node.Right) + 1;
return Math.Max(leftHeight, rightHeight);
}
#endregion
}