data-structure-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 (36) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/README.md +70 -11
  3. package/dist/cjs/index.cjs +85 -75
  4. package/dist/cjs/index.cjs.map +1 -1
  5. package/dist/cjs-legacy/index.cjs +85 -75
  6. package/dist/cjs-legacy/index.cjs.map +1 -1
  7. package/dist/esm/index.mjs +85 -75
  8. package/dist/esm/index.mjs.map +1 -1
  9. package/dist/esm-legacy/index.mjs +85 -75
  10. package/dist/esm-legacy/index.mjs.map +1 -1
  11. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  12. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +46 -26
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
  16. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
  18. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  19. package/dist/umd/data-structure-typed.js +85 -75
  20. package/dist/umd/data-structure-typed.js.map +1 -1
  21. package/dist/umd/data-structure-typed.min.js +1 -1
  22. package/dist/umd/data-structure-typed.min.js.map +1 -1
  23. package/package.json +2 -2
  24. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  25. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
  26. package/src/data-structures/binary-tree/avl-tree.ts +4 -5
  27. package/src/data-structures/binary-tree/bst.ts +111 -82
  28. package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
  29. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  30. package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
  31. package/src/types/data-structures/binary-tree/bst.ts +5 -5
  32. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +2 -2
  33. package/test/unit/data-structures/binary-tree/bst.test.ts +5 -8
  34. package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
  35. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
  36. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +2 -2
@@ -8614,36 +8614,20 @@ var _BST = class _BST extends BinaryTree {
8614
8614
  constructor(keysNodesEntriesOrRaws = [], options) {
8615
8615
  super([], options);
8616
8616
  __publicField(this, "_root");
8617
- __publicField(this, "_isReverse", false);
8618
8617
  /**
8619
- * The default comparator function.
8620
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
8621
- */
8622
- __publicField(this, "_comparator", /* @__PURE__ */ __name((a, b) => {
8623
- if (isComparable(a) && isComparable(b)) {
8624
- if (a > b) return 1;
8625
- if (a < b) return -1;
8626
- return 0;
8627
- }
8628
- if (this._specifyComparable) {
8629
- const va = this._specifyComparable(a);
8630
- const vb = this._specifyComparable(b);
8631
- if (va > vb) return 1;
8632
- if (va < vb) return -1;
8633
- return 0;
8634
- }
8635
- if (typeof a === "object" || typeof b === "object") {
8636
- throw TypeError(
8637
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
8638
- );
8639
- }
8640
- return 0;
8641
- }, "_comparator"));
8642
- __publicField(this, "_specifyComparable");
8618
+ * The comparator function used to determine the order of keys in the tree.
8619
+
8620
+ * @remarks Time O(1) Space O(1)
8621
+ */
8622
+ __publicField(this, "_comparator");
8643
8623
  if (options) {
8644
- const { specifyComparable, isReverse } = options;
8645
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
8646
- if (isReverse !== void 0) this._isReverse = isReverse;
8624
+ if ("comparator" in options && options.comparator !== void 0) {
8625
+ this._comparator = options.comparator;
8626
+ } else {
8627
+ this._comparator = this._createDefaultComparator();
8628
+ }
8629
+ } else {
8630
+ this._comparator = this._createDefaultComparator();
8647
8631
  }
8648
8632
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
8649
8633
  }
@@ -8657,13 +8641,25 @@ var _BST = class _BST extends BinaryTree {
8657
8641
  return this._root;
8658
8642
  }
8659
8643
  /**
8660
- * Gets whether the tree's comparison logic is reversed.
8661
- * @remarks Time O(1)
8662
- *
8663
- * @returns True if the tree is reversed (e.g., a max-heap logic).
8644
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
8645
+ * @remarks Time O(1) Space O(1)
8646
+ * @returns The default comparator function.
8664
8647
  */
8665
- get isReverse() {
8666
- return this._isReverse;
8648
+ _createDefaultComparator() {
8649
+ return (a, b) => {
8650
+ debugger;
8651
+ if (isComparable(a) && isComparable(b)) {
8652
+ if (a > b) return 1;
8653
+ if (a < b) return -1;
8654
+ return 0;
8655
+ }
8656
+ if (typeof a === "object" || typeof b === "object") {
8657
+ throw TypeError(
8658
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
8659
+ );
8660
+ }
8661
+ return 0;
8662
+ };
8667
8663
  }
8668
8664
  /**
8669
8665
  * Gets the comparator function used by the tree.
@@ -8674,15 +8670,6 @@ var _BST = class _BST extends BinaryTree {
8674
8670
  get comparator() {
8675
8671
  return this._comparator;
8676
8672
  }
8677
- /**
8678
- * Gets the function used to extract a comparable value from a complex key.
8679
- * @remarks Time O(1)
8680
- *
8681
- * @returns The key-to-comparable conversion function.
8682
- */
8683
- get specifyComparable() {
8684
- return this._specifyComparable;
8685
- }
8686
8673
  /**
8687
8674
  * (Protected) Creates a new BST node.
8688
8675
  * @remarks Time O(1), Space O(1)
@@ -8724,7 +8711,7 @@ var _BST = class _BST extends BinaryTree {
8724
8711
  * @returns True if the key is valid, false otherwise.
8725
8712
  */
8726
8713
  isValidKey(key) {
8727
- return isComparable(key, this._specifyComparable !== void 0);
8714
+ return isComparable(key);
8728
8715
  }
8729
8716
  /**
8730
8717
  * Performs a Depth-First Search (DFS) traversal.
@@ -8814,8 +8801,8 @@ var _BST = class _BST extends BinaryTree {
8814
8801
  if (!this.isRealNode(cur.left)) return false;
8815
8802
  if (isRange) {
8816
8803
  const range = keyNodeEntryOrPredicate;
8817
- const leftS = this.isReverse ? range.high : range.low;
8818
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
8804
+ const leftS = range.low;
8805
+ const leftI = range.includeLow;
8819
8806
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
8820
8807
  }
8821
8808
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -8829,8 +8816,8 @@ var _BST = class _BST extends BinaryTree {
8829
8816
  if (!this.isRealNode(cur.right)) return false;
8830
8817
  if (isRange) {
8831
8818
  const range = keyNodeEntryOrPredicate;
8832
- const rightS = this.isReverse ? range.low : range.high;
8833
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
8819
+ const rightS = range.high;
8820
+ const rightI = range.includeHigh;
8834
8821
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
8835
8822
  }
8836
8823
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -9151,31 +9138,55 @@ var _BST = class _BST extends BinaryTree {
9151
9138
  return out;
9152
9139
  }
9153
9140
  /**
9154
- * Deletes the first node found that satisfies the predicate.
9155
- * @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.
9141
+ * Deletes nodes that match a key, node, entry, predicate, or range.
9156
9142
  *
9157
- * @param predicate - A function to test each [key, value] pair.
9158
- * @returns True if a node was deleted, false otherwise.
9159
- */
9160
- deleteWhere(predicate) {
9161
- const stack = [];
9162
- let cur = this._root;
9163
- let index = 0;
9164
- while (stack.length > 0 || cur !== void 0) {
9165
- while (cur !== void 0 && cur !== null) {
9166
- stack.push(cur);
9167
- cur = cur.left;
9168
- }
9169
- const node = stack.pop();
9170
- if (!node) break;
9171
- const key = node.key;
9172
- const val = node.value;
9173
- if (predicate(key, val, index++, this)) {
9174
- return this._deleteByKey(key);
9175
- }
9176
- cur = node.right;
9143
+ * @remarks
9144
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
9145
+ * Space Complexity: O(M) for storing matched nodes and result map.
9146
+ *
9147
+ * @template K - The key type.
9148
+ * @template V - The value type.
9149
+ *
9150
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
9151
+ * - A key (type K): searches for exact key match using the comparator.
9152
+ * - A BSTNode: searches for the matching node in the tree.
9153
+ * - An entry tuple: searches for the key-value pair.
9154
+ * - A NodePredicate function: tests each node and returns true for matches.
9155
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
9156
+ * - null or undefined: treated as no match, returns empty results.
9157
+ *
9158
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
9159
+ * If false (default), searches for and deletes all matching nodes.
9160
+ *
9161
+ * @param startNode - The node to start the search from. Can be:
9162
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
9163
+ * - null or undefined: defaults to the root, searching the entire tree.
9164
+ * - Default value: this._root (the tree's root).
9165
+ *
9166
+ * @param iterationType - Controls the internal traversal implementation:
9167
+ * - 'RECURSIVE': uses recursive function calls for traversal.
9168
+ * - 'ITERATIVE': uses explicit stack-based iteration.
9169
+ * - Default: this.iterationType (the tree's default iteration mode).
9170
+ *
9171
+ * @returns A Map<K, boolean> containing the deletion results:
9172
+ * - Key: the matched node's key.
9173
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
9174
+ * - If no nodes match the search criteria, the returned map is empty.
9175
+ */
9176
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
9177
+ const toDelete = this.search(
9178
+ keyNodeEntryOrPredicate,
9179
+ onlyOne,
9180
+ (node) => node,
9181
+ startNode,
9182
+ iterationType
9183
+ );
9184
+ let results = [];
9185
+ for (const node of toDelete) {
9186
+ const deleteInfo = this.delete(node);
9187
+ results = results.concat(deleteInfo);
9177
9188
  }
9178
- return false;
9189
+ return results;
9179
9190
  }
9180
9191
  /**
9181
9192
  * (Protected) Core bound search implementation supporting all parameter types.
@@ -9328,8 +9339,7 @@ var _BST = class _BST extends BinaryTree {
9328
9339
  _snapshotOptions() {
9329
9340
  return {
9330
9341
  ...super._snapshotOptions(),
9331
- specifyComparable: this.specifyComparable,
9332
- isReverse: this.isReverse
9342
+ comparator: this._comparator
9333
9343
  };
9334
9344
  }
9335
9345
  /**
@@ -9357,14 +9367,14 @@ var _BST = class _BST extends BinaryTree {
9357
9367
  }
9358
9368
  /**
9359
9369
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
9360
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
9370
+ * @remarks Time O(1) Space O(1)
9361
9371
  *
9362
9372
  * @param a - The first key.
9363
9373
  * @param b - The second key.
9364
9374
  * @returns A number (1, -1, or 0) representing the comparison.
9365
9375
  */
9366
9376
  _compare(a, b) {
9367
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
9377
+ return this._comparator(a, b);
9368
9378
  }
9369
9379
  /**
9370
9380
  * (Private) Deletes a node by its key.