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