min-heap-typed 1.40.0 → 1.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/binary-tree.d.ts +8 -0
- package/dist/data-structures/binary-tree/binary-tree.js +22 -2
- package/dist/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/data-structures/binary-tree/bst.js +1 -1
- package/dist/data-structures/binary-tree/rb-tree.d.ts +95 -9
- package/dist/data-structures/binary-tree/rb-tree.js +379 -12
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -7
- package/dist/types/data-structures/binary-tree/rb-tree.js +11 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +23 -1
- package/src/data-structures/binary-tree/bst.ts +4 -4
- package/src/data-structures/binary-tree/rb-tree.ts +418 -350
- package/src/types/data-structures/binary-tree/rb-tree.ts +6 -6
|
@@ -1,358 +1,426 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {IBinaryTree} from '../../interfaces';
|
|
3
|
-
import {BST, BSTNode} from './bst';
|
|
4
|
-
|
|
5
|
-
export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
6
|
-
constructor(key: BTNKey, value?: V) {
|
|
7
|
-
super(key, value);
|
|
8
|
-
this.color = RBColor.RED;
|
|
9
|
-
}
|
|
1
|
+
import {RBTNColor} from "../../types";
|
|
10
2
|
|
|
11
|
-
|
|
3
|
+
export class RBTreeNode {
|
|
4
|
+
key: number = 0;
|
|
5
|
+
parent: RBTreeNode;
|
|
6
|
+
left: RBTreeNode;
|
|
7
|
+
right: RBTreeNode;
|
|
8
|
+
color: number = RBTNColor.BLACK;
|
|
12
9
|
|
|
10
|
+
constructor() {
|
|
11
|
+
this.parent = null as unknown as RBTreeNode;
|
|
12
|
+
this.left = null as unknown as RBTreeNode;
|
|
13
|
+
this.right = null as unknown as RBTreeNode;
|
|
14
|
+
}
|
|
13
15
|
}
|
|
14
16
|
|
|
15
|
-
export class
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
export class RedBlackTree {
|
|
18
|
+
|
|
19
|
+
constructor() {
|
|
20
|
+
this._NIL = new RBTreeNode();
|
|
21
|
+
this.NIL.color = RBTNColor.BLACK;
|
|
22
|
+
this.NIL.left = null as unknown as RBTreeNode;
|
|
23
|
+
this.NIL.right = null as unknown as RBTreeNode;
|
|
24
|
+
this._root = this.NIL;
|
|
20
25
|
}
|
|
21
26
|
|
|
22
|
-
|
|
23
|
-
|
|
27
|
+
protected _root: RBTreeNode;
|
|
28
|
+
|
|
29
|
+
get root(): RBTreeNode {
|
|
30
|
+
return this._root;
|
|
24
31
|
}
|
|
25
32
|
|
|
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
|
-
|
|
33
|
+
protected _NIL: RBTreeNode;
|
|
34
|
+
|
|
35
|
+
get NIL(): RBTreeNode {
|
|
36
|
+
return this._NIL;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The `insert` function inserts a new node with a given key into a red-black tree and fixes any
|
|
41
|
+
* violations of the red-black tree properties.
|
|
42
|
+
* @param {number} key - The key parameter is a number that represents the value to be inserted into
|
|
43
|
+
* the RBTree.
|
|
44
|
+
* @returns The function does not explicitly return anything.
|
|
45
|
+
*/
|
|
46
|
+
insert(key: number): void {
|
|
47
|
+
|
|
48
|
+
const node: RBTreeNode = new RBTreeNode();
|
|
49
|
+
node.parent = null as unknown as RBTreeNode;
|
|
50
|
+
node.key = key;
|
|
51
|
+
node.left = this.NIL;
|
|
52
|
+
node.right = this.NIL;
|
|
53
|
+
node.color = RBTNColor.RED;
|
|
54
|
+
|
|
55
|
+
let y: RBTreeNode = null as unknown as RBTreeNode;
|
|
56
|
+
let x: RBTreeNode = this.root;
|
|
57
|
+
|
|
58
|
+
while (x !== this.NIL) {
|
|
59
|
+
y = x;
|
|
60
|
+
if (node.key < x.key) {
|
|
61
|
+
x = x.left;
|
|
62
|
+
} else {
|
|
63
|
+
x = x.right;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
node.parent = y;
|
|
68
|
+
if (y === null) {
|
|
69
|
+
this._root = node;
|
|
70
|
+
} else if (node.key < y.key) {
|
|
71
|
+
y.left = node;
|
|
72
|
+
} else {
|
|
73
|
+
y.right = node;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (node.parent === null) {
|
|
77
|
+
node.color = RBTNColor.BLACK;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (node.parent.parent === null) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this._fixInsert(node);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
|
|
90
|
+
* tree.
|
|
91
|
+
* @param {RBTreeNode} node - The `node` parameter is of type `RBTreeNode` and represents the current
|
|
92
|
+
* node being processed in the delete operation.
|
|
93
|
+
* @returns The `delete` function does not return anything. It has a return type of `void`.
|
|
94
|
+
*/
|
|
95
|
+
delete(key: number): void {
|
|
96
|
+
const helper = (node: RBTreeNode): void => {
|
|
97
|
+
|
|
98
|
+
let z: RBTreeNode = this.NIL;
|
|
99
|
+
let x: RBTreeNode, y: RBTreeNode;
|
|
100
|
+
while (node !== this.NIL) {
|
|
101
|
+
if (node.key === key) {
|
|
102
|
+
z = node;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (node.key <= key) {
|
|
106
|
+
node = node.right;
|
|
107
|
+
} else {
|
|
108
|
+
node = node.left;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (z === this.NIL) {
|
|
113
|
+
console.log("Couldn't find key in the tree");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
y = z;
|
|
118
|
+
let yOriginalColor: number = y.color;
|
|
119
|
+
if (z.left === this.NIL) {
|
|
120
|
+
x = z.right;
|
|
121
|
+
this._rbTransplant(z, z.right);
|
|
122
|
+
} else if (z.right === this.NIL) {
|
|
123
|
+
x = z.left;
|
|
124
|
+
this._rbTransplant(z, z.left);
|
|
125
|
+
} else {
|
|
126
|
+
y = this.getLeftMost(z.right);
|
|
127
|
+
yOriginalColor = y.color;
|
|
128
|
+
x = y.right;
|
|
129
|
+
if (y.parent === z) {
|
|
130
|
+
x.parent = y;
|
|
131
|
+
} else {
|
|
132
|
+
this._rbTransplant(y, y.right);
|
|
133
|
+
y.right = z.right;
|
|
134
|
+
y.right.parent = y;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
this._rbTransplant(z, y);
|
|
138
|
+
y.left = z.left;
|
|
139
|
+
y.left.parent = y;
|
|
140
|
+
y.color = z.color;
|
|
141
|
+
}
|
|
142
|
+
if (yOriginalColor === 0) {
|
|
143
|
+
this._fixDelete(x);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
helper(this.root);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
|
|
151
|
+
* given key in a red-black tree.
|
|
152
|
+
* @param {number} key - The key parameter is a number that represents the value we are searching for
|
|
153
|
+
* in the RBTree.
|
|
154
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter that represents the starting
|
|
155
|
+
* point for the search in the binary search tree. If no value is provided for `beginRoot`, it
|
|
156
|
+
* defaults to the root of the binary search tree (`this.root`).
|
|
157
|
+
* @returns a RBTreeNode.
|
|
158
|
+
*/
|
|
159
|
+
getNode(key: number, beginRoot = this.root): RBTreeNode {
|
|
160
|
+
const dfs = (node: RBTreeNode): RBTreeNode => {
|
|
161
|
+
if (node === this.NIL || key === node.key) {
|
|
162
|
+
return node;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (key < node.key) {
|
|
166
|
+
return dfs(node.left);
|
|
167
|
+
}
|
|
168
|
+
return dfs(node.right);
|
|
169
|
+
}
|
|
170
|
+
return dfs(beginRoot);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The function returns the leftmost node in a red-black tree.
|
|
176
|
+
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
|
|
177
|
+
* a Red-Black Tree.
|
|
178
|
+
* @returns The leftmost node in the given RBTreeNode.
|
|
179
|
+
*/
|
|
180
|
+
getLeftMost(node: RBTreeNode): RBTreeNode {
|
|
181
|
+
while (node.left !== null && node.left !== this.NIL) {
|
|
182
|
+
node = node.left;
|
|
183
|
+
}
|
|
184
|
+
return node;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The function returns the rightmost node in a red-black tree.
|
|
190
|
+
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
191
|
+
* @returns the rightmost node in a red-black tree.
|
|
192
|
+
*/
|
|
193
|
+
getRightMost(node: RBTreeNode): RBTreeNode {
|
|
194
|
+
while (node.right !== null && node.right !== this.NIL) {
|
|
195
|
+
node = node.right;
|
|
196
|
+
}
|
|
197
|
+
return node;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The function returns the successor of a given node in a red-black tree.
|
|
202
|
+
* @param {RBTreeNode} x - RBTreeNode - The node for which we want to find the successor.
|
|
203
|
+
* @returns the successor of the given RBTreeNode.
|
|
204
|
+
*/
|
|
205
|
+
getSuccessor(x: RBTreeNode): RBTreeNode {
|
|
206
|
+
|
|
207
|
+
if (x.right !== this.NIL) {
|
|
208
|
+
return this.getLeftMost(x.right);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
let y: RBTreeNode = x.parent;
|
|
212
|
+
while (y !== this.NIL && y !== null && x === y.right) {
|
|
213
|
+
x = y;
|
|
214
|
+
y = y.parent;
|
|
215
|
+
}
|
|
216
|
+
return y;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* The function returns the predecessor of a given node in a red-black tree.
|
|
221
|
+
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
222
|
+
* Red-Black Tree.
|
|
223
|
+
* @returns the predecessor of the given RBTreeNode 'x'.
|
|
224
|
+
*/
|
|
225
|
+
getPredecessor(x: RBTreeNode): RBTreeNode {
|
|
226
|
+
|
|
227
|
+
if (x.left !== this.NIL) {
|
|
228
|
+
return this.getRightMost(x.left);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let y: RBTreeNode = x.parent;
|
|
232
|
+
while (y !== this.NIL && x === y.left) {
|
|
233
|
+
x = y;
|
|
234
|
+
y = y.parent;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return y;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* The function performs a left rotation on a red-black tree node.
|
|
242
|
+
* @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
|
|
243
|
+
*/
|
|
244
|
+
protected _leftRotate(x: RBTreeNode): void {
|
|
245
|
+
const y: RBTreeNode = x.right;
|
|
246
|
+
x.right = y.left;
|
|
247
|
+
if (y.left !== this.NIL) {
|
|
248
|
+
y.left.parent = x;
|
|
249
|
+
}
|
|
250
|
+
y.parent = x.parent;
|
|
251
|
+
if (x.parent === null) {
|
|
252
|
+
this._root = y;
|
|
253
|
+
} else if (x === x.parent.left) {
|
|
254
|
+
x.parent.left = y;
|
|
255
|
+
} else {
|
|
256
|
+
x.parent.right = y;
|
|
257
|
+
}
|
|
258
|
+
y.left = x;
|
|
259
|
+
x.parent = y;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* The function performs a right rotation on a red-black tree node.
|
|
264
|
+
* @param {RBTreeNode} x - x is a RBTreeNode, which represents the node that needs to be right
|
|
265
|
+
* rotated.
|
|
266
|
+
*/
|
|
267
|
+
protected _rightRotate(x: RBTreeNode): void {
|
|
268
|
+
const y: RBTreeNode = x.left;
|
|
269
|
+
x.left = y.right;
|
|
270
|
+
if (y.right !== this.NIL) {
|
|
271
|
+
y.right.parent = x;
|
|
272
|
+
}
|
|
273
|
+
y.parent = x.parent;
|
|
274
|
+
if (x.parent === null) {
|
|
275
|
+
this._root = y;
|
|
276
|
+
} else if (x === x.parent.right) {
|
|
277
|
+
x.parent.right = y;
|
|
278
|
+
} else {
|
|
279
|
+
x.parent.left = y;
|
|
280
|
+
}
|
|
281
|
+
y.right = x;
|
|
282
|
+
x.parent = y;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
|
|
287
|
+
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
288
|
+
* red-black tree.
|
|
289
|
+
*/
|
|
290
|
+
protected _fixDelete(x: RBTreeNode): void {
|
|
291
|
+
let s: RBTreeNode;
|
|
292
|
+
while (x !== this.root && x.color === 0) {
|
|
293
|
+
if (x === x.parent.left) {
|
|
294
|
+
s = x.parent.right;
|
|
295
|
+
if (s.color === 1) {
|
|
296
|
+
|
|
297
|
+
s.color = RBTNColor.BLACK;
|
|
298
|
+
x.parent.color = RBTNColor.RED;
|
|
299
|
+
this._leftRotate(x.parent);
|
|
300
|
+
s = x.parent.right;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (s.left.color === 0 && s.right.color === 0) {
|
|
304
|
+
|
|
305
|
+
s.color = RBTNColor.RED;
|
|
306
|
+
x = x.parent;
|
|
307
|
+
} else {
|
|
308
|
+
if (s.right.color === 0) {
|
|
309
|
+
|
|
310
|
+
s.left.color = RBTNColor.BLACK;
|
|
311
|
+
s.color = RBTNColor.RED;
|
|
312
|
+
this._rightRotate(s);
|
|
313
|
+
s = x.parent.right;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
s.color = x.parent.color;
|
|
317
|
+
x.parent.color = RBTNColor.BLACK;
|
|
318
|
+
s.right.color = RBTNColor.BLACK;
|
|
319
|
+
this._leftRotate(x.parent);
|
|
320
|
+
x = this.root;
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
s = x.parent.left;
|
|
324
|
+
if (s.color === 1) {
|
|
325
|
+
|
|
326
|
+
s.color = RBTNColor.BLACK;
|
|
327
|
+
x.parent.color = RBTNColor.RED;
|
|
328
|
+
this._rightRotate(x.parent);
|
|
329
|
+
s = x.parent.left;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (s.right.color === 0 && s.right.color === 0) {
|
|
333
|
+
|
|
334
|
+
s.color = RBTNColor.RED;
|
|
335
|
+
x = x.parent;
|
|
336
|
+
} else {
|
|
337
|
+
if (s.left.color === 0) {
|
|
338
|
+
|
|
339
|
+
s.right.color = RBTNColor.BLACK;
|
|
340
|
+
s.color = RBTNColor.RED;
|
|
341
|
+
this._leftRotate(s);
|
|
342
|
+
s = x.parent.left;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
s.color = x.parent.color;
|
|
346
|
+
x.parent.color = RBTNColor.BLACK;
|
|
347
|
+
s.left.color = RBTNColor.BLACK;
|
|
348
|
+
this._rightRotate(x.parent);
|
|
349
|
+
x = this.root;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
x.color = RBTNColor.BLACK;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* The function `_rbTransplant` replaces one node in a red-black tree with another node.
|
|
358
|
+
* @param {RBTreeNode} u - The parameter "u" represents a RBTreeNode object.
|
|
359
|
+
* @param {RBTreeNode} v - The parameter "v" is a RBTreeNode object.
|
|
360
|
+
*/
|
|
361
|
+
protected _rbTransplant(u: RBTreeNode, v: RBTreeNode): void {
|
|
362
|
+
if (u.parent === null) {
|
|
363
|
+
this._root = v;
|
|
364
|
+
} else if (u === u.parent.left) {
|
|
365
|
+
u.parent.left = v;
|
|
366
|
+
} else {
|
|
367
|
+
u.parent.right = v;
|
|
368
|
+
}
|
|
369
|
+
v.parent = u.parent;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
374
|
+
* @param {RBTreeNode} k - The parameter `k` is a RBTreeNode object, which represents a node in a
|
|
375
|
+
* red-black tree.
|
|
376
|
+
*/
|
|
377
|
+
protected _fixInsert(k: RBTreeNode): void {
|
|
378
|
+
let u: RBTreeNode;
|
|
379
|
+
while (k.parent.color === 1) {
|
|
380
|
+
if (k.parent === k.parent.parent.right) {
|
|
381
|
+
u = k.parent.parent.left;
|
|
382
|
+
if (u.color === 1) {
|
|
383
|
+
|
|
384
|
+
u.color = RBTNColor.BLACK;
|
|
385
|
+
k.parent.color = RBTNColor.BLACK;
|
|
386
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
387
|
+
k = k.parent.parent;
|
|
388
|
+
} else {
|
|
389
|
+
if (k === k.parent.left) {
|
|
390
|
+
|
|
391
|
+
k = k.parent;
|
|
392
|
+
this._rightRotate(k);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
k.parent.color = RBTNColor.BLACK;
|
|
396
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
397
|
+
this._leftRotate(k.parent.parent);
|
|
398
|
+
}
|
|
399
|
+
} else {
|
|
400
|
+
u = k.parent.parent.right;
|
|
401
|
+
|
|
402
|
+
if (u.color === 1) {
|
|
403
|
+
|
|
404
|
+
u.color = RBTNColor.BLACK;
|
|
405
|
+
k.parent.color = RBTNColor.BLACK;
|
|
406
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
407
|
+
k = k.parent.parent;
|
|
408
|
+
} else {
|
|
409
|
+
if (k === k.parent.right) {
|
|
410
|
+
|
|
411
|
+
k = k.parent;
|
|
412
|
+
this._leftRotate(k);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
k.parent.color = RBTNColor.BLACK;
|
|
416
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
417
|
+
this._rightRotate(k.parent.parent);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (k === this.root) {
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
this.root.color = RBTNColor.BLACK;
|
|
425
|
+
}
|
|
426
|
+
}
|