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
@@ -8634,9 +8634,13 @@ var BST = class extends BinaryTree {
8634
8634
  constructor(keysNodesEntriesOrRaws = [], options) {
8635
8635
  super([], options);
8636
8636
  if (options) {
8637
- const { specifyComparable, isReverse } = options;
8638
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
8639
- if (isReverse !== void 0) this._isReverse = isReverse;
8637
+ if ("comparator" in options && options.comparator !== void 0) {
8638
+ this._comparator = options.comparator;
8639
+ } else {
8640
+ this._comparator = this._createDefaultComparator();
8641
+ }
8642
+ } else {
8643
+ this._comparator = this._createDefaultComparator();
8640
8644
  }
8641
8645
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
8642
8646
  }
@@ -8650,40 +8654,33 @@ var BST = class extends BinaryTree {
8650
8654
  get root() {
8651
8655
  return this._root;
8652
8656
  }
8653
- _isReverse = false;
8654
8657
  /**
8655
- * Gets whether the tree's comparison logic is reversed.
8656
- * @remarks Time O(1)
8657
- *
8658
- * @returns True if the tree is reversed (e.g., a max-heap logic).
8658
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
8659
+ * @remarks Time O(1) Space O(1)
8660
+ * @returns The default comparator function.
8659
8661
  */
8660
- get isReverse() {
8661
- return this._isReverse;
8662
+ _createDefaultComparator() {
8663
+ return (a, b) => {
8664
+ debugger;
8665
+ if (isComparable(a) && isComparable(b)) {
8666
+ if (a > b) return 1;
8667
+ if (a < b) return -1;
8668
+ return 0;
8669
+ }
8670
+ if (typeof a === "object" || typeof b === "object") {
8671
+ throw TypeError(
8672
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
8673
+ );
8674
+ }
8675
+ return 0;
8676
+ };
8662
8677
  }
8663
8678
  /**
8664
- * The default comparator function.
8665
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
8666
- */
8667
- _comparator = /* @__PURE__ */ __name((a, b) => {
8668
- if (isComparable(a) && isComparable(b)) {
8669
- if (a > b) return 1;
8670
- if (a < b) return -1;
8671
- return 0;
8672
- }
8673
- if (this._specifyComparable) {
8674
- const va = this._specifyComparable(a);
8675
- const vb = this._specifyComparable(b);
8676
- if (va > vb) return 1;
8677
- if (va < vb) return -1;
8678
- return 0;
8679
- }
8680
- if (typeof a === "object" || typeof b === "object") {
8681
- throw TypeError(
8682
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
8683
- );
8684
- }
8685
- return 0;
8686
- }, "_comparator");
8679
+ * The comparator function used to determine the order of keys in the tree.
8680
+
8681
+ * @remarks Time O(1) Space O(1)
8682
+ */
8683
+ _comparator;
8687
8684
  /**
8688
8685
  * Gets the comparator function used by the tree.
8689
8686
  * @remarks Time O(1)
@@ -8693,16 +8690,6 @@ var BST = class extends BinaryTree {
8693
8690
  get comparator() {
8694
8691
  return this._comparator;
8695
8692
  }
8696
- _specifyComparable;
8697
- /**
8698
- * Gets the function used to extract a comparable value from a complex key.
8699
- * @remarks Time O(1)
8700
- *
8701
- * @returns The key-to-comparable conversion function.
8702
- */
8703
- get specifyComparable() {
8704
- return this._specifyComparable;
8705
- }
8706
8693
  /**
8707
8694
  * (Protected) Creates a new BST node.
8708
8695
  * @remarks Time O(1), Space O(1)
@@ -8743,7 +8730,7 @@ var BST = class extends BinaryTree {
8743
8730
  * @returns True if the key is valid, false otherwise.
8744
8731
  */
8745
8732
  isValidKey(key) {
8746
- return isComparable(key, this._specifyComparable !== void 0);
8733
+ return isComparable(key);
8747
8734
  }
8748
8735
  /**
8749
8736
  * Performs a Depth-First Search (DFS) traversal.
@@ -8832,8 +8819,8 @@ var BST = class extends BinaryTree {
8832
8819
  if (!this.isRealNode(cur.left)) return false;
8833
8820
  if (isRange) {
8834
8821
  const range = keyNodeEntryOrPredicate;
8835
- const leftS = this.isReverse ? range.high : range.low;
8836
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
8822
+ const leftS = range.low;
8823
+ const leftI = range.includeLow;
8837
8824
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
8838
8825
  }
8839
8826
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -8847,8 +8834,8 @@ var BST = class extends BinaryTree {
8847
8834
  if (!this.isRealNode(cur.right)) return false;
8848
8835
  if (isRange) {
8849
8836
  const range = keyNodeEntryOrPredicate;
8850
- const rightS = this.isReverse ? range.low : range.high;
8851
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
8837
+ const rightS = range.high;
8838
+ const rightI = range.includeHigh;
8852
8839
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
8853
8840
  }
8854
8841
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -9169,31 +9156,55 @@ var BST = class extends BinaryTree {
9169
9156
  return out;
9170
9157
  }
9171
9158
  /**
9172
- * Deletes the first node found that satisfies the predicate.
9173
- * @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.
9159
+ * Deletes nodes that match a key, node, entry, predicate, or range.
9174
9160
  *
9175
- * @param predicate - A function to test each [key, value] pair.
9176
- * @returns True if a node was deleted, false otherwise.
9177
- */
9178
- deleteWhere(predicate) {
9179
- const stack = [];
9180
- let cur = this._root;
9181
- let index = 0;
9182
- while (stack.length > 0 || cur !== void 0) {
9183
- while (cur !== void 0 && cur !== null) {
9184
- stack.push(cur);
9185
- cur = cur.left;
9186
- }
9187
- const node = stack.pop();
9188
- if (!node) break;
9189
- const key = node.key;
9190
- const val = node.value;
9191
- if (predicate(key, val, index++, this)) {
9192
- return this._deleteByKey(key);
9193
- }
9194
- cur = node.right;
9161
+ * @remarks
9162
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
9163
+ * Space Complexity: O(M) for storing matched nodes and result map.
9164
+ *
9165
+ * @template K - The key type.
9166
+ * @template V - The value type.
9167
+ *
9168
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
9169
+ * - A key (type K): searches for exact key match using the comparator.
9170
+ * - A BSTNode: searches for the matching node in the tree.
9171
+ * - An entry tuple: searches for the key-value pair.
9172
+ * - A NodePredicate function: tests each node and returns true for matches.
9173
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
9174
+ * - null or undefined: treated as no match, returns empty results.
9175
+ *
9176
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
9177
+ * If false (default), searches for and deletes all matching nodes.
9178
+ *
9179
+ * @param startNode - The node to start the search from. Can be:
9180
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
9181
+ * - null or undefined: defaults to the root, searching the entire tree.
9182
+ * - Default value: this._root (the tree's root).
9183
+ *
9184
+ * @param iterationType - Controls the internal traversal implementation:
9185
+ * - 'RECURSIVE': uses recursive function calls for traversal.
9186
+ * - 'ITERATIVE': uses explicit stack-based iteration.
9187
+ * - Default: this.iterationType (the tree's default iteration mode).
9188
+ *
9189
+ * @returns A Map<K, boolean> containing the deletion results:
9190
+ * - Key: the matched node's key.
9191
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
9192
+ * - If no nodes match the search criteria, the returned map is empty.
9193
+ */
9194
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
9195
+ const toDelete = this.search(
9196
+ keyNodeEntryOrPredicate,
9197
+ onlyOne,
9198
+ (node) => node,
9199
+ startNode,
9200
+ iterationType
9201
+ );
9202
+ let results = [];
9203
+ for (const node of toDelete) {
9204
+ const deleteInfo = this.delete(node);
9205
+ results = results.concat(deleteInfo);
9195
9206
  }
9196
- return false;
9207
+ return results;
9197
9208
  }
9198
9209
  /**
9199
9210
  * (Protected) Core bound search implementation supporting all parameter types.
@@ -9345,8 +9356,7 @@ var BST = class extends BinaryTree {
9345
9356
  _snapshotOptions() {
9346
9357
  return {
9347
9358
  ...super._snapshotOptions(),
9348
- specifyComparable: this.specifyComparable,
9349
- isReverse: this.isReverse
9359
+ comparator: this._comparator
9350
9360
  };
9351
9361
  }
9352
9362
  /**
@@ -9374,14 +9384,14 @@ var BST = class extends BinaryTree {
9374
9384
  }
9375
9385
  /**
9376
9386
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
9377
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
9387
+ * @remarks Time O(1) Space O(1)
9378
9388
  *
9379
9389
  * @param a - The first key.
9380
9390
  * @param b - The second key.
9381
9391
  * @returns A number (1, -1, or 0) representing the comparison.
9382
9392
  */
9383
9393
  _compare(a, b) {
9384
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
9394
+ return this._comparator(a, b);
9385
9395
  }
9386
9396
  /**
9387
9397
  * (Private) Deletes a node by its key.