min-heap-typed 1.50.0 → 1.50.2
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/base/iterable-base.d.ts +114 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/data-structures/binary-tree/bst.js +30 -71
- package/dist/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/data-structures/graph/abstract-graph.js +3 -0
- package/dist/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/data-structures/graph/directed-graph.js +26 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/data-structures/graph/undirected-graph.js +25 -0
- package/dist/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/data-structures/hash/hash-map.js +160 -25
- package/dist/data-structures/heap/heap.d.ts +66 -6
- package/dist/data-structures/heap/heap.js +66 -6
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/data-structures/linked-list/doubly-linked-list.js +70 -64
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +128 -103
- package/dist/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/queue/deque.d.ts +49 -19
- package/dist/data-structures/queue/deque.js +101 -47
- package/dist/data-structures/queue/queue.d.ts +39 -5
- package/dist/data-structures/queue/queue.js +47 -5
- package/dist/data-structures/stack/stack.d.ts +16 -0
- package/dist/data-structures/stack/stack.js +22 -0
- package/dist/data-structures/trie/trie.d.ts +38 -1
- package/dist/data-structures/trie/trie.js +41 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +172 -19
- package/src/data-structures/binary-tree/avl-tree.ts +97 -97
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +89 -131
- package/src/data-structures/binary-tree/rb-tree.ts +127 -155
- package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +28 -0
- package/src/data-structures/hash/hash-map.ts +175 -34
- package/src/data-structures/heap/heap.ts +66 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
- package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/queue/deque.ts +108 -49
- package/src/data-structures/queue/queue.ts +51 -5
- package/src/data-structures/stack/stack.ts +24 -0
- package/src/data-structures/trie/trie.ts +45 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -10,7 +10,6 @@ import {
|
|
|
10
10
|
BinaryTreeDeleteResult,
|
|
11
11
|
BSTNKeyOrNode,
|
|
12
12
|
BTNCallback,
|
|
13
|
-
IterationType,
|
|
14
13
|
KeyOrNodeOrEntry,
|
|
15
14
|
RBTNColor,
|
|
16
15
|
RBTreeOptions,
|
|
@@ -23,8 +22,8 @@ import { IBinaryTree } from '../../interfaces';
|
|
|
23
22
|
export class RedBlackTreeNode<
|
|
24
23
|
K = any,
|
|
25
24
|
V = any,
|
|
26
|
-
|
|
27
|
-
> extends BSTNode<K, V,
|
|
25
|
+
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>
|
|
26
|
+
> extends BSTNode<K, V, NODE> {
|
|
28
27
|
color: RBTNColor;
|
|
29
28
|
|
|
30
29
|
constructor(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK) {
|
|
@@ -43,17 +42,17 @@ export class RedBlackTreeNode<
|
|
|
43
42
|
export class RedBlackTree<
|
|
44
43
|
K = any,
|
|
45
44
|
V = any,
|
|
46
|
-
|
|
47
|
-
TREE extends RedBlackTree<K, V,
|
|
45
|
+
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
|
|
46
|
+
TREE extends RedBlackTree<K, V, NODE, TREE> = RedBlackTree<K, V, NODE, RedBlackTreeNested<K, V, NODE>>
|
|
48
47
|
>
|
|
49
|
-
extends BST<K, V,
|
|
50
|
-
implements IBinaryTree<K, V,
|
|
51
|
-
Sentinel:
|
|
48
|
+
extends BST<K, V, NODE, TREE>
|
|
49
|
+
implements IBinaryTree<K, V, NODE, TREE> {
|
|
50
|
+
Sentinel: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
|
|
52
51
|
|
|
53
52
|
/**
|
|
54
53
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
55
54
|
* initializes the tree with optional nodes and options.
|
|
56
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
55
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
57
56
|
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
58
57
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
59
58
|
* nodes to the
|
|
@@ -61,16 +60,16 @@ export class RedBlackTree<
|
|
|
61
60
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
62
61
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
63
62
|
*/
|
|
64
|
-
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V,
|
|
63
|
+
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: RBTreeOptions<K>) {
|
|
65
64
|
super([], options);
|
|
66
65
|
|
|
67
66
|
this._root = this.Sentinel;
|
|
68
67
|
if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
|
|
69
68
|
}
|
|
70
69
|
|
|
71
|
-
protected _root:
|
|
70
|
+
protected _root: NODE;
|
|
72
71
|
|
|
73
|
-
get root():
|
|
72
|
+
get root(): NODE {
|
|
74
73
|
return this._root;
|
|
75
74
|
}
|
|
76
75
|
|
|
@@ -92,8 +91,8 @@ export class RedBlackTree<
|
|
|
92
91
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
93
92
|
* value, and color.
|
|
94
93
|
*/
|
|
95
|
-
override createNode(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK):
|
|
96
|
-
return new RedBlackTreeNode<K, V,
|
|
94
|
+
override createNode(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK): NODE {
|
|
95
|
+
return new RedBlackTreeNode<K, V, NODE>(key, value, color) as NODE;
|
|
97
96
|
}
|
|
98
97
|
|
|
99
98
|
/**
|
|
@@ -104,22 +103,22 @@ export class RedBlackTree<
|
|
|
104
103
|
* @returns a new instance of a RedBlackTree object.
|
|
105
104
|
*/
|
|
106
105
|
override createTree(options?: RBTreeOptions<K>): TREE {
|
|
107
|
-
return new RedBlackTree<K, V,
|
|
106
|
+
return new RedBlackTree<K, V, NODE, TREE>([], {
|
|
108
107
|
iterationType: this.iterationType,
|
|
109
108
|
...options
|
|
110
109
|
}) as TREE;
|
|
111
110
|
}
|
|
112
111
|
|
|
113
112
|
/**
|
|
114
|
-
* The function `
|
|
115
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
113
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
114
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
116
115
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
117
|
-
* `
|
|
116
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
118
117
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
119
|
-
* @returns a node of type
|
|
118
|
+
* @returns a node of type NODE or undefined.
|
|
120
119
|
*/
|
|
121
|
-
override
|
|
122
|
-
let node:
|
|
120
|
+
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
121
|
+
let node: NODE | undefined;
|
|
123
122
|
|
|
124
123
|
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
125
124
|
return;
|
|
@@ -142,20 +141,15 @@ export class RedBlackTree<
|
|
|
142
141
|
|
|
143
142
|
/**
|
|
144
143
|
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
145
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
144
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
146
145
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
147
146
|
* class.
|
|
148
147
|
*/
|
|
149
|
-
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
148
|
+
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
|
|
150
149
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
151
150
|
}
|
|
152
151
|
|
|
153
|
-
|
|
154
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
155
|
-
* Space Complexity: O(1)
|
|
156
|
-
*/
|
|
157
|
-
|
|
158
|
-
override isRealNode(node: N | undefined): node is N {
|
|
152
|
+
override isRealNode(node: NODE | undefined): node is NODE {
|
|
159
153
|
if (node === this.Sentinel || node === undefined) return false;
|
|
160
154
|
return node instanceof RedBlackTreeNode;
|
|
161
155
|
}
|
|
@@ -163,7 +157,7 @@ export class RedBlackTree<
|
|
|
163
157
|
/**
|
|
164
158
|
* Time Complexity: O(log n)
|
|
165
159
|
* Space Complexity: O(1)
|
|
166
|
-
*
|
|
160
|
+
* On average (where n is the number of nodes in the tree)
|
|
167
161
|
*/
|
|
168
162
|
|
|
169
163
|
/**
|
|
@@ -176,17 +170,17 @@ export class RedBlackTree<
|
|
|
176
170
|
* entry.
|
|
177
171
|
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
178
172
|
* being added to the binary search tree.
|
|
179
|
-
* @returns The method `add` returns either the newly added node (`
|
|
173
|
+
* @returns The method `add` returns either the newly added node (`NODE`) or `undefined`.
|
|
180
174
|
*/
|
|
181
|
-
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
182
|
-
const newNode = this.
|
|
175
|
+
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
176
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
183
177
|
if (newNode === undefined) return false;
|
|
184
178
|
|
|
185
179
|
newNode.left = this.Sentinel;
|
|
186
180
|
newNode.right = this.Sentinel;
|
|
187
181
|
|
|
188
|
-
let y:
|
|
189
|
-
let x:
|
|
182
|
+
let y: NODE | undefined = undefined;
|
|
183
|
+
let x: NODE | undefined = this.root;
|
|
190
184
|
|
|
191
185
|
while (x !== this.Sentinel) {
|
|
192
186
|
y = x;
|
|
@@ -232,7 +226,6 @@ export class RedBlackTree<
|
|
|
232
226
|
/**
|
|
233
227
|
* Time Complexity: O(log n)
|
|
234
228
|
* Space Complexity: O(1)
|
|
235
|
-
* on average (where n is the number of nodes in the tree)
|
|
236
229
|
*/
|
|
237
230
|
|
|
238
231
|
/**
|
|
@@ -245,20 +238,20 @@ export class RedBlackTree<
|
|
|
245
238
|
* that you want to use to identify the node that you want to delete from the binary tree. It can be
|
|
246
239
|
* of any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
247
240
|
* you don't want to
|
|
248
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
241
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` and
|
|
249
242
|
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
250
243
|
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
251
|
-
* @returns an array of `BinaryTreeDeleteResult<
|
|
244
|
+
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
252
245
|
*/
|
|
253
|
-
delete<C extends BTNCallback<
|
|
246
|
+
delete<C extends BTNCallback<NODE>>(
|
|
254
247
|
identifier: ReturnType<C> | null | undefined,
|
|
255
248
|
callback: C = this._defaultOneParamCallback as C
|
|
256
|
-
): BinaryTreeDeleteResult<
|
|
257
|
-
const ans: BinaryTreeDeleteResult<
|
|
249
|
+
): BinaryTreeDeleteResult<NODE>[] {
|
|
250
|
+
const ans: BinaryTreeDeleteResult<NODE>[] = [];
|
|
258
251
|
if (identifier === null) return ans;
|
|
259
|
-
const helper = (node:
|
|
260
|
-
let z:
|
|
261
|
-
let x:
|
|
252
|
+
const helper = (node: NODE | undefined): void => {
|
|
253
|
+
let z: NODE = this.Sentinel;
|
|
254
|
+
let x: NODE | undefined, y: NODE;
|
|
262
255
|
while (node !== this.Sentinel) {
|
|
263
256
|
if (node && callback(node) === identifier) {
|
|
264
257
|
z = node;
|
|
@@ -311,27 +304,6 @@ export class RedBlackTree<
|
|
|
311
304
|
return ans;
|
|
312
305
|
}
|
|
313
306
|
|
|
314
|
-
getNode<C extends BTNCallback<N, K>>(
|
|
315
|
-
identifier: K,
|
|
316
|
-
callback?: C,
|
|
317
|
-
beginRoot?: N | undefined,
|
|
318
|
-
iterationType?: IterationType
|
|
319
|
-
): N | undefined;
|
|
320
|
-
|
|
321
|
-
getNode<C extends BTNCallback<N, N>>(
|
|
322
|
-
identifier: N | undefined,
|
|
323
|
-
callback?: C,
|
|
324
|
-
beginRoot?: N | undefined,
|
|
325
|
-
iterationType?: IterationType
|
|
326
|
-
): N | undefined;
|
|
327
|
-
|
|
328
|
-
getNode<C extends BTNCallback<N>>(
|
|
329
|
-
identifier: ReturnType<C>,
|
|
330
|
-
callback: C,
|
|
331
|
-
beginRoot?: N | undefined,
|
|
332
|
-
iterationType?: IterationType
|
|
333
|
-
): N | undefined;
|
|
334
|
-
|
|
335
307
|
/**
|
|
336
308
|
* Time Complexity: O(log n)
|
|
337
309
|
* Space Complexity: O(1)
|
|
@@ -349,26 +321,36 @@ export class RedBlackTree<
|
|
|
349
321
|
* node that matches the other criteria
|
|
350
322
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
351
323
|
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
352
|
-
* function should take a single parameter of type `
|
|
353
|
-
* @param {K |
|
|
324
|
+
* function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
|
|
325
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is the starting point for
|
|
354
326
|
* searching for a node in a binary tree. It can be either a key value or a node object. If it is not
|
|
355
327
|
* provided, the search will start from the root of the binary tree.
|
|
356
328
|
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
357
329
|
* iteration to be performed when searching for nodes in the binary tree. It is used in the
|
|
358
330
|
* `getNodes` method, which is called within the `getNode` method.
|
|
359
|
-
* @returns a value of type `
|
|
331
|
+
* @returns a value of type `NODE`, `null`, or `undefined`.
|
|
360
332
|
*/
|
|
361
|
-
getNode<C extends BTNCallback<
|
|
333
|
+
getNode<C extends BTNCallback<NODE>>(
|
|
362
334
|
identifier: ReturnType<C> | undefined,
|
|
363
335
|
callback: C = this._defaultOneParamCallback as C,
|
|
364
|
-
beginRoot: BSTNKeyOrNode<K,
|
|
336
|
+
beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
|
|
365
337
|
iterationType = this.iterationType
|
|
366
|
-
):
|
|
338
|
+
): NODE | null | undefined {
|
|
367
339
|
if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
|
|
368
340
|
beginRoot = this.ensureNode(beginRoot);
|
|
369
341
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
370
342
|
}
|
|
371
343
|
|
|
344
|
+
/**
|
|
345
|
+
* Time Complexity: O(1)
|
|
346
|
+
* Space Complexity: O(1)
|
|
347
|
+
*/
|
|
348
|
+
|
|
349
|
+
override clear() {
|
|
350
|
+
this._root = this.Sentinel;
|
|
351
|
+
this._size = 0;
|
|
352
|
+
}
|
|
353
|
+
|
|
372
354
|
/**
|
|
373
355
|
* Time Complexity: O(log n)
|
|
374
356
|
* Space Complexity: O(1)
|
|
@@ -383,12 +365,12 @@ export class RedBlackTree<
|
|
|
383
365
|
* Red-Black Tree.
|
|
384
366
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
385
367
|
*/
|
|
386
|
-
override getPredecessor(x:
|
|
368
|
+
override getPredecessor(x: NODE): NODE {
|
|
387
369
|
if (this.isRealNode(x.left)) {
|
|
388
370
|
return this.getRightMost(x.left)!;
|
|
389
371
|
}
|
|
390
372
|
|
|
391
|
-
let y:
|
|
373
|
+
let y: NODE | undefined = x.parent;
|
|
392
374
|
while (this.isRealNode(y) && x === y.left) {
|
|
393
375
|
x = y!;
|
|
394
376
|
y = y!.parent;
|
|
@@ -397,17 +379,7 @@ export class RedBlackTree<
|
|
|
397
379
|
return y!;
|
|
398
380
|
}
|
|
399
381
|
|
|
400
|
-
|
|
401
|
-
* Time Complexity: O(1)
|
|
402
|
-
* Space Complexity: O(1)
|
|
403
|
-
*/
|
|
404
|
-
|
|
405
|
-
override clear() {
|
|
406
|
-
this._root = this.Sentinel;
|
|
407
|
-
this._size = 0;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
protected override _setRoot(v: N) {
|
|
382
|
+
protected override _setRoot(v: NODE) {
|
|
411
383
|
if (v) {
|
|
412
384
|
v.parent = undefined;
|
|
413
385
|
}
|
|
@@ -424,11 +396,11 @@ export class RedBlackTree<
|
|
|
424
396
|
* Space Complexity: O(1)
|
|
425
397
|
*
|
|
426
398
|
* The function performs a left rotation on a binary tree node.
|
|
427
|
-
* @param {RedBlackTreeNode} x - The parameter `x` is of type `
|
|
399
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `NODE`, which likely represents a node in a binary tree.
|
|
428
400
|
*/
|
|
429
|
-
protected _leftRotate(x:
|
|
401
|
+
protected _leftRotate(x: NODE): void {
|
|
430
402
|
if (x.right) {
|
|
431
|
-
const y:
|
|
403
|
+
const y: NODE = x.right;
|
|
432
404
|
x.right = y.left;
|
|
433
405
|
if (y.left !== this.Sentinel) {
|
|
434
406
|
if (y.left) y.left.parent = x;
|
|
@@ -459,9 +431,9 @@ export class RedBlackTree<
|
|
|
459
431
|
* @param {RedBlackTreeNode} x - x is a RedBlackTreeNode, which represents the node that needs to be right
|
|
460
432
|
* rotated.
|
|
461
433
|
*/
|
|
462
|
-
protected _rightRotate(x:
|
|
434
|
+
protected _rightRotate(x: NODE): void {
|
|
463
435
|
if (x.left) {
|
|
464
|
-
const y:
|
|
436
|
+
const y: NODE = x.left;
|
|
465
437
|
x.left = y.right;
|
|
466
438
|
if (y.right !== this.Sentinel) {
|
|
467
439
|
if (y.right) y.right.parent = x;
|
|
@@ -479,6 +451,65 @@ export class RedBlackTree<
|
|
|
479
451
|
}
|
|
480
452
|
}
|
|
481
453
|
|
|
454
|
+
/**
|
|
455
|
+
* Time Complexity: O(log n)
|
|
456
|
+
* Space Complexity: O(1)
|
|
457
|
+
*/
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Time Complexity: O(log n)
|
|
461
|
+
* Space Complexity: O(1)
|
|
462
|
+
*
|
|
463
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
464
|
+
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
465
|
+
* red-black tree.
|
|
466
|
+
*/
|
|
467
|
+
protected _fixInsert(k: NODE): void {
|
|
468
|
+
let u: NODE | undefined;
|
|
469
|
+
while (k.parent && k.parent.color === 1) {
|
|
470
|
+
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
471
|
+
u = k.parent.parent.left;
|
|
472
|
+
if (u && u.color === 1) {
|
|
473
|
+
u.color = RBTNColor.BLACK;
|
|
474
|
+
k.parent.color = RBTNColor.BLACK;
|
|
475
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
476
|
+
k = k.parent.parent;
|
|
477
|
+
} else {
|
|
478
|
+
if (k === k.parent.left) {
|
|
479
|
+
k = k.parent;
|
|
480
|
+
this._rightRotate(k);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
k.parent!.color = RBTNColor.BLACK;
|
|
484
|
+
k.parent!.parent!.color = RBTNColor.RED;
|
|
485
|
+
this._leftRotate(k.parent!.parent!);
|
|
486
|
+
}
|
|
487
|
+
} else {
|
|
488
|
+
u = k.parent.parent!.right;
|
|
489
|
+
|
|
490
|
+
if (u && u.color === 1) {
|
|
491
|
+
u.color = RBTNColor.BLACK;
|
|
492
|
+
k.parent.color = RBTNColor.BLACK;
|
|
493
|
+
k.parent.parent!.color = RBTNColor.RED;
|
|
494
|
+
k = k.parent.parent!;
|
|
495
|
+
} else {
|
|
496
|
+
if (k === k.parent.right) {
|
|
497
|
+
k = k.parent;
|
|
498
|
+
this._leftRotate(k);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
k.parent!.color = RBTNColor.BLACK;
|
|
502
|
+
k.parent!.parent!.color = RBTNColor.RED;
|
|
503
|
+
this._rightRotate(k.parent!.parent!);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
if (k === this.root) {
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
this.root.color = RBTNColor.BLACK;
|
|
511
|
+
}
|
|
512
|
+
|
|
482
513
|
/**
|
|
483
514
|
* Time Complexity: O(log n)
|
|
484
515
|
* Space Complexity: O(1)
|
|
@@ -491,8 +522,8 @@ export class RedBlackTree<
|
|
|
491
522
|
* The function `_fixDelete` is used to fix the red-black tree after a node deletion.
|
|
492
523
|
* @param {RedBlackTreeNode} x - The parameter `x` represents a node in a Red-Black Tree (RBT).
|
|
493
524
|
*/
|
|
494
|
-
protected _fixDelete(x:
|
|
495
|
-
let s:
|
|
525
|
+
protected _fixDelete(x: NODE): void {
|
|
526
|
+
let s: NODE | undefined;
|
|
496
527
|
while (x !== this.root && x.color === RBTNColor.BLACK) {
|
|
497
528
|
if (x.parent && x === x.parent.left) {
|
|
498
529
|
s = x.parent.right!;
|
|
@@ -564,7 +595,7 @@ export class RedBlackTree<
|
|
|
564
595
|
* @param {RedBlackTreeNode} u - The parameter "u" represents a RedBlackTreeNode object.
|
|
565
596
|
* @param {RedBlackTreeNode} v - The parameter "v" is a RedBlackTreeNode object.
|
|
566
597
|
*/
|
|
567
|
-
protected _rbTransplant(u:
|
|
598
|
+
protected _rbTransplant(u: NODE, v: NODE): void {
|
|
568
599
|
if (u.parent === undefined) {
|
|
569
600
|
this._setRoot(v);
|
|
570
601
|
} else if (u === u.parent.left) {
|
|
@@ -575,75 +606,16 @@ export class RedBlackTree<
|
|
|
575
606
|
v.parent = u.parent;
|
|
576
607
|
}
|
|
577
608
|
|
|
578
|
-
/**
|
|
579
|
-
* Time Complexity: O(log n)
|
|
580
|
-
* Space Complexity: O(1)
|
|
581
|
-
*/
|
|
582
|
-
|
|
583
|
-
/**
|
|
584
|
-
* Time Complexity: O(log n)
|
|
585
|
-
* Space Complexity: O(1)
|
|
586
|
-
*
|
|
587
|
-
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
588
|
-
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
589
|
-
* red-black tree.
|
|
590
|
-
*/
|
|
591
|
-
protected _fixInsert(k: N): void {
|
|
592
|
-
let u: N | undefined;
|
|
593
|
-
while (k.parent && k.parent.color === 1) {
|
|
594
|
-
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
595
|
-
u = k.parent.parent.left;
|
|
596
|
-
if (u && u.color === 1) {
|
|
597
|
-
u.color = RBTNColor.BLACK;
|
|
598
|
-
k.parent.color = RBTNColor.BLACK;
|
|
599
|
-
k.parent.parent.color = RBTNColor.RED;
|
|
600
|
-
k = k.parent.parent;
|
|
601
|
-
} else {
|
|
602
|
-
if (k === k.parent.left) {
|
|
603
|
-
k = k.parent;
|
|
604
|
-
this._rightRotate(k);
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
k.parent!.color = RBTNColor.BLACK;
|
|
608
|
-
k.parent!.parent!.color = RBTNColor.RED;
|
|
609
|
-
this._leftRotate(k.parent!.parent!);
|
|
610
|
-
}
|
|
611
|
-
} else {
|
|
612
|
-
u = k.parent.parent!.right;
|
|
613
|
-
|
|
614
|
-
if (u && u.color === 1) {
|
|
615
|
-
u.color = RBTNColor.BLACK;
|
|
616
|
-
k.parent.color = RBTNColor.BLACK;
|
|
617
|
-
k.parent.parent!.color = RBTNColor.RED;
|
|
618
|
-
k = k.parent.parent!;
|
|
619
|
-
} else {
|
|
620
|
-
if (k === k.parent.right) {
|
|
621
|
-
k = k.parent;
|
|
622
|
-
this._leftRotate(k);
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
k.parent!.color = RBTNColor.BLACK;
|
|
626
|
-
k.parent!.parent!.color = RBTNColor.RED;
|
|
627
|
-
this._rightRotate(k.parent!.parent!);
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
if (k === this.root) {
|
|
631
|
-
break;
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
this.root.color = RBTNColor.BLACK;
|
|
635
|
-
}
|
|
636
|
-
|
|
637
609
|
/**
|
|
638
610
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
639
|
-
* @param {
|
|
640
|
-
* data structure. It is of type `
|
|
641
|
-
* @param {
|
|
611
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
612
|
+
* data structure. It is of type `NODE`, which is the type of the nodes in the data structure.
|
|
613
|
+
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
642
614
|
* data structure.
|
|
643
615
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
644
616
|
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
645
617
|
*/
|
|
646
|
-
protected _replaceNode(oldNode:
|
|
618
|
+
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
647
619
|
newNode.color = oldNode.color;
|
|
648
620
|
|
|
649
621
|
return super._replaceNode(oldNode, newNode);
|