min-heap-typed 1.53.8 → 1.53.9
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 +36 -17
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +64 -35
- package/dist/data-structures/binary-tree/avl-tree.d.ts +12 -8
- package/dist/data-structures/binary-tree/avl-tree.js +19 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +53 -40
- package/dist/data-structures/binary-tree/binary-tree.js +75 -71
- package/dist/data-structures/binary-tree/bst.d.ts +35 -30
- package/dist/data-structures/binary-tree/bst.js +54 -40
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +34 -10
- package/dist/data-structures/binary-tree/red-black-tree.js +41 -43
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +33 -17
- package/dist/data-structures/binary-tree/tree-multi-map.js +62 -36
- 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 +3 -4
- 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 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -5
- 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 +81 -54
- package/src/data-structures/binary-tree/avl-tree.ts +32 -15
- package/src/data-structures/binary-tree/binary-tree.ts +88 -83
- package/src/data-structures/binary-tree/bst.ts +87 -74
- package/src/data-structures/binary-tree/red-black-tree.ts +55 -54
- package/src/data-structures/binary-tree/tree-multi-map.ts +79 -49
- 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 +3 -13
- package/src/types/data-structures/base/base.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/bst.ts +3 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -6
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -6
|
@@ -5,8 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import
|
|
9
|
-
BSTNested,
|
|
8
|
+
import {
|
|
10
9
|
BSTNodeNested,
|
|
11
10
|
BSTNOptKeyOrNode,
|
|
12
11
|
BSTOptions,
|
|
@@ -15,6 +14,7 @@ import type {
|
|
|
15
14
|
Comparator,
|
|
16
15
|
CP,
|
|
17
16
|
DFSOrderPattern,
|
|
17
|
+
EntryCallback,
|
|
18
18
|
IterationType,
|
|
19
19
|
NodeCallback,
|
|
20
20
|
NodePredicate,
|
|
@@ -151,15 +151,9 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
151
151
|
* console.log(findLCA(5, 35)); // 15
|
|
152
152
|
* console.log(findLCA(20, 30)); // 25
|
|
153
153
|
*/
|
|
154
|
-
export class BST<
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
R = object,
|
|
158
|
-
NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
|
|
159
|
-
TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>
|
|
160
|
-
>
|
|
161
|
-
extends BinaryTree<K, V, R, NODE, TREE>
|
|
162
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
154
|
+
export class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>>
|
|
155
|
+
extends BinaryTree<K, V, R, NODE>
|
|
156
|
+
implements IBinaryTree<K, V, R, NODE>
|
|
163
157
|
{
|
|
164
158
|
/**
|
|
165
159
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
@@ -173,8 +167,8 @@ export class BST<
|
|
|
173
167
|
super([], options);
|
|
174
168
|
|
|
175
169
|
if (options) {
|
|
176
|
-
const {
|
|
177
|
-
if (typeof
|
|
170
|
+
const { specifyComparable, isReverse } = options;
|
|
171
|
+
if (typeof specifyComparable === 'function') this._specifyComparable = specifyComparable;
|
|
178
172
|
if (isReverse !== undefined) this._isReverse = isReverse;
|
|
179
173
|
}
|
|
180
174
|
|
|
@@ -202,6 +196,45 @@ export class BST<
|
|
|
202
196
|
return this._isReverse;
|
|
203
197
|
}
|
|
204
198
|
|
|
199
|
+
protected _comparator: Comparator<K> = (a: K, b: K): number => {
|
|
200
|
+
if (isComparable(a) && isComparable(b)) {
|
|
201
|
+
if (a > b) return 1;
|
|
202
|
+
if (a < b) return -1;
|
|
203
|
+
return 0;
|
|
204
|
+
}
|
|
205
|
+
if (this._specifyComparable) {
|
|
206
|
+
if (this._specifyComparable(a) > this._specifyComparable(b)) return 1;
|
|
207
|
+
if (this._specifyComparable(a) < this._specifyComparable(b)) return -1;
|
|
208
|
+
return 0;
|
|
209
|
+
}
|
|
210
|
+
if (typeof a === 'object' || typeof b === 'object') {
|
|
211
|
+
throw TypeError(
|
|
212
|
+
`When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return 0;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* The function returns the value of the _comparator property.
|
|
221
|
+
* @returns The `_comparator` property is being returned.
|
|
222
|
+
*/
|
|
223
|
+
get comparator() {
|
|
224
|
+
return this._comparator;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
protected _specifyComparable?: (key: K) => Comparable;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* This function returns the value of the `_specifyComparable` property.
|
|
231
|
+
* @returns The method `specifyComparable()` is being returned, which is a getter method for the
|
|
232
|
+
* `_specifyComparable` property.
|
|
233
|
+
*/
|
|
234
|
+
get specifyComparable() {
|
|
235
|
+
return this._specifyComparable;
|
|
236
|
+
}
|
|
237
|
+
|
|
205
238
|
/**
|
|
206
239
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
207
240
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -215,39 +248,26 @@ export class BST<
|
|
|
215
248
|
}
|
|
216
249
|
|
|
217
250
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
251
|
+
* Time Complexity: O(1)
|
|
252
|
+
* Space Complexity: O(1)
|
|
253
|
+
*
|
|
254
|
+
* The `createTree` function in TypeScript overrides the default options with the provided options to
|
|
255
|
+
* create a new Binary Search Tree.
|
|
256
|
+
* @param [options] - The `options` parameter in the `createTree` method is an optional object that
|
|
257
|
+
* can contain the following properties:
|
|
258
|
+
* @returns A new instance of a Binary Search Tree (BST) is being returned with the specified options
|
|
259
|
+
* and properties inherited from the current instance.
|
|
223
260
|
*/
|
|
224
|
-
|
|
225
|
-
|
|
261
|
+
// @ts-ignore
|
|
262
|
+
override createTree(options?: BSTOptions<K, V, R>) {
|
|
263
|
+
return new BST<K, V, R>([], {
|
|
226
264
|
iterationType: this.iterationType,
|
|
227
265
|
isMapMode: this._isMapMode,
|
|
228
|
-
|
|
266
|
+
specifyComparable: this._specifyComparable,
|
|
229
267
|
toEntryFn: this._toEntryFn,
|
|
230
268
|
isReverse: this._isReverse,
|
|
231
269
|
...options
|
|
232
|
-
})
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
237
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
238
|
-
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
239
|
-
* element.
|
|
240
|
-
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
241
|
-
* value associated with a key in a key-value pair.
|
|
242
|
-
* @returns either a NODE object or undefined.
|
|
243
|
-
*/
|
|
244
|
-
protected override _keyValueNodeEntryRawToNodeAndValue(
|
|
245
|
-
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
246
|
-
value?: V
|
|
247
|
-
): [OptNode<NODE>, V | undefined] {
|
|
248
|
-
const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
249
|
-
if (node === null) return [undefined, undefined];
|
|
250
|
-
return [node, value ?? entryValue];
|
|
270
|
+
});
|
|
251
271
|
}
|
|
252
272
|
|
|
253
273
|
/**
|
|
@@ -292,7 +312,7 @@ export class BST<
|
|
|
292
312
|
* this._DEFAULT_COMPARATOR`.
|
|
293
313
|
*/
|
|
294
314
|
override isKey(key: any): key is K {
|
|
295
|
-
return isComparable(key, this.
|
|
315
|
+
return isComparable(key, this._specifyComparable !== undefined);
|
|
296
316
|
}
|
|
297
317
|
|
|
298
318
|
/**
|
|
@@ -472,7 +492,7 @@ export class BST<
|
|
|
472
492
|
* @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
|
|
473
493
|
* value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
|
|
474
494
|
*/
|
|
475
|
-
override merge(anotherTree:
|
|
495
|
+
override merge(anotherTree: this) {
|
|
476
496
|
this.addMany(anotherTree, [], false);
|
|
477
497
|
}
|
|
478
498
|
|
|
@@ -924,43 +944,36 @@ export class BST<
|
|
|
924
944
|
return balanced;
|
|
925
945
|
}
|
|
926
946
|
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
}
|
|
938
|
-
if (typeof a === 'object' || typeof b === 'object') {
|
|
939
|
-
throw TypeError(
|
|
940
|
-
`When comparing object types, a custom extractComparable must be defined in the constructor's options parameter.`
|
|
941
|
-
);
|
|
947
|
+
// @ts-ignore
|
|
948
|
+
override map<MK, MV, MR>(
|
|
949
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
950
|
+
options?: BSTOptions<MK, MV, MR>,
|
|
951
|
+
thisArg?: any
|
|
952
|
+
) {
|
|
953
|
+
const newTree = new BST<MK, MV, MR>([], options);
|
|
954
|
+
let index = 0;
|
|
955
|
+
for (const [key, value] of this) {
|
|
956
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
942
957
|
}
|
|
943
|
-
|
|
944
|
-
return 0;
|
|
945
|
-
};
|
|
946
|
-
|
|
947
|
-
/**
|
|
948
|
-
* The function returns the value of the _comparator property.
|
|
949
|
-
* @returns The `_comparator` property is being returned.
|
|
950
|
-
*/
|
|
951
|
-
get comparator() {
|
|
952
|
-
return this._comparator;
|
|
958
|
+
return newTree;
|
|
953
959
|
}
|
|
954
960
|
|
|
955
|
-
protected _extractComparable?: (key: K) => Comparable;
|
|
956
|
-
|
|
957
961
|
/**
|
|
958
|
-
*
|
|
959
|
-
* @
|
|
960
|
-
*
|
|
962
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
963
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
964
|
+
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
965
|
+
* element.
|
|
966
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
967
|
+
* value associated with a key in a key-value pair.
|
|
968
|
+
* @returns either a NODE object or undefined.
|
|
961
969
|
*/
|
|
962
|
-
|
|
963
|
-
|
|
970
|
+
protected override _keyValueNodeEntryRawToNodeAndValue(
|
|
971
|
+
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
972
|
+
value?: V
|
|
973
|
+
): [OptNode<NODE>, V | undefined] {
|
|
974
|
+
const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
975
|
+
if (node === null) return [undefined, undefined];
|
|
976
|
+
return [node, value ?? entryValue];
|
|
964
977
|
}
|
|
965
978
|
|
|
966
979
|
/**
|
|
@@ -2,10 +2,10 @@ import type {
|
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
3
|
BTNRep,
|
|
4
4
|
CRUD,
|
|
5
|
+
EntryCallback,
|
|
5
6
|
OptNode,
|
|
6
7
|
RBTNColor,
|
|
7
|
-
|
|
8
|
-
RedBlackTreeNested,
|
|
8
|
+
RedBlackTreeOptions,
|
|
9
9
|
RedBlackTreeNodeNested
|
|
10
10
|
} from '../../types';
|
|
11
11
|
import { BST, BSTNode } from './bst';
|
|
@@ -108,23 +108,22 @@ export class RedBlackTree<
|
|
|
108
108
|
K = any,
|
|
109
109
|
V = any,
|
|
110
110
|
R = object,
|
|
111
|
-
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V
|
|
112
|
-
TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>
|
|
111
|
+
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>
|
|
113
112
|
>
|
|
114
|
-
extends BST<K, V, R, NODE
|
|
115
|
-
implements IBinaryTree<K, V, R, NODE
|
|
113
|
+
extends BST<K, V, R, NODE>
|
|
114
|
+
implements IBinaryTree<K, V, R, NODE>
|
|
116
115
|
{
|
|
117
116
|
/**
|
|
118
117
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
119
118
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
120
119
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
121
|
-
* initialize the
|
|
120
|
+
* initialize the RedBlackTree with the provided elements.
|
|
122
121
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
123
|
-
* constructor. It is of type `
|
|
122
|
+
* constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for
|
|
124
123
|
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
125
124
|
* depend on the implementation
|
|
126
125
|
*/
|
|
127
|
-
constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?:
|
|
126
|
+
constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?: RedBlackTreeOptions<K, V, R>) {
|
|
128
127
|
super([], options);
|
|
129
128
|
|
|
130
129
|
this._root = this.NIL;
|
|
@@ -163,19 +162,23 @@ export class RedBlackTree<
|
|
|
163
162
|
}
|
|
164
163
|
|
|
165
164
|
/**
|
|
166
|
-
* The function
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
165
|
+
* The function `createTree` overrides the default implementation to create a Red-Black Tree with
|
|
166
|
+
* specified options in TypeScript.
|
|
167
|
+
* @param [options] - The `options` parameter in the `createTree` method is of type `RedBlackTreeOptions<K,
|
|
168
|
+
* V, R>`, which is a generic type with three type parameters `K`, `V`, and `R`. This parameter
|
|
169
|
+
* allows you to pass additional configuration options when creating a new Red-
|
|
170
|
+
* @returns A new instance of a RedBlackTree with the specified options and properties from the
|
|
171
|
+
* current object is being returned.
|
|
170
172
|
*/
|
|
171
|
-
|
|
172
|
-
|
|
173
|
+
// @ts-ignore
|
|
174
|
+
override createTree(options?: RedBlackTreeOptions<K, V, R>) {
|
|
175
|
+
return new RedBlackTree<K, V, R>([], {
|
|
173
176
|
iterationType: this.iterationType,
|
|
174
177
|
isMapMode: this._isMapMode,
|
|
175
|
-
|
|
178
|
+
specifyComparable: this._specifyComparable,
|
|
176
179
|
toEntryFn: this._toEntryFn,
|
|
177
180
|
...options
|
|
178
|
-
})
|
|
181
|
+
});
|
|
179
182
|
}
|
|
180
183
|
|
|
181
184
|
/**
|
|
@@ -192,42 +195,6 @@ export class RedBlackTree<
|
|
|
192
195
|
return keyNodeEntryOrRaw instanceof RedBlackTreeNode;
|
|
193
196
|
}
|
|
194
197
|
|
|
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
198
|
/**
|
|
232
199
|
* Time Complexity: O(1)
|
|
233
200
|
* Space Complexity: O(1)
|
|
@@ -353,6 +320,40 @@ export class RedBlackTree<
|
|
|
353
320
|
return results;
|
|
354
321
|
}
|
|
355
322
|
|
|
323
|
+
/**
|
|
324
|
+
* Time Complexity: O(n)
|
|
325
|
+
* Space Complexity: O(n)
|
|
326
|
+
*
|
|
327
|
+
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
|
|
328
|
+
* applying a callback to each entry in the original tree.
|
|
329
|
+
* @param callback - A function that will be called for each entry in the tree, with parameters
|
|
330
|
+
* representing the key, value, index, and the tree itself. It should return an entry for the new
|
|
331
|
+
* tree.
|
|
332
|
+
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
|
|
333
|
+
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
|
|
334
|
+
* Tree that will be created during the mapping process. These options could include things like
|
|
335
|
+
* custom comparators
|
|
336
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
337
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
338
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
339
|
+
* or
|
|
340
|
+
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
341
|
+
* provided callback function.
|
|
342
|
+
*/
|
|
343
|
+
// @ts-ignore
|
|
344
|
+
override map<MK, MV, MR>(
|
|
345
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
346
|
+
options?: RedBlackTreeOptions<MK, MV, MR>,
|
|
347
|
+
thisArg?: any
|
|
348
|
+
) {
|
|
349
|
+
const newTree = new RedBlackTree<MK, MV, MR>([], options);
|
|
350
|
+
let index = 0;
|
|
351
|
+
for (const [key, value] of this) {
|
|
352
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
353
|
+
}
|
|
354
|
+
return newTree;
|
|
355
|
+
}
|
|
356
|
+
|
|
356
357
|
/**
|
|
357
358
|
* Time Complexity: O(1)
|
|
358
359
|
* Space Complexity: O(1)
|
|
@@ -419,7 +420,7 @@ export class RedBlackTree<
|
|
|
419
420
|
|
|
420
421
|
if (!parent) {
|
|
421
422
|
this._setRoot(node);
|
|
422
|
-
} else if (node.key
|
|
423
|
+
} else if (this._compare(node.key, parent.key) < 0) {
|
|
423
424
|
parent.left = node;
|
|
424
425
|
} else {
|
|
425
426
|
parent.right = node;
|
|
@@ -9,10 +9,10 @@ import type {
|
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
10
|
BSTNOptKeyOrNode,
|
|
11
11
|
BTNRep,
|
|
12
|
+
EntryCallback,
|
|
12
13
|
IterationType,
|
|
13
14
|
OptNode,
|
|
14
15
|
RBTNColor,
|
|
15
|
-
TreeMultiMapNested,
|
|
16
16
|
TreeMultiMapNodeNested,
|
|
17
17
|
TreeMultiMapOptions
|
|
18
18
|
} from '../../types';
|
|
@@ -65,11 +65,10 @@ export class TreeMultiMap<
|
|
|
65
65
|
K = any,
|
|
66
66
|
V = any,
|
|
67
67
|
R = object,
|
|
68
|
-
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V
|
|
69
|
-
TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
|
|
68
|
+
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>
|
|
70
69
|
>
|
|
71
|
-
extends RedBlackTree<K, V, R, NODE
|
|
72
|
-
implements IBinaryTree<K, V, R, NODE
|
|
70
|
+
extends RedBlackTree<K, V, R, NODE>
|
|
71
|
+
implements IBinaryTree<K, V, R, NODE>
|
|
73
72
|
{
|
|
74
73
|
/**
|
|
75
74
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
@@ -135,53 +134,15 @@ export class TreeMultiMap<
|
|
|
135
134
|
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
|
|
136
135
|
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
137
136
|
*/
|
|
138
|
-
|
|
139
|
-
|
|
137
|
+
// @ts-ignore
|
|
138
|
+
override createTree(options?: TreeMultiMapOptions<K, V, R>) {
|
|
139
|
+
return new TreeMultiMap<K, V, R, NODE>([], {
|
|
140
140
|
iterationType: this.iterationType,
|
|
141
141
|
isMapMode: this._isMapMode,
|
|
142
|
-
|
|
142
|
+
specifyComparable: this._specifyComparable,
|
|
143
143
|
toEntryFn: this._toEntryFn,
|
|
144
144
|
...options
|
|
145
|
-
})
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
|
|
150
|
-
* node based on the input.
|
|
151
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
152
|
-
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
153
|
-
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
154
|
-
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
155
|
-
* an existing node.
|
|
156
|
-
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
157
|
-
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
158
|
-
* @returns either a NODE object or undefined.
|
|
159
|
-
*/
|
|
160
|
-
protected override _keyValueNodeEntryRawToNodeAndValue(
|
|
161
|
-
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
162
|
-
value?: V,
|
|
163
|
-
count = 1
|
|
164
|
-
): [NODE | undefined, V | undefined] {
|
|
165
|
-
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
|
|
166
|
-
|
|
167
|
-
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
168
|
-
|
|
169
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
170
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
171
|
-
if (key === undefined || key === null) return [undefined, undefined];
|
|
172
|
-
const finalValue = value ?? entryValue;
|
|
173
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
177
|
-
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
178
|
-
const finalValue = value ?? entryValue;
|
|
179
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
|
|
183
|
-
|
|
184
|
-
return [undefined, undefined];
|
|
145
|
+
});
|
|
185
146
|
}
|
|
186
147
|
|
|
187
148
|
/**
|
|
@@ -402,13 +363,82 @@ export class TreeMultiMap<
|
|
|
402
363
|
* The function overrides the clone method to create a deep copy of a tree object.
|
|
403
364
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
404
365
|
*/
|
|
405
|
-
|
|
366
|
+
// @ts-ignore
|
|
367
|
+
override clone() {
|
|
406
368
|
const cloned = this.createTree();
|
|
407
369
|
this.bfs(node => cloned.add(node.key, undefined, node.count));
|
|
408
370
|
if (this._isMapMode) cloned._store = this._store;
|
|
409
371
|
return cloned;
|
|
410
372
|
}
|
|
411
373
|
|
|
374
|
+
/**
|
|
375
|
+
* The `map` function in TypeScript overrides the default behavior to create a new TreeMultiMap with
|
|
376
|
+
* modified entries based on a provided callback.
|
|
377
|
+
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
378
|
+
* map. It takes four arguments:
|
|
379
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
380
|
+
* `TreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional configuration
|
|
381
|
+
* options when creating a new `TreeMultiMap` instance within the `map` function. These options could
|
|
382
|
+
* include things like
|
|
383
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
384
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
385
|
+
* (value of `this`) for the callback function when it is called within the `map` function. This
|
|
386
|
+
* @returns A new TreeMultiMap instance is being returned, which is populated with entries generated
|
|
387
|
+
* by the provided callback function.
|
|
388
|
+
*/
|
|
389
|
+
// @ts-ignore
|
|
390
|
+
override map<MK, MV, MR>(
|
|
391
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
392
|
+
options?: TreeMultiMapOptions<MK, MV, MR>,
|
|
393
|
+
thisArg?: any
|
|
394
|
+
) {
|
|
395
|
+
const newTree = new TreeMultiMap<MK, MV, MR>([], options);
|
|
396
|
+
let index = 0;
|
|
397
|
+
for (const [key, value] of this) {
|
|
398
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
399
|
+
}
|
|
400
|
+
return newTree;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
|
|
405
|
+
* node based on the input.
|
|
406
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
407
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
408
|
+
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
409
|
+
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
410
|
+
* an existing node.
|
|
411
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
412
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
413
|
+
* @returns either a NODE object or undefined.
|
|
414
|
+
*/
|
|
415
|
+
protected override _keyValueNodeEntryRawToNodeAndValue(
|
|
416
|
+
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
417
|
+
value?: V,
|
|
418
|
+
count = 1
|
|
419
|
+
): [NODE | undefined, V | undefined] {
|
|
420
|
+
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
|
|
421
|
+
|
|
422
|
+
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
423
|
+
|
|
424
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
425
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
426
|
+
if (key === undefined || key === null) return [undefined, undefined];
|
|
427
|
+
const finalValue = value ?? entryValue;
|
|
428
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
432
|
+
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
433
|
+
const finalValue = value ?? entryValue;
|
|
434
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
|
|
438
|
+
|
|
439
|
+
return [undefined, undefined];
|
|
440
|
+
}
|
|
441
|
+
|
|
412
442
|
/**
|
|
413
443
|
* Time Complexity: O(1)
|
|
414
444
|
* Space Complexity: O(1)
|
|
@@ -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
|
*/
|