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.
Files changed (29) hide show
  1. package/dist/cjs/index.cjs +85 -75
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +85 -75
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +85 -75
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +85 -75
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  10. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  12. package/dist/types/data-structures/binary-tree/bst.d.ts +46 -26
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
  14. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  15. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
  16. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  17. package/dist/umd/avl-tree-typed.js +85 -75
  18. package/dist/umd/avl-tree-typed.js.map +1 -1
  19. package/dist/umd/avl-tree-typed.min.js +2 -2
  20. package/dist/umd/avl-tree-typed.min.js.map +1 -1
  21. package/package.json +2 -2
  22. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  23. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
  24. package/src/data-structures/binary-tree/avl-tree.ts +4 -5
  25. package/src/data-structures/binary-tree/bst.ts +111 -82
  26. package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
  27. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  28. package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
  29. package/src/types/data-structures/binary-tree/bst.ts +5 -5
@@ -2828,9 +2828,13 @@ var BST = class extends BinaryTree {
2828
2828
  constructor(keysNodesEntriesOrRaws = [], options) {
2829
2829
  super([], options);
2830
2830
  if (options) {
2831
- const { specifyComparable, isReverse } = options;
2832
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
2833
- if (isReverse !== void 0) this._isReverse = isReverse;
2831
+ if ("comparator" in options && options.comparator !== void 0) {
2832
+ this._comparator = options.comparator;
2833
+ } else {
2834
+ this._comparator = this._createDefaultComparator();
2835
+ }
2836
+ } else {
2837
+ this._comparator = this._createDefaultComparator();
2834
2838
  }
2835
2839
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
2836
2840
  }
@@ -2844,40 +2848,33 @@ var BST = class extends BinaryTree {
2844
2848
  get root() {
2845
2849
  return this._root;
2846
2850
  }
2847
- _isReverse = false;
2848
2851
  /**
2849
- * Gets whether the tree's comparison logic is reversed.
2850
- * @remarks Time O(1)
2851
- *
2852
- * @returns True if the tree is reversed (e.g., a max-heap logic).
2852
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
2853
+ * @remarks Time O(1) Space O(1)
2854
+ * @returns The default comparator function.
2853
2855
  */
2854
- get isReverse() {
2855
- return this._isReverse;
2856
+ _createDefaultComparator() {
2857
+ return (a, b) => {
2858
+ debugger;
2859
+ if (isComparable(a) && isComparable(b)) {
2860
+ if (a > b) return 1;
2861
+ if (a < b) return -1;
2862
+ return 0;
2863
+ }
2864
+ if (typeof a === "object" || typeof b === "object") {
2865
+ throw TypeError(
2866
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
2867
+ );
2868
+ }
2869
+ return 0;
2870
+ };
2856
2871
  }
2857
2872
  /**
2858
- * The default comparator function.
2859
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
2860
- */
2861
- _comparator = /* @__PURE__ */ __name((a, b) => {
2862
- if (isComparable(a) && isComparable(b)) {
2863
- if (a > b) return 1;
2864
- if (a < b) return -1;
2865
- return 0;
2866
- }
2867
- if (this._specifyComparable) {
2868
- const va = this._specifyComparable(a);
2869
- const vb = this._specifyComparable(b);
2870
- if (va > vb) return 1;
2871
- if (va < vb) return -1;
2872
- return 0;
2873
- }
2874
- if (typeof a === "object" || typeof b === "object") {
2875
- throw TypeError(
2876
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
2877
- );
2878
- }
2879
- return 0;
2880
- }, "_comparator");
2873
+ * The comparator function used to determine the order of keys in the tree.
2874
+
2875
+ * @remarks Time O(1) Space O(1)
2876
+ */
2877
+ _comparator;
2881
2878
  /**
2882
2879
  * Gets the comparator function used by the tree.
2883
2880
  * @remarks Time O(1)
@@ -2887,16 +2884,6 @@ var BST = class extends BinaryTree {
2887
2884
  get comparator() {
2888
2885
  return this._comparator;
2889
2886
  }
2890
- _specifyComparable;
2891
- /**
2892
- * Gets the function used to extract a comparable value from a complex key.
2893
- * @remarks Time O(1)
2894
- *
2895
- * @returns The key-to-comparable conversion function.
2896
- */
2897
- get specifyComparable() {
2898
- return this._specifyComparable;
2899
- }
2900
2887
  /**
2901
2888
  * (Protected) Creates a new BST node.
2902
2889
  * @remarks Time O(1), Space O(1)
@@ -2937,7 +2924,7 @@ var BST = class extends BinaryTree {
2937
2924
  * @returns True if the key is valid, false otherwise.
2938
2925
  */
2939
2926
  isValidKey(key) {
2940
- return isComparable(key, this._specifyComparable !== void 0);
2927
+ return isComparable(key);
2941
2928
  }
2942
2929
  /**
2943
2930
  * Performs a Depth-First Search (DFS) traversal.
@@ -3026,8 +3013,8 @@ var BST = class extends BinaryTree {
3026
3013
  if (!this.isRealNode(cur.left)) return false;
3027
3014
  if (isRange) {
3028
3015
  const range = keyNodeEntryOrPredicate;
3029
- const leftS = this.isReverse ? range.high : range.low;
3030
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
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 extends BinaryTree {
3041
3028
  if (!this.isRealNode(cur.right)) return false;
3042
3029
  if (isRange) {
3043
3030
  const range = keyNodeEntryOrPredicate;
3044
- const rightS = this.isReverse ? range.low : range.high;
3045
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
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 extends BinaryTree {
3363
3350
  return out;
3364
3351
  }
3365
3352
  /**
3366
- * Deletes the first node found that satisfies the predicate.
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
- * @param predicate - A function to test each [key, value] pair.
3370
- * @returns True if a node was deleted, false otherwise.
3371
- */
3372
- deleteWhere(predicate) {
3373
- const stack = [];
3374
- let cur = this._root;
3375
- let index = 0;
3376
- while (stack.length > 0 || cur !== void 0) {
3377
- while (cur !== void 0 && cur !== null) {
3378
- stack.push(cur);
3379
- cur = cur.left;
3380
- }
3381
- const node = stack.pop();
3382
- if (!node) break;
3383
- const key = node.key;
3384
- const val = node.value;
3385
- if (predicate(key, val, index++, this)) {
3386
- return this._deleteByKey(key);
3387
- }
3388
- cur = node.right;
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 false;
3401
+ return results;
3391
3402
  }
3392
3403
  /**
3393
3404
  * (Protected) Core bound search implementation supporting all parameter types.
@@ -3539,8 +3550,7 @@ var BST = class extends BinaryTree {
3539
3550
  _snapshotOptions() {
3540
3551
  return {
3541
3552
  ...super._snapshotOptions(),
3542
- specifyComparable: this.specifyComparable,
3543
- isReverse: this.isReverse
3553
+ comparator: this._comparator
3544
3554
  };
3545
3555
  }
3546
3556
  /**
@@ -3568,14 +3578,14 @@ var BST = class extends BinaryTree {
3568
3578
  }
3569
3579
  /**
3570
3580
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
3571
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
3581
+ * @remarks Time O(1) Space O(1)
3572
3582
  *
3573
3583
  * @param a - The first key.
3574
3584
  * @param b - The second key.
3575
3585
  * @returns A number (1, -1, or 0) representing the comparison.
3576
3586
  */
3577
3587
  _compare(a, b) {
3578
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
3588
+ return this._comparator(a, b);
3579
3589
  }
3580
3590
  /**
3581
3591
  * (Private) Deletes a node by its key.