tree-multimap-typed 2.2.3 → 2.2.4
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/cjs/index.cjs +85 -75
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +85 -75
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +85 -75
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +85 -75
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +46 -26
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/tree-multimap-typed.js +85 -75
- package/dist/umd/tree-multimap-typed.js.map +1 -1
- package/dist/umd/tree-multimap-typed.min.js +3 -3
- package/dist/umd/tree-multimap-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
- package/src/data-structures/binary-tree/avl-tree.ts +4 -5
- package/src/data-structures/binary-tree/bst.ts +111 -82
- package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
package/dist/esm/index.mjs
CHANGED
|
@@ -2833,9 +2833,13 @@ var BST = class extends BinaryTree {
|
|
|
2833
2833
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
2834
2834
|
super([], options);
|
|
2835
2835
|
if (options) {
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2836
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
2837
|
+
this._comparator = options.comparator;
|
|
2838
|
+
} else {
|
|
2839
|
+
this._comparator = this._createDefaultComparator();
|
|
2840
|
+
}
|
|
2841
|
+
} else {
|
|
2842
|
+
this._comparator = this._createDefaultComparator();
|
|
2839
2843
|
}
|
|
2840
2844
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
2841
2845
|
}
|
|
@@ -2849,40 +2853,33 @@ var BST = class extends BinaryTree {
|
|
|
2849
2853
|
get root() {
|
|
2850
2854
|
return this._root;
|
|
2851
2855
|
}
|
|
2852
|
-
_isReverse = false;
|
|
2853
2856
|
/**
|
|
2854
|
-
*
|
|
2855
|
-
* @remarks Time O(1)
|
|
2856
|
-
*
|
|
2857
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
2857
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
2858
|
+
* @remarks Time O(1) Space O(1)
|
|
2859
|
+
* @returns The default comparator function.
|
|
2858
2860
|
*/
|
|
2859
|
-
|
|
2860
|
-
return
|
|
2861
|
+
_createDefaultComparator() {
|
|
2862
|
+
return (a, b) => {
|
|
2863
|
+
debugger;
|
|
2864
|
+
if (isComparable(a) && isComparable(b)) {
|
|
2865
|
+
if (a > b) return 1;
|
|
2866
|
+
if (a < b) return -1;
|
|
2867
|
+
return 0;
|
|
2868
|
+
}
|
|
2869
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
2870
|
+
throw TypeError(
|
|
2871
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
2872
|
+
);
|
|
2873
|
+
}
|
|
2874
|
+
return 0;
|
|
2875
|
+
};
|
|
2861
2876
|
}
|
|
2862
2877
|
/**
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
if (a > b) return 1;
|
|
2869
|
-
if (a < b) return -1;
|
|
2870
|
-
return 0;
|
|
2871
|
-
}
|
|
2872
|
-
if (this._specifyComparable) {
|
|
2873
|
-
const va = this._specifyComparable(a);
|
|
2874
|
-
const vb = this._specifyComparable(b);
|
|
2875
|
-
if (va > vb) return 1;
|
|
2876
|
-
if (va < vb) return -1;
|
|
2877
|
-
return 0;
|
|
2878
|
-
}
|
|
2879
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2880
|
-
throw TypeError(
|
|
2881
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2882
|
-
);
|
|
2883
|
-
}
|
|
2884
|
-
return 0;
|
|
2885
|
-
}, "_comparator");
|
|
2878
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
2879
|
+
|
|
2880
|
+
* @remarks Time O(1) Space O(1)
|
|
2881
|
+
*/
|
|
2882
|
+
_comparator;
|
|
2886
2883
|
/**
|
|
2887
2884
|
* Gets the comparator function used by the tree.
|
|
2888
2885
|
* @remarks Time O(1)
|
|
@@ -2892,16 +2889,6 @@ var BST = class extends BinaryTree {
|
|
|
2892
2889
|
get comparator() {
|
|
2893
2890
|
return this._comparator;
|
|
2894
2891
|
}
|
|
2895
|
-
_specifyComparable;
|
|
2896
|
-
/**
|
|
2897
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
2898
|
-
* @remarks Time O(1)
|
|
2899
|
-
*
|
|
2900
|
-
* @returns The key-to-comparable conversion function.
|
|
2901
|
-
*/
|
|
2902
|
-
get specifyComparable() {
|
|
2903
|
-
return this._specifyComparable;
|
|
2904
|
-
}
|
|
2905
2892
|
/**
|
|
2906
2893
|
* (Protected) Creates a new BST node.
|
|
2907
2894
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2942,7 +2929,7 @@ var BST = class extends BinaryTree {
|
|
|
2942
2929
|
* @returns True if the key is valid, false otherwise.
|
|
2943
2930
|
*/
|
|
2944
2931
|
isValidKey(key) {
|
|
2945
|
-
return isComparable(key
|
|
2932
|
+
return isComparable(key);
|
|
2946
2933
|
}
|
|
2947
2934
|
/**
|
|
2948
2935
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -3031,8 +3018,8 @@ var BST = class extends BinaryTree {
|
|
|
3031
3018
|
if (!this.isRealNode(cur.left)) return false;
|
|
3032
3019
|
if (isRange) {
|
|
3033
3020
|
const range = keyNodeEntryOrPredicate;
|
|
3034
|
-
const leftS =
|
|
3035
|
-
const leftI =
|
|
3021
|
+
const leftS = range.low;
|
|
3022
|
+
const leftI = range.includeLow;
|
|
3036
3023
|
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
3037
3024
|
}
|
|
3038
3025
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -3046,8 +3033,8 @@ var BST = class extends BinaryTree {
|
|
|
3046
3033
|
if (!this.isRealNode(cur.right)) return false;
|
|
3047
3034
|
if (isRange) {
|
|
3048
3035
|
const range = keyNodeEntryOrPredicate;
|
|
3049
|
-
const rightS =
|
|
3050
|
-
const rightI =
|
|
3036
|
+
const rightS = range.high;
|
|
3037
|
+
const rightI = range.includeHigh;
|
|
3051
3038
|
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
3052
3039
|
}
|
|
3053
3040
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -3368,31 +3355,55 @@ var BST = class extends BinaryTree {
|
|
|
3368
3355
|
return out;
|
|
3369
3356
|
}
|
|
3370
3357
|
/**
|
|
3371
|
-
* Deletes
|
|
3372
|
-
* @remarks Performs an in-order traversal. Time O(N) worst-case (O(log N) to find + O(log N) to delete). Space O(log N) for stack.
|
|
3358
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
3373
3359
|
*
|
|
3374
|
-
* @
|
|
3375
|
-
*
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3360
|
+
* @remarks
|
|
3361
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
3362
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
3363
|
+
*
|
|
3364
|
+
* @template K - The key type.
|
|
3365
|
+
* @template V - The value type.
|
|
3366
|
+
*
|
|
3367
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
3368
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
3369
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
3370
|
+
* - An entry tuple: searches for the key-value pair.
|
|
3371
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
3372
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
3373
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
3374
|
+
*
|
|
3375
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
3376
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
3377
|
+
*
|
|
3378
|
+
* @param startNode - The node to start the search from. Can be:
|
|
3379
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
3380
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
3381
|
+
* - Default value: this._root (the tree's root).
|
|
3382
|
+
*
|
|
3383
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
3384
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
3385
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
3386
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
3387
|
+
*
|
|
3388
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
3389
|
+
* - Key: the matched node's key.
|
|
3390
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
3391
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
3392
|
+
*/
|
|
3393
|
+
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
3394
|
+
const toDelete = this.search(
|
|
3395
|
+
keyNodeEntryOrPredicate,
|
|
3396
|
+
onlyOne,
|
|
3397
|
+
(node) => node,
|
|
3398
|
+
startNode,
|
|
3399
|
+
iterationType
|
|
3400
|
+
);
|
|
3401
|
+
let results = [];
|
|
3402
|
+
for (const node of toDelete) {
|
|
3403
|
+
const deleteInfo = this.delete(node);
|
|
3404
|
+
results = results.concat(deleteInfo);
|
|
3394
3405
|
}
|
|
3395
|
-
return
|
|
3406
|
+
return results;
|
|
3396
3407
|
}
|
|
3397
3408
|
/**
|
|
3398
3409
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
@@ -3544,8 +3555,7 @@ var BST = class extends BinaryTree {
|
|
|
3544
3555
|
_snapshotOptions() {
|
|
3545
3556
|
return {
|
|
3546
3557
|
...super._snapshotOptions(),
|
|
3547
|
-
|
|
3548
|
-
isReverse: this.isReverse
|
|
3558
|
+
comparator: this._comparator
|
|
3549
3559
|
};
|
|
3550
3560
|
}
|
|
3551
3561
|
/**
|
|
@@ -3573,14 +3583,14 @@ var BST = class extends BinaryTree {
|
|
|
3573
3583
|
}
|
|
3574
3584
|
/**
|
|
3575
3585
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
3576
|
-
* @remarks Time O(1)
|
|
3586
|
+
* @remarks Time O(1) Space O(1)
|
|
3577
3587
|
*
|
|
3578
3588
|
* @param a - The first key.
|
|
3579
3589
|
* @param b - The second key.
|
|
3580
3590
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
3581
3591
|
*/
|
|
3582
3592
|
_compare(a, b) {
|
|
3583
|
-
return this.
|
|
3593
|
+
return this._comparator(a, b);
|
|
3584
3594
|
}
|
|
3585
3595
|
/**
|
|
3586
3596
|
* (Private) Deletes a node by its key.
|