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
@@ -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
- * The default comparator function.
2830
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
2831
- */
2832
- __publicField(this, "_comparator", /* @__PURE__ */ __name((a, b) => {
2833
- if (isComparable(a) && isComparable(b)) {
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
- const { specifyComparable, isReverse } = options;
2855
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
2856
- if (isReverse !== void 0) this._isReverse = isReverse;
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
- * Gets whether the tree's comparison logic is reversed.
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
- get isReverse() {
2876
- return this._isReverse;
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, this._specifyComparable !== void 0);
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 = this.isReverse ? range.high : range.low;
3028
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
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 = this.isReverse ? range.low : range.high;
3043
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
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 the first node found that satisfies the predicate.
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
- * @param predicate - A function to test each [key, value] pair.
3368
- * @returns True if a node was deleted, false otherwise.
3369
- */
3370
- deleteWhere(predicate) {
3371
- const stack = [];
3372
- let cur = this._root;
3373
- let index = 0;
3374
- while (stack.length > 0 || cur !== void 0) {
3375
- while (cur !== void 0 && cur !== null) {
3376
- stack.push(cur);
3377
- cur = cur.left;
3378
- }
3379
- const node = stack.pop();
3380
- if (!node) break;
3381
- const key = node.key;
3382
- const val = node.value;
3383
- if (predicate(key, val, index++, this)) {
3384
- return this._deleteByKey(key);
3385
- }
3386
- cur = node.right;
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 false;
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
- specifyComparable: this.specifyComparable,
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) (or O(C) if `specifyComparable` is used).
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._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
3587
+ return this._comparator(a, b);
3578
3588
  }
3579
3589
  /**
3580
3590
  * (Private) Deletes a node by its key.