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