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