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