linked-list-typed 1.53.8 → 1.54.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/base/iterable-entry-base.js +4 -4
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +34 -27
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +62 -52
- package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -14
- package/dist/data-structures/binary-tree/avl-tree.js +22 -25
- package/dist/data-structures/binary-tree/binary-tree.d.ts +32 -16
- package/dist/data-structures/binary-tree/binary-tree.js +43 -24
- package/dist/data-structures/binary-tree/bst.d.ts +14 -23
- package/dist/data-structures/binary-tree/bst.js +28 -29
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +26 -16
- package/dist/data-structures/binary-tree/red-black-tree.js +43 -59
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +18 -14
- package/dist/data-structures/binary-tree/tree-multi-map.js +36 -23
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/data-structures/hash/hash-map.js +5 -5
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +10 -10
- package/dist/data-structures/linked-list/doubly-linked-list.js +12 -12
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +10 -10
- package/dist/data-structures/linked-list/singly-linked-list.js +16 -16
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -4
- package/package.json +2 -2
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +83 -64
- package/src/data-structures/binary-tree/avl-tree.ts +40 -34
- package/src/data-structures/binary-tree/binary-tree.ts +74 -30
- package/src/data-structures/binary-tree/bst.ts +57 -43
- package/src/data-structures/binary-tree/red-black-tree.ts +66 -70
- package/src/data-structures/binary-tree/tree-multi-map.ts +56 -30
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +7 -7
- package/src/data-structures/linked-list/doubly-linked-list.ts +13 -13
- package/src/data-structures/linked-list/singly-linked-list.ts +17 -17
- package/src/interfaces/binary-tree.ts +4 -1
- package/src/types/data-structures/base/base.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +3 -3
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -4
|
@@ -2,9 +2,10 @@ import type {
|
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
3
|
BTNRep,
|
|
4
4
|
CRUD,
|
|
5
|
+
EntryCallback,
|
|
5
6
|
OptNode,
|
|
6
7
|
RBTNColor,
|
|
7
|
-
|
|
8
|
+
RedBlackTreeOptions,
|
|
8
9
|
RedBlackTreeNested,
|
|
9
10
|
RedBlackTreeNodeNested
|
|
10
11
|
} from '../../types';
|
|
@@ -31,24 +32,6 @@ export class RedBlackTreeNode<
|
|
|
31
32
|
super(key, value);
|
|
32
33
|
this._color = color;
|
|
33
34
|
}
|
|
34
|
-
|
|
35
|
-
protected _color: RBTNColor;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* The function returns the color value of a variable.
|
|
39
|
-
* @returns The color value stored in the private variable `_color`.
|
|
40
|
-
*/
|
|
41
|
-
get color(): RBTNColor {
|
|
42
|
-
return this._color;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* The function sets the color property to the specified value.
|
|
47
|
-
* @param {RBTNColor} value - The value parameter is of type RBTNColor.
|
|
48
|
-
*/
|
|
49
|
-
set color(value: RBTNColor) {
|
|
50
|
-
this._color = value;
|
|
51
|
-
}
|
|
52
35
|
}
|
|
53
36
|
|
|
54
37
|
/**
|
|
@@ -108,11 +91,23 @@ export class RedBlackTree<
|
|
|
108
91
|
K = any,
|
|
109
92
|
V = any,
|
|
110
93
|
R = object,
|
|
94
|
+
MK = any,
|
|
95
|
+
MV = any,
|
|
96
|
+
MR = object,
|
|
111
97
|
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
|
|
112
|
-
TREE extends RedBlackTree<K, V, R,
|
|
98
|
+
TREE extends RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE> = RedBlackTree<
|
|
99
|
+
K,
|
|
100
|
+
V,
|
|
101
|
+
R,
|
|
102
|
+
MK,
|
|
103
|
+
MV,
|
|
104
|
+
MR,
|
|
105
|
+
NODE,
|
|
106
|
+
RedBlackTreeNested<K, V, R, MK, MV, MR, NODE>
|
|
107
|
+
>
|
|
113
108
|
>
|
|
114
|
-
extends BST<K, V, R, NODE, TREE>
|
|
115
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
109
|
+
extends BST<K, V, R, MK, MV, MR, NODE, TREE>
|
|
110
|
+
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
116
111
|
{
|
|
117
112
|
/**
|
|
118
113
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
@@ -120,11 +115,11 @@ export class RedBlackTree<
|
|
|
120
115
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
121
116
|
* initialize the RBTree with the provided elements.
|
|
122
117
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
123
|
-
* constructor. It is of type `
|
|
118
|
+
* constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for
|
|
124
119
|
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
125
120
|
* depend on the implementation
|
|
126
121
|
*/
|
|
127
|
-
constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?:
|
|
122
|
+
constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?: RedBlackTreeOptions<K, V, R>) {
|
|
128
123
|
super([], options);
|
|
129
124
|
|
|
130
125
|
this._root = this.NIL;
|
|
@@ -168,11 +163,11 @@ export class RedBlackTree<
|
|
|
168
163
|
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
169
164
|
* @returns a new instance of a RedBlackTree object.
|
|
170
165
|
*/
|
|
171
|
-
override createTree(options?:
|
|
172
|
-
return new RedBlackTree<K, V, R, NODE, TREE>([], {
|
|
166
|
+
override createTree(options?: RedBlackTreeOptions<K, V, R>): TREE {
|
|
167
|
+
return new RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
173
168
|
iterationType: this.iterationType,
|
|
174
169
|
isMapMode: this._isMapMode,
|
|
175
|
-
|
|
170
|
+
specifyComparable: this._specifyComparable,
|
|
176
171
|
toEntryFn: this._toEntryFn,
|
|
177
172
|
...options
|
|
178
173
|
}) as TREE;
|
|
@@ -192,42 +187,6 @@ export class RedBlackTree<
|
|
|
192
187
|
return keyNodeEntryOrRaw instanceof RedBlackTreeNode;
|
|
193
188
|
}
|
|
194
189
|
|
|
195
|
-
// /**
|
|
196
|
-
// * Time Complexity: O(1)
|
|
197
|
-
// * Space Complexity: O(1)
|
|
198
|
-
// */
|
|
199
|
-
//
|
|
200
|
-
// /**
|
|
201
|
-
// * Time Complexity: O(1)
|
|
202
|
-
// * Space Complexity: O(1)
|
|
203
|
-
// *
|
|
204
|
-
// * The function `keyValueNodeEntryRawToNodeAndValue` takes a key, value, or entry and returns a node if it is
|
|
205
|
-
// * valid, otherwise it returns undefined.
|
|
206
|
-
// * @param {BTNRep<K, V, NODE>} keyNodeEntryOrRaw - The key, value, or entry to convert.
|
|
207
|
-
// * @param {V} [value] - The value associated with the key (if `keyNodeEntryOrRaw` is a key).
|
|
208
|
-
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
209
|
-
// */
|
|
210
|
-
// override keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): NODE | undefined {
|
|
211
|
-
//
|
|
212
|
-
// if (keyNodeEntryOrRaw === null || keyNodeEntryOrRaw === undefined) return;
|
|
213
|
-
// if (this.isNode(keyNodeEntryOrRaw)) return keyNodeEntryOrRaw;
|
|
214
|
-
//
|
|
215
|
-
// if (this._toEntryFn) {
|
|
216
|
-
// const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
|
|
217
|
-
// if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'RED');
|
|
218
|
-
// }
|
|
219
|
-
//
|
|
220
|
-
// if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
221
|
-
// const [key, value] = keyNodeEntryOrRaw;
|
|
222
|
-
// if (key === undefined || key === null) return;
|
|
223
|
-
// else return this.createNode(key, value, 'RED');
|
|
224
|
-
// }
|
|
225
|
-
//
|
|
226
|
-
// if (this.isKey(keyNodeEntryOrRaw)) return this.createNode(keyNodeEntryOrRaw, value, 'RED');
|
|
227
|
-
//
|
|
228
|
-
// return ;
|
|
229
|
-
// }
|
|
230
|
-
|
|
231
190
|
/**
|
|
232
191
|
* Time Complexity: O(1)
|
|
233
192
|
* Space Complexity: O(1)
|
|
@@ -309,8 +268,10 @@ export class RedBlackTree<
|
|
|
309
268
|
let replacementNode: NODE | undefined;
|
|
310
269
|
|
|
311
270
|
if (!this.isRealNode(nodeToDelete.left)) {
|
|
312
|
-
|
|
313
|
-
|
|
271
|
+
if (nodeToDelete.right !== null) {
|
|
272
|
+
replacementNode = nodeToDelete.right;
|
|
273
|
+
this._transplant(nodeToDelete, nodeToDelete.right);
|
|
274
|
+
}
|
|
314
275
|
} else if (!this.isRealNode(nodeToDelete.right)) {
|
|
315
276
|
replacementNode = nodeToDelete.left;
|
|
316
277
|
this._transplant(nodeToDelete, nodeToDelete.left);
|
|
@@ -318,15 +279,17 @@ export class RedBlackTree<
|
|
|
318
279
|
const successor = this.getLeftMost(node => node, nodeToDelete.right);
|
|
319
280
|
if (successor) {
|
|
320
281
|
originalColor = successor.color;
|
|
321
|
-
replacementNode = successor.right;
|
|
282
|
+
if (successor.right !== null) replacementNode = successor.right;
|
|
322
283
|
|
|
323
284
|
if (successor.parent === nodeToDelete) {
|
|
324
285
|
if (this.isRealNode(replacementNode)) {
|
|
325
286
|
replacementNode.parent = successor;
|
|
326
287
|
}
|
|
327
288
|
} else {
|
|
328
|
-
|
|
329
|
-
|
|
289
|
+
if (successor.right !== null) {
|
|
290
|
+
this._transplant(successor, successor.right);
|
|
291
|
+
successor.right = nodeToDelete.right;
|
|
292
|
+
}
|
|
330
293
|
if (this.isRealNode(successor.right)) {
|
|
331
294
|
successor.right.parent = successor;
|
|
332
295
|
}
|
|
@@ -419,7 +382,7 @@ export class RedBlackTree<
|
|
|
419
382
|
|
|
420
383
|
if (!parent) {
|
|
421
384
|
this._setRoot(node);
|
|
422
|
-
} else if (node.key
|
|
385
|
+
} else if (this._compare(node.key, parent.key) < 0) {
|
|
423
386
|
parent.left = node;
|
|
424
387
|
} else {
|
|
425
388
|
parent.right = node;
|
|
@@ -497,7 +460,7 @@ export class RedBlackTree<
|
|
|
497
460
|
} else {
|
|
498
461
|
// Symmetric case for the right child (left and right exchanged)
|
|
499
462
|
// Follow the same logic as above with left and right exchanged
|
|
500
|
-
const y: NODE | undefined = z?.parent?.parent?.left;
|
|
463
|
+
const y: NODE | undefined = z?.parent?.parent?.left ?? undefined;
|
|
501
464
|
if (y?.color === 'RED') {
|
|
502
465
|
z.parent.color = 'BLACK';
|
|
503
466
|
y.color = 'BLACK';
|
|
@@ -674,4 +637,37 @@ export class RedBlackTree<
|
|
|
674
637
|
x.right = y;
|
|
675
638
|
y.parent = x;
|
|
676
639
|
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* Time Complexity: O(n)
|
|
643
|
+
* Space Complexity: O(n)
|
|
644
|
+
*
|
|
645
|
+
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
|
|
646
|
+
* applying a callback to each entry in the original tree.
|
|
647
|
+
* @param callback - A function that will be called for each entry in the tree, with parameters
|
|
648
|
+
* representing the key, value, index, and the tree itself. It should return an entry for the new
|
|
649
|
+
* tree.
|
|
650
|
+
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
|
|
651
|
+
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
|
|
652
|
+
* Tree that will be created during the mapping process. These options could include things like
|
|
653
|
+
* custom comparators
|
|
654
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
655
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
656
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
657
|
+
* or
|
|
658
|
+
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
659
|
+
* provided callback function.
|
|
660
|
+
*/
|
|
661
|
+
override map(
|
|
662
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
663
|
+
options?: RedBlackTreeOptions<MK, MV, MR>,
|
|
664
|
+
thisArg?: any
|
|
665
|
+
): RedBlackTree<MK, MV, MR> {
|
|
666
|
+
const newTree = new RedBlackTree<MK, MV, MR>([], options);
|
|
667
|
+
let index = 0;
|
|
668
|
+
for (const [key, value] of this) {
|
|
669
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
670
|
+
}
|
|
671
|
+
return newTree;
|
|
672
|
+
}
|
|
677
673
|
}
|
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
10
|
BSTNOptKeyOrNode,
|
|
11
11
|
BTNRep,
|
|
12
|
+
EntryCallback,
|
|
12
13
|
IterationType,
|
|
13
14
|
OptNode,
|
|
14
15
|
RBTNColor,
|
|
@@ -40,36 +41,29 @@ export class TreeMultiMapNode<
|
|
|
40
41
|
super(key, value, color);
|
|
41
42
|
this.count = count;
|
|
42
43
|
}
|
|
43
|
-
|
|
44
|
-
protected _count: number = 1;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* The function returns the value of the private variable _count.
|
|
48
|
-
* @returns The count property of the object, which is of type number.
|
|
49
|
-
*/
|
|
50
|
-
get count(): number {
|
|
51
|
-
return this._count;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* The above function sets the value of the count property.
|
|
56
|
-
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
57
|
-
* numeric value.
|
|
58
|
-
*/
|
|
59
|
-
set count(value: number) {
|
|
60
|
-
this._count = value;
|
|
61
|
-
}
|
|
62
44
|
}
|
|
63
45
|
|
|
64
46
|
export class TreeMultiMap<
|
|
65
47
|
K = any,
|
|
66
48
|
V = any,
|
|
67
49
|
R = object,
|
|
50
|
+
MK = any,
|
|
51
|
+
MV = any,
|
|
52
|
+
MR = object,
|
|
68
53
|
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
|
|
69
|
-
TREE extends TreeMultiMap<K, V, R,
|
|
54
|
+
TREE extends TreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = TreeMultiMap<
|
|
55
|
+
K,
|
|
56
|
+
V,
|
|
57
|
+
R,
|
|
58
|
+
MK,
|
|
59
|
+
MV,
|
|
60
|
+
MR,
|
|
61
|
+
NODE,
|
|
62
|
+
TreeMultiMapNested<K, V, R, MK, MV, MR, NODE>
|
|
63
|
+
>
|
|
70
64
|
>
|
|
71
|
-
extends RedBlackTree<K, V, R, NODE, TREE>
|
|
72
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
65
|
+
extends RedBlackTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
66
|
+
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
73
67
|
{
|
|
74
68
|
/**
|
|
75
69
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
@@ -136,10 +130,10 @@ export class TreeMultiMap<
|
|
|
136
130
|
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
137
131
|
*/
|
|
138
132
|
override createTree(options?: TreeMultiMapOptions<K, V, R>): TREE {
|
|
139
|
-
return new TreeMultiMap<K, V, R, NODE, TREE>([], {
|
|
133
|
+
return new TreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
140
134
|
iterationType: this.iterationType,
|
|
141
135
|
isMapMode: this._isMapMode,
|
|
142
|
-
|
|
136
|
+
specifyComparable: this._specifyComparable,
|
|
143
137
|
toEntryFn: this._toEntryFn,
|
|
144
138
|
...options
|
|
145
139
|
}) as TREE;
|
|
@@ -256,10 +250,12 @@ export class TreeMultiMap<
|
|
|
256
250
|
let replacementNode: NODE | undefined;
|
|
257
251
|
|
|
258
252
|
if (!this.isRealNode(nodeToDelete.left)) {
|
|
259
|
-
replacementNode = nodeToDelete.right;
|
|
253
|
+
if (nodeToDelete.right !== null) replacementNode = nodeToDelete.right;
|
|
260
254
|
if (ignoreCount || nodeToDelete.count <= 1) {
|
|
261
|
-
|
|
262
|
-
|
|
255
|
+
if (nodeToDelete.right !== null) {
|
|
256
|
+
this._transplant(nodeToDelete, nodeToDelete.right);
|
|
257
|
+
this._count -= nodeToDelete.count;
|
|
258
|
+
}
|
|
263
259
|
} else {
|
|
264
260
|
nodeToDelete.count--;
|
|
265
261
|
this._count--;
|
|
@@ -281,7 +277,7 @@ export class TreeMultiMap<
|
|
|
281
277
|
const successor = this.getLeftMost(node => node, nodeToDelete.right);
|
|
282
278
|
if (successor) {
|
|
283
279
|
originalColor = successor.color;
|
|
284
|
-
replacementNode = successor.right;
|
|
280
|
+
if (successor.right !== null) replacementNode = successor.right;
|
|
285
281
|
|
|
286
282
|
if (successor.parent === nodeToDelete) {
|
|
287
283
|
if (this.isRealNode(replacementNode)) {
|
|
@@ -289,8 +285,10 @@ export class TreeMultiMap<
|
|
|
289
285
|
}
|
|
290
286
|
} else {
|
|
291
287
|
if (ignoreCount || nodeToDelete.count <= 1) {
|
|
292
|
-
|
|
293
|
-
|
|
288
|
+
if (successor.right !== null) {
|
|
289
|
+
this._transplant(successor, successor.right);
|
|
290
|
+
this._count -= nodeToDelete.count;
|
|
291
|
+
}
|
|
294
292
|
} else {
|
|
295
293
|
nodeToDelete.count--;
|
|
296
294
|
this._count--;
|
|
@@ -466,4 +464,32 @@ export class TreeMultiMap<
|
|
|
466
464
|
newNode.count = oldNode.count + newNode.count;
|
|
467
465
|
return super._replaceNode(oldNode, newNode);
|
|
468
466
|
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with
|
|
470
|
+
* modified entries based on a provided callback.
|
|
471
|
+
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
472
|
+
* map. It takes four arguments:
|
|
473
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
474
|
+
* `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
|
|
475
|
+
* options when creating a new `TreeMultiMap` instance within the `map` function. These options could
|
|
476
|
+
* include things like
|
|
477
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
478
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
479
|
+
* (value of `this`) for the callback function when it is called within the `map` function. This
|
|
480
|
+
* @returns A new TreeMultiMap instance is being returned, which is populated with entries generated
|
|
481
|
+
* by the provided callback function.
|
|
482
|
+
*/
|
|
483
|
+
override map(
|
|
484
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
485
|
+
options?: TreeMultiMapOptions<MK, MV, MR>,
|
|
486
|
+
thisArg?: any
|
|
487
|
+
): TreeMultiMap<MK, MV, MR> {
|
|
488
|
+
const newTree = new TreeMultiMap<MK, MV, MR>([], options);
|
|
489
|
+
let index = 0;
|
|
490
|
+
for (const [key, value] of this) {
|
|
491
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
492
|
+
}
|
|
493
|
+
return newTree;
|
|
494
|
+
}
|
|
469
495
|
}
|
|
@@ -947,7 +947,7 @@ export abstract class AbstractGraph<
|
|
|
947
947
|
const filtered: [VertexKey, V | undefined][] = [];
|
|
948
948
|
let index = 0;
|
|
949
949
|
for (const [key, value] of this) {
|
|
950
|
-
if (predicate.call(thisArg,
|
|
950
|
+
if (predicate.call(thisArg, key, value, index, this)) {
|
|
951
951
|
filtered.push([key, value]);
|
|
952
952
|
}
|
|
953
953
|
index++;
|
|
@@ -972,7 +972,7 @@ export abstract class AbstractGraph<
|
|
|
972
972
|
const mapped: T[] = [];
|
|
973
973
|
let index = 0;
|
|
974
974
|
for (const [key, value] of this) {
|
|
975
|
-
mapped.push(callback.call(thisArg,
|
|
975
|
+
mapped.push(callback.call(thisArg, key, value, index, this));
|
|
976
976
|
index++;
|
|
977
977
|
}
|
|
978
978
|
return mapped;
|
|
@@ -287,7 +287,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
287
287
|
const resultMap = new HashMap<K, VM>();
|
|
288
288
|
let index = 0;
|
|
289
289
|
for (const [key, value] of this) {
|
|
290
|
-
resultMap.set(key, callbackfn.call(thisArg,
|
|
290
|
+
resultMap.set(key, callbackfn.call(thisArg, key, value, index++, this));
|
|
291
291
|
}
|
|
292
292
|
return resultMap;
|
|
293
293
|
}
|
|
@@ -312,7 +312,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
312
312
|
const filteredMap = new HashMap<K, V>();
|
|
313
313
|
let index = 0;
|
|
314
314
|
for (const [key, value] of this) {
|
|
315
|
-
if (predicate.call(thisArg,
|
|
315
|
+
if (predicate.call(thisArg, key, value, index++, this)) {
|
|
316
316
|
filteredMap.set(key, value);
|
|
317
317
|
}
|
|
318
318
|
}
|
|
@@ -826,7 +826,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
826
826
|
const filteredMap = new LinkedHashMap<K, V>();
|
|
827
827
|
let index = 0;
|
|
828
828
|
for (const [key, value] of this) {
|
|
829
|
-
if (predicate.call(thisArg,
|
|
829
|
+
if (predicate.call(thisArg, key, value, index, this)) {
|
|
830
830
|
filteredMap.set(key, value);
|
|
831
831
|
}
|
|
832
832
|
index++;
|
|
@@ -851,12 +851,12 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
851
851
|
* @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
|
|
852
852
|
* function.
|
|
853
853
|
*/
|
|
854
|
-
map<
|
|
855
|
-
const mappedMap = new LinkedHashMap<
|
|
854
|
+
map<MK, MV>(callback: EntryCallback<K, V, [MK, MV]>, thisArg?: any): LinkedHashMap<MK, MV> {
|
|
855
|
+
const mappedMap = new LinkedHashMap<MK, MV>();
|
|
856
856
|
let index = 0;
|
|
857
857
|
for (const [key, value] of this) {
|
|
858
|
-
const newValue = callback.call(thisArg,
|
|
859
|
-
mappedMap.set(
|
|
858
|
+
const [newKey, newValue] = callback.call(thisArg, key, value, index, this);
|
|
859
|
+
mappedMap.set(newKey, newValue);
|
|
860
860
|
index++;
|
|
861
861
|
}
|
|
862
862
|
return mappedMap;
|
|
@@ -588,6 +588,19 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
588
588
|
return this.tail?.value;
|
|
589
589
|
}
|
|
590
590
|
|
|
591
|
+
/**
|
|
592
|
+
* Time Complexity: O(n)
|
|
593
|
+
* Space Complexity: O(n)
|
|
594
|
+
*
|
|
595
|
+
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
596
|
+
* given array.
|
|
597
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
598
|
+
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
|
599
|
+
*/
|
|
600
|
+
static fromArray<E>(data: E[]) {
|
|
601
|
+
return new DoublyLinkedList<E>(data);
|
|
602
|
+
}
|
|
603
|
+
|
|
591
604
|
/**
|
|
592
605
|
* Time Complexity: O(1)
|
|
593
606
|
* Space Complexity: O(1)
|
|
@@ -1252,19 +1265,6 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
1252
1265
|
return count;
|
|
1253
1266
|
}
|
|
1254
1267
|
|
|
1255
|
-
/**
|
|
1256
|
-
* Time Complexity: O(n)
|
|
1257
|
-
* Space Complexity: O(n)
|
|
1258
|
-
*
|
|
1259
|
-
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
1260
|
-
* given array.
|
|
1261
|
-
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
1262
|
-
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
|
1263
|
-
*/
|
|
1264
|
-
static fromArray<E>(data: E[]) {
|
|
1265
|
-
return new DoublyLinkedList<E>(data);
|
|
1266
|
-
}
|
|
1267
|
-
|
|
1268
1268
|
/**
|
|
1269
1269
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
1270
1270
|
*/
|
|
@@ -116,6 +116,23 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
116
116
|
return this._size;
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Time Complexity: O(n)
|
|
121
|
+
* Space Complexity: O(n)
|
|
122
|
+
*
|
|
123
|
+
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
124
|
+
* array.
|
|
125
|
+
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
126
|
+
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
127
|
+
*/
|
|
128
|
+
static fromArray<E>(data: E[]) {
|
|
129
|
+
const singlyLinkedList = new SinglyLinkedList<E>();
|
|
130
|
+
for (const item of data) {
|
|
131
|
+
singlyLinkedList.push(item);
|
|
132
|
+
}
|
|
133
|
+
return singlyLinkedList;
|
|
134
|
+
}
|
|
135
|
+
|
|
119
136
|
/**
|
|
120
137
|
* Time Complexity: O(1)
|
|
121
138
|
* Space Complexity: O(1)
|
|
@@ -763,23 +780,6 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
763
780
|
}
|
|
764
781
|
}
|
|
765
782
|
|
|
766
|
-
/**
|
|
767
|
-
* Time Complexity: O(n)
|
|
768
|
-
* Space Complexity: O(n)
|
|
769
|
-
*
|
|
770
|
-
* The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
|
|
771
|
-
* array.
|
|
772
|
-
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
773
|
-
* @returns The `fromArray` function returns a `SinglyLinkedList` object.
|
|
774
|
-
*/
|
|
775
|
-
static fromArray<E>(data: E[]) {
|
|
776
|
-
const singlyLinkedList = new SinglyLinkedList<E>();
|
|
777
|
-
for (const item of data) {
|
|
778
|
-
singlyLinkedList.push(item);
|
|
779
|
-
}
|
|
780
|
-
return singlyLinkedList;
|
|
781
|
-
}
|
|
782
|
-
|
|
783
783
|
/**
|
|
784
784
|
* The _isPredicate function in TypeScript checks if the input is a function that takes a
|
|
785
785
|
* SinglyLinkedListNode as an argument and returns a boolean.
|
|
@@ -12,8 +12,11 @@ export interface IBinaryTree<
|
|
|
12
12
|
K = any,
|
|
13
13
|
V = any,
|
|
14
14
|
R = object,
|
|
15
|
+
MK = any,
|
|
16
|
+
MV = any,
|
|
17
|
+
MR = object,
|
|
15
18
|
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNodeNested<K, V>,
|
|
16
|
-
TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTreeNested<K, V, R, NODE>
|
|
19
|
+
TREE extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> = BinaryTreeNested<K, V, R, MK, MV, MR, NODE>
|
|
17
20
|
> {
|
|
18
21
|
createNode(key: K, value?: NODE['value']): NODE;
|
|
19
22
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { IterableElementBase, IterableEntryBase } from '../../../data-structures';
|
|
2
2
|
|
|
3
|
-
export type EntryCallback<K, V, R> = (
|
|
3
|
+
export type EntryCallback<K, V, R> = (key: K, value: V, index: number, container: IterableEntryBase<K, V>) => R;
|
|
4
4
|
export type ElementCallback<E, R, RT, C> = (element: E, index: number, container: IterableElementBase<E, R, C>) => RT;
|
|
5
5
|
export type ReduceEntryCallback<K, V, R> = (
|
|
6
6
|
accumulator: R,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { AVLTreeMultiMap, AVLTreeMultiMapNode } from '../../../data-structures';
|
|
2
2
|
import type { AVLTreeOptions } from './avl-tree';
|
|
3
3
|
|
|
4
|
-
export type AVLTreeMultiMapNodeNested<K, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V,
|
|
4
|
+
export type AVLTreeMultiMapNodeNested<K, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, any>>>
|
|
5
5
|
|
|
6
|
-
export type AVLTreeMultiMapNested<K, V, R,
|
|
6
|
+
export type AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE extends AVLTreeMultiMapNode<K, V, NODE>> = AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, any>>>
|
|
7
7
|
|
|
8
8
|
export type AVLTreeMultiMapOptions<K, V, R> = AVLTreeOptions<K, V, R> & {}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { AVLTree, AVLTreeNode } from '../../../data-structures';
|
|
2
2
|
import { BSTOptions } from './bst';
|
|
3
3
|
|
|
4
|
-
export type AVLTreeNodeNested<K, V> = AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V,
|
|
4
|
+
export type AVLTreeNodeNested<K, V> = AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTreeNode<K, V, any>>>
|
|
5
5
|
|
|
6
|
-
export type AVLTreeNested<K, V, R, NODE extends AVLTreeNode<K, V, NODE>> = AVLTree<K, V, R,
|
|
6
|
+
export type AVLTreeNested<K, V, R,MK, MV, MR, NODE extends AVLTreeNode<K, V, NODE>> = AVLTree<K, V, R,MK, MV, MR, NODE, AVLTree<K, V, R,MK, MV, MR, NODE, AVLTree<K, V, R,MK, MV, MR, NODE, any>>>
|
|
7
7
|
|
|
8
8
|
export type AVLTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
|
|
@@ -2,9 +2,9 @@ import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
|
|
|
2
2
|
import { IterationType, OptValue } from '../../common';
|
|
3
3
|
import { DFSOperation } from '../../../common';
|
|
4
4
|
|
|
5
|
-
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V,
|
|
5
|
+
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>
|
|
6
6
|
|
|
7
|
-
export type BinaryTreeNested<K, V, R,
|
|
7
|
+
export type BinaryTreeNested<K, V, R, MK, MV, MR, NODE extends BinaryTreeNode<K, V, NODE>> = BinaryTree<K, V, R, MK, MV, MR, NODE,BinaryTree<K, V, R, MK, MV, MR, NODE,BinaryTree<K, V, R, MK, MV, MR, NODE,any>>>
|
|
8
8
|
|
|
9
9
|
export type ToEntryFn<K, V, R> = (rawElement: R) => BTNEntry<K, V>;
|
|
10
10
|
|
|
@@ -2,12 +2,12 @@ import { BST, BSTNode } from '../../../data-structures';
|
|
|
2
2
|
import type { BinaryTreeOptions } from './binary-tree';
|
|
3
3
|
import { Comparable } from '../../utils';
|
|
4
4
|
|
|
5
|
-
export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V,
|
|
5
|
+
export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>>
|
|
6
6
|
|
|
7
|
-
export type BSTNested<K, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R,
|
|
7
|
+
export type BSTNested<K, V, R,MK, MV, MR, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R,MK, MV, MR, NODE,BST<K, V, R,MK, MV, MR, NODE,BST<K, V, R,MK, MV, MR, NODE, any>>>
|
|
8
8
|
|
|
9
9
|
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
|
|
10
|
-
|
|
10
|
+
specifyComparable?: (key: K) => Comparable
|
|
11
11
|
isReverse?: boolean;
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -3,8 +3,8 @@ import type { BSTOptions } from "./bst";
|
|
|
3
3
|
|
|
4
4
|
export type RBTNColor = 'RED' | 'BLACK';
|
|
5
5
|
|
|
6
|
-
export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V,
|
|
6
|
+
export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>>
|
|
7
7
|
|
|
8
|
-
export type RedBlackTreeNested<K, V, R,
|
|
8
|
+
export type RedBlackTreeNested<K, V, R, MK, MV, MR, NODE extends RedBlackTreeNode<K, V, NODE>> = RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTree<K, V, R, MK, MV, MR, NODE, RedBlackTree<K, V, R, MK, MV, MR, NODE, any>>>
|
|
9
9
|
|
|
10
|
-
export type
|
|
10
|
+
export type RedBlackTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
|