-
Notifications
You must be signed in to change notification settings - Fork 2
/
red-black-tree.ts
615 lines (520 loc) · 18 KB
/
red-black-tree.ts
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import Stack from "./Stack";
// RED BLACK TREES
/*
PROPERTIES:
1. Every node is either red or black.
2. The root is black
3. Every leaf (NIL) is black.
4. If a node is red, then both its children are black.
5. For each node, all paths from the node to descendant leaves contain the same number of black nodes.
*/
class RedBlackTreeNode<T> {
public left: RedBlackTreeNode<T>;
public right: RedBlackTreeNode<T>;
public parent: RedBlackTreeNode<T>;
public value: T;
public color: 'red' | 'black';
constructor(nill: RedBlackTreeNode<null>) {
this.left = nill;
this.right = nill;
this.parent = null;
this.color = 'black';
}
}
const nill: RedBlackTreeNode<null> = new RedBlackTreeNode(null)
class RedBlackTree<T> {
// private nill: RedBlackTreeNode<null> = new RedBlackTreeNode(null);
private nill: RedBlackTreeNode<null> = nill;
private root: RedBlackTreeNode<T>;
private blackHeight: number;
constructor() {
this.root = this.nill;
this.blackHeight = 0;
}
// comment this
getter() {
return this.root;
}
/**
* ROTATION ON THE TREE NODE
* Time Complexity: O(1)
*
*
* | |
* Y right-rotate X
* / \ ---------------->>>>> / \
* X a <<<<<---------------- b Y
* / \ left-rotate / \
* b c c a
*
*
*
*/
private rotateTree(direction: 'left' | 'right', node: RedBlackTreeNode<T>) {
if (direction === 'left') {
this.leftRotate(node);
} else if (direction === 'right') {
this.rightRotate(node);
}
}
private leftRotate(x: RedBlackTreeNode<T>) {
//Set y
const y = x.right;
//Turn y’s left subtree into x’s right subtree.
x.right = y.left;
if (y.left !== this.nill) {
y.left.parent = x;
}
// Link x’s parent to y.
y.parent = x.parent;
if (x.parent === this.nill) {
this.root = y;
} else if (x.parent.left === x) {
x.parent.left = y
} else {
x.parent.right = y
}
//Put x on y’s left
y.left = x;
x.parent = y;
}
private rightRotate(y: RedBlackTreeNode<T>) {
//Set x
const x = y.left;
//Turn x’s right subtree into y’s left subtree.
y.left = x.right;
if (x.right !== this.nill) {
x.right.parent = y;
}
// Link y’s parent to x.
x.parent = y.parent;
if (y.parent === this.nill) {
this.root = x;
} else if (y.parent.left === y) {
y.parent.left = x;
} else {
y.parent.right = x
}
//Put y on x’s right
x.right = y;
y.parent = x;
}
/**
* @description:inserts node to the red black tree just like insertion in binary tree and then insertion_fixup is called
* @dependency: insertion_fixup
*
* @param {RedBlackTreeNode<T>} newNode
* @memberof RedBlackTree
*/
private insertNode(newNode: RedBlackTreeNode<T>) {
let insertionPointer: RedBlackTreeNode<T> = this.nill;
let traversalPointer: RedBlackTreeNode<T> = this.root;
while (traversalPointer !== this.nill) {
insertionPointer = traversalPointer;
if (newNode.value < traversalPointer.value) {
traversalPointer = traversalPointer.left;
} else {
traversalPointer = traversalPointer.right;
}
}
newNode.parent = insertionPointer;
if (insertionPointer === this.nill) {
this.root = newNode;
} else if (newNode.value < insertionPointer.value) {
insertionPointer.left = newNode;
} else {
insertionPointer.right = newNode;
}
newNode.left = this.nill;
newNode.right = this.nill;
newNode.color = 'red';
this.insertion_fixup(newNode)
}
/**
*fixes the violation of rule 2,4 caused by inserting a node
*
* @param {RedBlackTreeNode<T>} node
* @memberof RedBlackTree
*/
private insertion_fixup(node: RedBlackTreeNode<T>) {
while (node.parent && node.parent.color === 'red') {
if (node.parent == node.parent.parent.left) {
const uncle = node.parent.parent.right;
if (uncle && uncle.color === 'red') {
node.parent.color = "black";
uncle.color = "black";
node.parent.parent.color = 'red';
node = node.parent.parent
} else if (node == node.parent.right) {
node = node.parent;
this.rotateTree("left", node);
} else {
node.parent.color = 'black';
node.parent.parent.color = 'red';
this.rotateTree('right', node.parent.parent);
}
} else {
const uncle = node.parent.parent.left;
if (uncle && uncle.color === 'red') {
node.parent.color = "black";
uncle.color = "black";
node.parent.parent.color = 'red';
node = node.parent.parent
} else if (node == node.parent.left) {
node = node.parent;
this.rotateTree("right", node);
} else {
node.parent.color = 'black';
node.parent.parent.color = 'red';
this.rotateTree('left', node.parent.parent);
}
}
}
if (this.root.color === 'red') {
this.blackHeight = this.blackHeight + 1;
}
this.root.color = 'black'
}
/**
* @description inserts values into red black tree
* Time Complexity: O(lg(n))
*
* @param {T} value
* @memberof RedBlackTree
*/
insert(value: T) {
const newNode: RedBlackTreeNode<T> = new RedBlackTreeNode<T>(this.nill);
newNode.value = value;
this.insertNode(newNode);
}
/**
*@description returns next element value of the given element
*
* @param {RedBlackTreeNode<T>} z
* @returns {RedBlackTreeNode<T>}
* @memberof RedBlackTree
*/
private successor(z: RedBlackTreeNode<T>): RedBlackTreeNode<T> {
if (z === this.nill) {
return z
}
return this.minimum(z.right)
}
/**
*returns successor of given value i.e., next biggest value,
*returns null if it doesn't have successor or given value doesn't exist or there are no elements in tree
*
* @param {T} value
* @returns
* @memberof RedBlackTree
*/
public successorValue(value: T) {
const node = this.findNode(value);
return this.successor(node).value;
}
private minimum(root: RedBlackTreeNode<T>): RedBlackTreeNode<T> {
while (root !== this.nill && root.left !== this.nill) {
root = root.left;
}
return root
}
/**
*return minimum value of the tree, returns null if there are no elements in tree
*
* @returns
* @memberof RedBlackTree
*/
public minValue() {
return this.minimum(this.root).value
}
/**
*finds if a value is present in the tree
*
* @param {T} value
* @returns {boolean}
* @memberof RedBlackTree
*/
public find(value: T): boolean {
const node = this.findNode(value);
if (node !== this.nill) {
return true
} else {
return false
}
}
private findNode(value: T): RedBlackTreeNode<T> {
let traverser = this.root;
while (traverser !== this.nill && traverser.value !== value) {
if (value < traverser.value) {
traverser = traverser.left
} else {
traverser = traverser.right;
}
}
return traverser;
}
private deleteNode(z: RedBlackTreeNode<T>) {
// node y is either either removed from the tree or moved within the tree
let y: RedBlackTreeNode<T>
// node x that moves into y's original position
let x: RedBlackTreeNode<T>;
// assignment of y
if (z.left === this.nill || z.right === this.nill) {
y = z;
} else {
y = this.successor(z);
}
// assignment of x
if (y.left !== this.nill) {
x = y.left;
} else {
x = y.right;
}
// move x to y's place
x.parent = y.parent;
if (y.parent === this.nill) {
this.root = x;
} else if (y === y.parent.left) {
y.parent.left = x;
} else {
y.parent.right = x;
}
if (y !== z) {
z.value = y.value
}
if (y.color === 'black') {
this.deleteFixup(x)
}
}
private deleteFixup(arbitraryNode: RedBlackTreeNode<T>) {
let sibling: RedBlackTreeNode<T>;
while (arbitraryNode !== this.root && arbitraryNode.color === 'black') {
if (arbitraryNode === arbitraryNode.parent.left) {
sibling = arbitraryNode.parent.right;
//case 1
if (sibling.color === 'red') {
sibling.color = 'black';
arbitraryNode.parent.color = 'red';
this.rotateTree('left', arbitraryNode.parent);
sibling = arbitraryNode.parent.right;
}
if (sibling.color === 'black') {
//case 2
if (sibling.left.color === 'black' && sibling.right.color === 'black') {
sibling.color = 'red';
arbitraryNode.parent.color = 'black';
arbitraryNode = arbitraryNode.parent;
}
//case 3
else if (sibling.right.color === 'black') {
sibling.left.color = 'black';
sibling.color = 'red';
this.rotateTree('right', sibling);
}
//case 4
else {
sibling.color = arbitraryNode.parent.color;
arbitraryNode.parent.color = 'black';
sibling.right.color = 'black';
this.rotateTree('left', arbitraryNode.parent);
arbitraryNode = this.root;
}
}
} else {
sibling = arbitraryNode.parent.left;
//case 1
if (sibling.color === 'red') {
sibling.color = 'black';
arbitraryNode.parent.color = 'red';
this.rotateTree('right', arbitraryNode.parent);
sibling = arbitraryNode.parent.left;
}
if (sibling.color === 'black') {
//case 2
if (sibling.right.color === 'black' && sibling.left.color === 'black') {
sibling.color = 'red';
arbitraryNode.parent.color = 'black';
arbitraryNode = arbitraryNode.parent;
}
//case 3
else if (sibling.left.color === 'black') {
sibling.right.color = 'black';
sibling.color = 'red';
this.rotateTree('left', sibling);
}
//case 4
else {
sibling.color = arbitraryNode.parent.color;
arbitraryNode.parent.color = 'black';
sibling.left.color = 'black';
this.rotateTree('right', arbitraryNode.parent);
arbitraryNode = this.root;
}
}
}
}
if (arbitraryNode === this.root) {
this.blackHeight = this.blackHeight - 1
}
arbitraryNode.color = 'black';
}
/**
*deletes a value from red black tree
*
* @param {T} value
* @memberof RedBlackTree
*/
public delete(value: T) {
const node = this.findNode(value)
if (node === this.nill) {
throw `no value with -(${value}) present in tree to delete`;
} else {
this.deleteNode(node);
}
}
// traversals
public inOrder() {
const stack = new Stack<RedBlackTreeNode<T>>();
const tree: T[] = [];
let traverser = this.root;
while (traverser !== this.nill || !stack.isEmpty()) {
if (traverser === this.nill) {
const parent = stack.pop();
tree.push(parent.value);
traverser = parent.right;
continue;
}
stack.push(traverser);
traverser = traverser.left;
}
return tree;
}
public preOrder() {
const stack = new Stack<RedBlackTreeNode<T>>();
const tree: T[] = [];
let traverser = this.root;
while (traverser !== this.nill || !stack.isEmpty()) {
if (traverser === this.nill) {
const parent = stack.pop();
traverser = parent.right;
continue;
}
tree.push(traverser.value);
stack.push(traverser);
traverser = traverser.left;
}
return tree;
}
public postOrder() {
const stack = new Stack<RedBlackTreeNode<T>>();
const tree: T[] = [];
let traverser = this.root;
while (traverser !== this.nill || !stack.isEmpty()) {
if (traverser === this.nill) {
const node = stack.pop();
if (node.right === stack.pop()) {
traverser = node.right;
stack.push(node);
continue;
}
stack.revert();
tree.push(node.value);
continue;
}
if (traverser.right !== this.nill) {
stack.push(traverser.right);
}
stack.push(traverser);
traverser = traverser.left;
}
}
getSubTreeByBlackHeight(tree: RedBlackTree<T>, blackHeight: number) {
let subTreeBlackHeight: number = tree.blackHeight;
let subTree: RedBlackTreeNode<T> = tree.root;
while (subTreeBlackHeight > blackHeight) {
if (subTree.right) {
subTree = subTree.right;
} else {
subTree = subTree.left;
}
if (subTree.color === 'black') {
subTreeBlackHeight = subTreeBlackHeight - 1;
}
}
return subTree;
}
/**
*replaces u with v in the given tree
*
* @private
* @param {RedBlackTree<T>} tree
* @param {RedBlackTreeNode<T>} u
* @param {RedBlackTreeNode<T>} v
* @memberof RedBlackTree
*/
private RB_Transplant(tree: RedBlackTree<T>, u: RedBlackTreeNode<T>, v: RedBlackTreeNode<T>) {
if (u.parent === tree.nill) {
tree.root = v;
} else if (u === u.parent.left) {
u.parent.left = v;
} else {
u.parent.right = v;
}
v.parent = u.parent;
}
/**
*joins second tree into first tree with the merge node
*
* @private
* @param {RedBlackTree<T>} tree
* @param {RedBlackTreeNode<T>} firstTreeRoot
* @param {RedBlackTreeNode<T>} mergeNode
* @param {RedBlackTreeNode<T>} secondTreeRoot
* @memberof RedBlackTree
*/
private RB_Join(tree: RedBlackTree<T>, firstTreeRoot: RedBlackTreeNode<T>, mergeNode: RedBlackTreeNode<T>, secondTreeRoot: RedBlackTreeNode<T>) {
this.RB_Transplant(tree, firstTreeRoot, mergeNode);
if (firstTreeRoot.value < secondTreeRoot.value) {
mergeNode.left = firstTreeRoot;
mergeNode.right = secondTreeRoot;
} else {
mergeNode.right = firstTreeRoot;
mergeNode.left = secondTreeRoot;
}
firstTreeRoot.parent = mergeNode;
secondTreeRoot.parent = mergeNode;
}
/**
*only gives correct results if new tree is completely has larger value items than present tree
* addn info: The join operation takes two dynamic sets S1 and S2
* and an element x such that for any x1 ∈ S1 and x2 ∈ S2, we havekey[x1] ≤ key[x] ≤ key[x2].
* It returns a set S = S1 ∪{x}∪ S2. In this problem, we investigate how to implement the join operation on red-black trees.
*
* TODO: should accept any tree to merge into existing
*
* @param {RedBlackTree<T>} newTree
* @memberof RedBlackTree
*/
public mergeTree(newTree: RedBlackTree<T>) {
let smallTree: RedBlackTree<T>;
let bigTree: RedBlackTree<T>;
if (this.blackHeight < newTree.blackHeight) {
smallTree = this;
bigTree = newTree;
} else {
smallTree = newTree;
bigTree = this;
}
// smallTree.nill = this.nill;
// bigTree.nill = this.nill;
const mergeNode = new RedBlackTreeNode<T>(smallTree.nill);
mergeNode.color = 'red';
mergeNode.value = smallTree.root.value;
this.RB_Join(bigTree, this.getSubTreeByBlackHeight(bigTree, smallTree.blackHeight), mergeNode, smallTree.root);
this.root = bigTree.root;
// this.nill = bigTree.nill;
this.blackHeight = bigTree.blackHeight;
this.insertion_fixup(mergeNode);
this.deleteNode(mergeNode);
}
}