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.
@@ -1,358 +1,426 @@
1
- import {BTNKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types';
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
- color: RBColor;
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 RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
16
- extends BST<V, N>
17
- implements IBinaryTree<V, N> {
18
- constructor(options?: RBTreeOptions) {
19
- super(options);
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
- override createNode(key: BTNKey, value?: V): N {
23
- return new RBTreeNode(key, value) as N;
27
+ protected _root: RBTreeNode;
28
+
29
+ get root(): RBTreeNode {
30
+ return this._root;
24
31
  }
25
32
 
26
- // override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
27
- // const inserted = super.add(keyOrNode, value);
28
- // if (inserted) this._fixInsertViolation(inserted);
29
- // return inserted;
30
- // }
31
- //
32
- // // Method for fixing insert violations in a red-black tree
33
- // protected _fixInsertViolation(node: N) {
34
- // while (node !== this.root! && node.color === RBColor.RED && node.parent!.color === RBColor.RED) {
35
- // const parent = node.parent!;
36
- // const grandparent = parent.parent!;
37
- // let uncle: N | null | undefined = null;
38
- //
39
- // if (parent === grandparent.left) {
40
- // uncle = grandparent.right;
41
- //
42
- // // Case 1: The uncle node is red
43
- // if (uncle && uncle.color === RBColor.RED) {
44
- // grandparent.color = RBColor.RED;
45
- // parent.color = RBColor.BLACK;
46
- // uncle.color = RBColor.BLACK;
47
- // node = grandparent;
48
- // } else {
49
- // // Case 2: The uncle node is black, and the current node is a right child
50
- // if (node === parent.right) {
51
- // this._rotateLeft(parent);
52
- // node = parent;
53
- // // Update parent reference
54
- // node.parent = grandparent;
55
- // parent.parent = node;
56
- // }
57
- //
58
- // // Case 3: The uncle node is black, and the current node is a left child
59
- // parent.color = RBColor.BLACK;
60
- // grandparent.color = RBColor.RED;
61
- // this._rotateRight(grandparent);
62
- // }
63
- // } else {
64
- // // Symmetric case: The parent is the right child of the grandparent
65
- // uncle = grandparent.left;
66
- //
67
- // // Case 1: The uncle node is red
68
- // if (uncle && uncle.color === RBColor.RED) {
69
- // grandparent.color = RBColor.RED;
70
- // parent.color = RBColor.BLACK;
71
- // uncle.color = RBColor.BLACK;
72
- // node = grandparent;
73
- // } else {
74
- // // Case 2: The uncle node is black, and the current node is a left child
75
- // if (node === parent.left) {
76
- // this._rotateRight(parent);
77
- // node = parent;
78
- // // Update parent reference
79
- // node.parent = grandparent;
80
- // parent.parent = node;
81
- // }
82
- //
83
- // // Case 3: The uncle node is black, and the current node is a right child
84
- // parent.color = RBColor.BLACK;
85
- // grandparent.color = RBColor.RED;
86
- // this._rotateLeft(grandparent);
87
- // }
88
- // }
89
- // }
90
- //
91
- // // The root node is always black
92
- // this.root!.color = RBColor.BLACK;
93
- // }
94
- //
95
- // // Left rotation operation
96
- // protected _rotateLeft(node: N) {
97
- // const rightChild = node.right;
98
- // node.right = rightChild!.left;
99
- //
100
- // if (rightChild!.left) {
101
- // rightChild!.left.parent = node;
102
- // }
103
- //
104
- // rightChild!.parent = node.parent;
105
- //
106
- // if (node === this.root) {
107
- // // @ts-ignore
108
- // this._setRoot(rightChild);
109
- // } else if (node === node.parent!.left) {
110
- // node.parent!.left = rightChild;
111
- // } else {
112
- // node.parent!.right = rightChild;
113
- // }
114
- //
115
- // rightChild!.left = node;
116
- // node.parent = rightChild;
117
- // }
118
- //
119
- // // Right rotation operation
120
- // protected _rotateRight(node: N) {
121
- // const leftChild = node.left;
122
- // node.left = leftChild!.right;
123
- //
124
- // if (leftChild!.right) {
125
- // leftChild!.right.parent = node;
126
- // }
127
- //
128
- // leftChild!.parent = node.parent;
129
- //
130
- // if (node === this.root) {
131
- // // @ts-ignore
132
- // this._setRoot(leftChild);
133
- // } else if (node === node.parent!.right) {
134
- // node.parent!.right = leftChild;
135
- // } else {
136
- // node.parent!.left = leftChild;
137
- // }
138
- //
139
- // leftChild!.right = node;
140
- // node.parent = leftChild;
141
- // }
142
- //
143
- // protected _isNodeRed(node: N | null | undefined): boolean {
144
- // return node ? node.color === RBColor.RED : false;
145
- // }
146
- //
147
- // // Find the sibling node
148
- // protected _findSibling(node: N): N | null | undefined {
149
- // if (!node.parent) {
150
- // return undefined;
151
- // }
152
- //
153
- // return node === node.parent.left ? node.parent.right : node.parent.left;
154
- // }
155
- //
156
- // // Remove a node
157
- // protected _removeNode(node: N, replacement: N | null | undefined): void {
158
- // if (node === this.root && !replacement) {
159
- // // If there's only the root node and no replacement, simply delete the root node
160
- // this._setRoot(null);
161
- // } else if (node === this.root || this._isNodeRed(node)) {
162
- // // If the node is the root or a red node, delete it directly
163
- // if (node.parent!.left === node) {
164
- // node.parent!.left = replacement;
165
- // } else {
166
- // node.parent!.right = replacement;
167
- // }
168
- //
169
- // if (replacement) {
170
- // replacement.parent = node.parent!;
171
- // replacement.color = RBColor.BLACK; // Set the replacement node's color to black
172
- // }
173
- // } else {
174
- // // If the node is a black node, perform removal and repair
175
- // const sibling = this._findSibling(node);
176
- //
177
- // if (node.parent!.left === node) {
178
- // node.parent!.left = replacement;
179
- // } else {
180
- // node.parent!.right = replacement;
181
- // }
182
- //
183
- // if (replacement) {
184
- // replacement.parent = node.parent;
185
- // }
186
- //
187
- // if (!this._isNodeRed(sibling)) {
188
- // // If the sibling node is black, perform repair
189
- // this._fixDeleteViolation(replacement || node);
190
- // }
191
- // }
192
- //
193
- // if (node.parent) {
194
- // node.parent = null;
195
- // }
196
- // node.left = null;
197
- // node.right = null;
198
- // }
199
- //
200
- // override delete(keyOrNode: BTNKey | N): BinaryTreeDeletedResult<N>[] {
201
- // const node = this.get(keyOrNode);
202
- // const result: BinaryTreeDeletedResult<N>[] = [{deleted: undefined, needBalanced: null}];
203
- // if (!node) return result; // Node does not exist
204
- //
205
- // const replacement = this._getReplacementNode(node);
206
- //
207
- // const isRed = this._isNodeRed(node);
208
- // const isRedReplacement = this._isNodeRed(replacement);
209
- //
210
- // // Remove the node
211
- // this._removeNode(node, replacement);
212
- //
213
- // if (isRed || isRedReplacement) {
214
- // // If the removed node is red or the replacement node is red, no repair is needed
215
- // return result;
216
- // }
217
- //
218
- // // Repair any violation introduced by the removal
219
- // this._fixDeleteViolation(replacement);
220
- //
221
- // return result;
222
- // }
223
- //
224
- // // Repair operation after node deletion
225
- // protected _fixDeleteViolation(node: N | null | undefined) {
226
- // let sibling;
227
- //
228
- // while (node && node !== this.root && !this._isNodeRed(node)) {
229
- // if (node === node.parent!.left) {
230
- // sibling = node.parent!.right;
231
- //
232
- // if (sibling && this._isNodeRed(sibling)) {
233
- // // Case 1: The sibling node is red
234
- // sibling.color = RBColor.BLACK;
235
- // node.parent!.color = RBColor.RED;
236
- // this._rotateLeft(node.parent!);
237
- // sibling = node.parent!.right;
238
- // }
239
- //
240
- // if (!sibling) return;
241
- //
242
- // if (
243
- // (!sibling.left || sibling.left.color === RBColor.BLACK) &&
244
- // (!sibling.right || sibling.right.color === RBColor.BLACK)
245
- // ) {
246
- // // Case 2: The sibling node and its children are all black
247
- // sibling.color = RBColor.RED;
248
- // node = node.parent!;
249
- // } else {
250
- // if (!(sibling.right && this._isNodeRed(sibling.right))) {
251
- // // Case 3: The sibling node is black, and the left child is red, the right child is black
252
- // sibling.left!.color = RBColor.BLACK;
253
- // sibling.color = RBColor.RED;
254
- // this._rotateRight(sibling);
255
- // sibling = node.parent!.right;
256
- // }
257
- //
258
- // // Case 4: The sibling node is black, and the right child is red
259
- // if (sibling) {
260
- // sibling.color = node.parent!.color;
261
- // }
262
- // if (node.parent) {
263
- // node.parent.color = RBColor.BLACK;
264
- // }
265
- // if (sibling!.right) {
266
- // sibling!.right.color = RBColor.BLACK;
267
- // }
268
- // this._rotateLeft(node.parent!);
269
- // node = this.root;
270
- // }
271
- // } else {
272
- // // Symmetric case: The parent is the right child of the grandparent
273
- // sibling = node.parent!.left;
274
- //
275
- // if (sibling && this._isNodeRed(sibling)) {
276
- // // Case 1: The sibling node is red
277
- // sibling.color = RBColor.BLACK;
278
- // node.parent!.color = RBColor.RED;
279
- // this._rotateRight(node.parent!);
280
- // sibling = node.parent!.left;
281
- // }
282
- //
283
- // if (!sibling) return;
284
- //
285
- // if (
286
- // (!sibling.left || sibling.left.color === RBColor.BLACK) &&
287
- // (!sibling.right || sibling.right.color === RBColor.BLACK)
288
- // ) {
289
- // // Case 2: The sibling node and its children are all black
290
- // sibling.color = RBColor.RED;
291
- // node = node.parent!;
292
- // } else {
293
- // if (!(sibling.left && this._isNodeRed(sibling.left))) {
294
- // // Case 3: The sibling node is black, and the right child is red, the left child is black
295
- // sibling.right!.color = RBColor.BLACK;
296
- // sibling.color = RBColor.RED;
297
- // this._rotateLeft(sibling);
298
- // sibling = node.parent!.left;
299
- // }
300
- //
301
- // // Case 4: The sibling node is black, and the left child is red
302
- // if (sibling) {
303
- // sibling.color = node.parent!.color;
304
- // }
305
- // if (node.parent) {
306
- // node.parent.color = RBColor.BLACK;
307
- // }
308
- // if (sibling!.left) {
309
- // sibling!.left.color = RBColor.BLACK;
310
- // }
311
- // this._rotateRight(node.parent!);
312
- // node = this.root;
313
- // }
314
- // }
315
- // }
316
- //
317
- // if (node) {
318
- // node.color = RBColor.BLACK;
319
- // }
320
- // }
321
- //
322
- // protected _findMin(node: N): N {
323
- // while (node.left) {
324
- // node = node.left;
325
- // }
326
- // return node;
327
- // }
328
- //
329
- // // Get the replacement node
330
- // protected _getReplacementNode(node: N): N | null | undefined {
331
- // if (node.left && node.right) {
332
- // return this._findSuccessor(node);
333
- // }
334
- //
335
- // if (!node.left && !node.right) {
336
- // return null; // Return a fake node with color black
337
- // }
338
- //
339
- // return node.left || node.right;
340
- // }
341
- //
342
- // // Find the successor node
343
- // protected _findSuccessor(node: N): N | null | undefined {
344
- // if (node.right) {
345
- // // If the node has a right child, find the minimum node in the right subtree as the successor
346
- // return this._findMin(node.right);
347
- // }
348
- //
349
- // // Otherwise, traverse upward until finding the first parent whose left child is the current node
350
- // let parent = node.parent;
351
- // while (parent && node === parent.right) {
352
- // node = parent;
353
- // parent = parent.parent;
354
- // }
355
- //
356
- // return parent;
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
+ }