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
|
@@ -2831,36 +2831,20 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2831
2831
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
2832
2832
|
super([], options);
|
|
2833
2833
|
__publicField(this, "_root");
|
|
2834
|
-
__publicField(this, "_isReverse", false);
|
|
2835
2834
|
/**
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
if (a > b) return 1;
|
|
2842
|
-
if (a < b) return -1;
|
|
2843
|
-
return 0;
|
|
2844
|
-
}
|
|
2845
|
-
if (this._specifyComparable) {
|
|
2846
|
-
const va = this._specifyComparable(a);
|
|
2847
|
-
const vb = this._specifyComparable(b);
|
|
2848
|
-
if (va > vb) return 1;
|
|
2849
|
-
if (va < vb) return -1;
|
|
2850
|
-
return 0;
|
|
2851
|
-
}
|
|
2852
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2853
|
-
throw TypeError(
|
|
2854
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2855
|
-
);
|
|
2856
|
-
}
|
|
2857
|
-
return 0;
|
|
2858
|
-
}, "_comparator"));
|
|
2859
|
-
__publicField(this, "_specifyComparable");
|
|
2835
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
2836
|
+
|
|
2837
|
+
* @remarks Time O(1) Space O(1)
|
|
2838
|
+
*/
|
|
2839
|
+
__publicField(this, "_comparator");
|
|
2860
2840
|
if (options) {
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2841
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
2842
|
+
this._comparator = options.comparator;
|
|
2843
|
+
} else {
|
|
2844
|
+
this._comparator = this._createDefaultComparator();
|
|
2845
|
+
}
|
|
2846
|
+
} else {
|
|
2847
|
+
this._comparator = this._createDefaultComparator();
|
|
2864
2848
|
}
|
|
2865
2849
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
2866
2850
|
}
|
|
@@ -2874,13 +2858,25 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2874
2858
|
return this._root;
|
|
2875
2859
|
}
|
|
2876
2860
|
/**
|
|
2877
|
-
*
|
|
2878
|
-
* @remarks Time O(1)
|
|
2879
|
-
*
|
|
2880
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
2861
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
2862
|
+
* @remarks Time O(1) Space O(1)
|
|
2863
|
+
* @returns The default comparator function.
|
|
2881
2864
|
*/
|
|
2882
|
-
|
|
2883
|
-
return
|
|
2865
|
+
_createDefaultComparator() {
|
|
2866
|
+
return (a, b) => {
|
|
2867
|
+
debugger;
|
|
2868
|
+
if (isComparable(a) && isComparable(b)) {
|
|
2869
|
+
if (a > b) return 1;
|
|
2870
|
+
if (a < b) return -1;
|
|
2871
|
+
return 0;
|
|
2872
|
+
}
|
|
2873
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
2874
|
+
throw TypeError(
|
|
2875
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
2876
|
+
);
|
|
2877
|
+
}
|
|
2878
|
+
return 0;
|
|
2879
|
+
};
|
|
2884
2880
|
}
|
|
2885
2881
|
/**
|
|
2886
2882
|
* Gets the comparator function used by the tree.
|
|
@@ -2891,15 +2887,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2891
2887
|
get comparator() {
|
|
2892
2888
|
return this._comparator;
|
|
2893
2889
|
}
|
|
2894
|
-
/**
|
|
2895
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
2896
|
-
* @remarks Time O(1)
|
|
2897
|
-
*
|
|
2898
|
-
* @returns The key-to-comparable conversion function.
|
|
2899
|
-
*/
|
|
2900
|
-
get specifyComparable() {
|
|
2901
|
-
return this._specifyComparable;
|
|
2902
|
-
}
|
|
2903
2890
|
/**
|
|
2904
2891
|
* (Protected) Creates a new BST node.
|
|
2905
2892
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2941,7 +2928,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2941
2928
|
* @returns True if the key is valid, false otherwise.
|
|
2942
2929
|
*/
|
|
2943
2930
|
isValidKey(key) {
|
|
2944
|
-
return isComparable(key
|
|
2931
|
+
return isComparable(key);
|
|
2945
2932
|
}
|
|
2946
2933
|
/**
|
|
2947
2934
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -3031,8 +3018,8 @@ var _BST = class _BST 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 _BST 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 _BST 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.
|
|
@@ -3545,8 +3556,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3545
3556
|
_snapshotOptions() {
|
|
3546
3557
|
return {
|
|
3547
3558
|
...super._snapshotOptions(),
|
|
3548
|
-
|
|
3549
|
-
isReverse: this.isReverse
|
|
3559
|
+
comparator: this._comparator
|
|
3550
3560
|
};
|
|
3551
3561
|
}
|
|
3552
3562
|
/**
|
|
@@ -3574,14 +3584,14 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3574
3584
|
}
|
|
3575
3585
|
/**
|
|
3576
3586
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
3577
|
-
* @remarks Time O(1)
|
|
3587
|
+
* @remarks Time O(1) Space O(1)
|
|
3578
3588
|
*
|
|
3579
3589
|
* @param a - The first key.
|
|
3580
3590
|
* @param b - The second key.
|
|
3581
3591
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
3582
3592
|
*/
|
|
3583
3593
|
_compare(a, b) {
|
|
3584
|
-
return this.
|
|
3594
|
+
return this._comparator(a, b);
|
|
3585
3595
|
}
|
|
3586
3596
|
/**
|
|
3587
3597
|
* (Private) Deletes a node by its key.
|