data-structure-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/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +1 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +34 -27
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +62 -52
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +3 -14
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +22 -25
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +32 -16
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +43 -24
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +14 -23
- package/dist/cjs/data-structures/binary-tree/bst.js +28 -29
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +35 -28
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +52 -71
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +18 -14
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +36 -23
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +2 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js +5 -5
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +10 -10
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +12 -12
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +10 -10
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +16 -16
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -4
- package/dist/mjs/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/mjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +34 -27
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +62 -52
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +3 -14
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +22 -26
- package/dist/mjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +32 -16
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +43 -24
- package/dist/mjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +14 -23
- package/dist/mjs/data-structures/binary-tree/bst.js +29 -30
- package/dist/mjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/red-black-tree.d.ts +35 -28
- package/dist/mjs/data-structures/binary-tree/red-black-tree.js +51 -71
- package/dist/mjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +18 -14
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +36 -23
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.js +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/mjs/data-structures/hash/hash-map.js +5 -5
- package/dist/mjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +10 -10
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +12 -12
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +10 -10
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +16 -16
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -4
- package/dist/umd/data-structure-typed.js +258 -249
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- 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
- package/test/integration/index.html +3 -3
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +12 -12
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +10 -10
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +10 -10
- package/test/unit/data-structures/binary-tree/bst.test.ts +11 -11
- package/test/unit/data-structures/binary-tree/data/cost-of-living-by-country.ts +259 -0
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +30 -10
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +10 -10
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +12 -12
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import
|
|
8
|
+
import {
|
|
9
9
|
BSTNested,
|
|
10
10
|
BSTNodeNested,
|
|
11
11
|
BSTNOptKeyOrNode,
|
|
@@ -15,10 +15,12 @@ import type {
|
|
|
15
15
|
Comparator,
|
|
16
16
|
CP,
|
|
17
17
|
DFSOrderPattern,
|
|
18
|
+
EntryCallback,
|
|
18
19
|
IterationType,
|
|
19
20
|
NodeCallback,
|
|
20
21
|
NodePredicate,
|
|
21
|
-
OptNode
|
|
22
|
+
OptNode,
|
|
23
|
+
OptNodeOrNull
|
|
22
24
|
} from '../../types';
|
|
23
25
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
24
26
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -40,13 +42,13 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
40
42
|
this._right = undefined;
|
|
41
43
|
}
|
|
42
44
|
|
|
43
|
-
|
|
45
|
+
override _left?: OptNodeOrNull<NODE>;
|
|
44
46
|
|
|
45
47
|
/**
|
|
46
48
|
* The function returns the value of the `_left` property.
|
|
47
49
|
* @returns The `_left` property of the current object is being returned.
|
|
48
50
|
*/
|
|
49
|
-
override get left():
|
|
51
|
+
override get left(): OptNodeOrNull<NODE> {
|
|
50
52
|
return this._left;
|
|
51
53
|
}
|
|
52
54
|
|
|
@@ -55,21 +57,21 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
55
57
|
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
|
|
56
58
|
* instance of the `NODE` class or `undefined`.
|
|
57
59
|
*/
|
|
58
|
-
override set left(v:
|
|
60
|
+
override set left(v: OptNodeOrNull<NODE>) {
|
|
59
61
|
if (v) {
|
|
60
62
|
v.parent = this as unknown as NODE;
|
|
61
63
|
}
|
|
62
64
|
this._left = v;
|
|
63
65
|
}
|
|
64
66
|
|
|
65
|
-
|
|
67
|
+
override _right?: OptNodeOrNull<NODE>;
|
|
66
68
|
|
|
67
69
|
/**
|
|
68
70
|
* The function returns the right node of a binary tree or undefined if there is no right node.
|
|
69
71
|
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
|
|
70
72
|
* `undefined`.
|
|
71
73
|
*/
|
|
72
|
-
override get right():
|
|
74
|
+
override get right(): OptNodeOrNull<NODE> {
|
|
73
75
|
return this._right;
|
|
74
76
|
}
|
|
75
77
|
|
|
@@ -78,7 +80,7 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
78
80
|
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
|
|
79
81
|
* `NODE` object or `undefined`.
|
|
80
82
|
*/
|
|
81
|
-
override set right(v:
|
|
83
|
+
override set right(v: OptNodeOrNull<NODE>) {
|
|
82
84
|
if (v) {
|
|
83
85
|
v.parent = this as unknown as NODE;
|
|
84
86
|
}
|
|
@@ -155,11 +157,23 @@ export class BST<
|
|
|
155
157
|
K = any,
|
|
156
158
|
V = any,
|
|
157
159
|
R = object,
|
|
160
|
+
MK = any,
|
|
161
|
+
MV = any,
|
|
162
|
+
MR = object,
|
|
158
163
|
NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
|
|
159
|
-
TREE extends BST<K, V, R,
|
|
164
|
+
TREE extends BST<K, V, R, MK, MV, MR, NODE, TREE> = BST<
|
|
165
|
+
K,
|
|
166
|
+
V,
|
|
167
|
+
R,
|
|
168
|
+
MK,
|
|
169
|
+
MV,
|
|
170
|
+
MR,
|
|
171
|
+
NODE,
|
|
172
|
+
BSTNested<K, V, R, MK, MV, MR, NODE>
|
|
173
|
+
>
|
|
160
174
|
>
|
|
161
|
-
extends BinaryTree<K, V, R, NODE, TREE>
|
|
162
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
175
|
+
extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
176
|
+
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
163
177
|
{
|
|
164
178
|
/**
|
|
165
179
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
@@ -173,8 +187,8 @@ export class BST<
|
|
|
173
187
|
super([], options);
|
|
174
188
|
|
|
175
189
|
if (options) {
|
|
176
|
-
const {
|
|
177
|
-
if (typeof
|
|
190
|
+
const { specifyComparable, isReverse } = options;
|
|
191
|
+
if (typeof specifyComparable === 'function') this._specifyComparable = specifyComparable;
|
|
178
192
|
if (isReverse !== undefined) this._isReverse = isReverse;
|
|
179
193
|
}
|
|
180
194
|
|
|
@@ -222,10 +236,10 @@ export class BST<
|
|
|
222
236
|
* @returns a new instance of the BST class with the provided options.
|
|
223
237
|
*/
|
|
224
238
|
override createTree(options?: BSTOptions<K, V, R>): TREE {
|
|
225
|
-
return new BST<K, V, R, NODE, TREE>([], {
|
|
239
|
+
return new BST<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
226
240
|
iterationType: this.iterationType,
|
|
227
241
|
isMapMode: this._isMapMode,
|
|
228
|
-
|
|
242
|
+
specifyComparable: this._specifyComparable,
|
|
229
243
|
toEntryFn: this._toEntryFn,
|
|
230
244
|
isReverse: this._isReverse,
|
|
231
245
|
...options
|
|
@@ -292,7 +306,7 @@ export class BST<
|
|
|
292
306
|
* this._DEFAULT_COMPARATOR`.
|
|
293
307
|
*/
|
|
294
308
|
override isKey(key: any): key is K {
|
|
295
|
-
return isComparable(key, this.
|
|
309
|
+
return isComparable(key, this._specifyComparable !== undefined);
|
|
296
310
|
}
|
|
297
311
|
|
|
298
312
|
/**
|
|
@@ -330,7 +344,7 @@ export class BST<
|
|
|
330
344
|
this._size++;
|
|
331
345
|
return true;
|
|
332
346
|
}
|
|
333
|
-
current = current.left;
|
|
347
|
+
if (current.left !== null) current = current.left;
|
|
334
348
|
} else {
|
|
335
349
|
if (current.right === undefined) {
|
|
336
350
|
current.right = newNode;
|
|
@@ -338,7 +352,7 @@ export class BST<
|
|
|
338
352
|
this._size++;
|
|
339
353
|
return true;
|
|
340
354
|
}
|
|
341
|
-
current = current.right;
|
|
355
|
+
if (current.right !== null) current = current.right;
|
|
342
356
|
}
|
|
343
357
|
}
|
|
344
358
|
|
|
@@ -463,19 +477,6 @@ export class BST<
|
|
|
463
477
|
return inserted;
|
|
464
478
|
}
|
|
465
479
|
|
|
466
|
-
/**
|
|
467
|
-
* Time Complexity: O(n)
|
|
468
|
-
* Space Complexity: O(1)
|
|
469
|
-
*
|
|
470
|
-
* The `merge` function overrides the base class method by adding elements from another
|
|
471
|
-
* binary search tree.
|
|
472
|
-
* @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
|
|
473
|
-
* value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
|
|
474
|
-
*/
|
|
475
|
-
override merge(anotherTree: BST<K, V, R, NODE, TREE>) {
|
|
476
|
-
this.addMany(anotherTree, [], false);
|
|
477
|
-
}
|
|
478
|
-
|
|
479
480
|
/**
|
|
480
481
|
* Time Complexity: O(log n)
|
|
481
482
|
* Space Complexity: O(k + log n)
|
|
@@ -886,7 +887,7 @@ export class BST<
|
|
|
886
887
|
let balanced = true;
|
|
887
888
|
|
|
888
889
|
if (iterationType === 'RECURSIVE') {
|
|
889
|
-
const _height = (cur:
|
|
890
|
+
const _height = (cur: OptNodeOrNull<NODE>): number => {
|
|
890
891
|
if (!cur) return 0;
|
|
891
892
|
const leftHeight = _height(cur.left),
|
|
892
893
|
rightHeight = _height(cur.right);
|
|
@@ -903,7 +904,7 @@ export class BST<
|
|
|
903
904
|
while (stack.length > 0 || node) {
|
|
904
905
|
if (node) {
|
|
905
906
|
stack.push(node);
|
|
906
|
-
node = node.left;
|
|
907
|
+
if (node.left !== null) node = node.left;
|
|
907
908
|
} else {
|
|
908
909
|
node = stack[stack.length - 1];
|
|
909
910
|
if (!node.right || last === node.right) {
|
|
@@ -930,14 +931,14 @@ export class BST<
|
|
|
930
931
|
if (a < b) return -1;
|
|
931
932
|
return 0;
|
|
932
933
|
}
|
|
933
|
-
if (this.
|
|
934
|
-
if (this.
|
|
935
|
-
if (this.
|
|
934
|
+
if (this._specifyComparable) {
|
|
935
|
+
if (this._specifyComparable(a) > this._specifyComparable(b)) return 1;
|
|
936
|
+
if (this._specifyComparable(a) < this._specifyComparable(b)) return -1;
|
|
936
937
|
return 0;
|
|
937
938
|
}
|
|
938
939
|
if (typeof a === 'object' || typeof b === 'object') {
|
|
939
940
|
throw TypeError(
|
|
940
|
-
`When comparing object types, a custom
|
|
941
|
+
`When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.`
|
|
941
942
|
);
|
|
942
943
|
}
|
|
943
944
|
|
|
@@ -952,15 +953,15 @@ export class BST<
|
|
|
952
953
|
return this._comparator;
|
|
953
954
|
}
|
|
954
955
|
|
|
955
|
-
protected
|
|
956
|
+
protected _specifyComparable?: (key: K) => Comparable;
|
|
956
957
|
|
|
957
958
|
/**
|
|
958
|
-
* This function returns the value of the `
|
|
959
|
-
* @returns The method `
|
|
960
|
-
* `
|
|
959
|
+
* This function returns the value of the `_specifyComparable` property.
|
|
960
|
+
* @returns The method `specifyComparable()` is being returned, which is a getter method for the
|
|
961
|
+
* `_specifyComparable` property.
|
|
961
962
|
*/
|
|
962
|
-
get
|
|
963
|
-
return this.
|
|
963
|
+
get specifyComparable() {
|
|
964
|
+
return this._specifyComparable;
|
|
964
965
|
}
|
|
965
966
|
|
|
966
967
|
/**
|
|
@@ -978,4 +979,17 @@ export class BST<
|
|
|
978
979
|
protected _compare(a: K, b: K) {
|
|
979
980
|
return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
|
|
980
981
|
}
|
|
982
|
+
|
|
983
|
+
override map(
|
|
984
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
985
|
+
options?: BSTOptions<MK, MV, MR>,
|
|
986
|
+
thisArg?: any
|
|
987
|
+
): BST<MK, MV, MR> {
|
|
988
|
+
const newTree = new BST<MK, MV, MR>([], options);
|
|
989
|
+
let index = 0;
|
|
990
|
+
for (const [key, value] of this) {
|
|
991
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
992
|
+
}
|
|
993
|
+
return newTree;
|
|
994
|
+
}
|
|
981
995
|
}
|
|
@@ -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;
|