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