forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathred-black-tree.ts
180 lines (162 loc) · 4.76 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
import { defaultCompare, ICompareFunction, Compare } from '../util';
import BinarySearchTree from './binary-search-tree';
import { RedBlackNode, Colors } from './models/red-black-node';
export default class RedBlackTree<T> extends BinarySearchTree<T> {
protected root: RedBlackNode<T>;
constructor(protected compareFn: ICompareFunction<T> = defaultCompare) {
super(compareFn);
}
/**
* Left left case: rotate right
*
* b a
* / \ / \
* a e -> rotationLL(b) -> c b
* / \ / \
* c d d e
*
* @param node Node<T>
*/
private rotationLL(node: RedBlackNode<T>) {
const tmp = node.left;
node.left = tmp.right;
if (tmp.right && tmp.right.key) {
tmp.right.parent = node;
}
tmp.parent = node.parent;
if (!node.parent) {
this.root = tmp;
} else {
if (node === node.parent.left) {
node.parent.left = tmp;
} else {
node.parent.right = tmp;
}
}
tmp.right = node;
node.parent = tmp;
}
/**
* Right right case: rotate left
*
* a b
* / \ / \
* c b -> rotationRR(a) -> a e
* / \ / \
* d e c d
*
* @param node Node<T>
*/
private rotationRR(node: RedBlackNode<T>) {
const tmp = node.right;
node.right = tmp.left;
if (tmp.left && tmp.left.key) {
tmp.left.parent = node;
}
tmp.parent = node.parent;
if (!node.parent) {
this.root = tmp;
} else {
if (node === node.parent.left) {
node.parent.left = tmp;
} else {
node.parent.right = tmp;
}
}
tmp.left = node;
node.parent = tmp;
}
insert(key: T) {
// special case: first key
if (this.root == null) {
this.root = new RedBlackNode(key);
this.root.color = Colors.BLACK;
} else {
const newNode = this.insertNode(this.root, key);
this.fixTreeProperties(newNode);
}
}
protected insertNode(node: RedBlackNode<T>, key: T): RedBlackNode<T> {
if (this.compareFn(key, node.key) === Compare.LESS_THAN) {
if (node.left == null) {
node.left = new RedBlackNode(key);
node.left.parent = node;
return node.left;
} else {
return this.insertNode(node.left, key);
}
} else if (node.right == null) {
node.right = new RedBlackNode(key);
node.right.parent = node;
return node.right;
} else {
return this.insertNode(node.right, key);
}
}
private fixTreeProperties(node: RedBlackNode<T>) {
while (node && node.parent && node.parent.color === Colors.RED && node.color !== Colors.BLACK) {
let parent = node.parent;
const grandParent = parent.parent;
// case A
if (grandParent && grandParent.left === parent) {
const uncle = grandParent.right;
// case 1: uncle of node is also red - only recoloring
if (uncle && uncle.isRed()) {
grandParent.color = Colors.RED;
parent.color = Colors.BLACK;
uncle.color = Colors.BLACK;
node = grandParent;
} else {
// case 2: node is right child - left rotate
if (node === parent.right) {
this.rotationRR(parent);
node = parent;
parent = node.parent;
}
// case 3: node is left child - right rotate
this.rotationLL(grandParent);
// swap color
parent.color = Colors.BLACK;
grandParent.color = Colors.RED;
node = parent;
}
} else { // case B: parent is right child of grand parent
const uncle = grandParent.left;
// case 1: uncle is read - only recoloring
if (uncle && uncle.isRed()) {
grandParent.color = Colors.RED;
parent.color = Colors.BLACK;
uncle.color = Colors.BLACK;
node = grandParent;
} else {
// case 2: node is left child - left rotate
if (node === parent.left) {
this.rotationLL(parent);
node = parent;
parent = node.parent;
}
// case 3: node is right child - left rotate
this.rotationRR(grandParent);
// swap color
parent.color = Colors.BLACK;
grandParent.color = Colors.RED;
node = parent;
}
}
}
this.root.color = Colors.BLACK;
}
getRoot() {
return this.root;
}
/* private flipColors(node: RedBlackNode<T>) {
node.left.flipColor();
node.right.flipColor();
}
private isRed(node: RedBlackNode<T>) {
if (!node) {
return false;
}
return node.isRed();
}*/
}