data-structure-typed 2.2.4 → 2.2.6

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 (30) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/index.cjs +327 -57
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +329 -57
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +327 -57
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +329 -57
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/base/iterable-entry-base.d.ts +6 -0
  11. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +14 -57
  12. package/dist/types/data-structures/binary-tree/bst.d.ts +110 -96
  13. package/dist/umd/data-structure-typed.js +329 -57
  14. package/dist/umd/data-structure-typed.js.map +1 -1
  15. package/dist/umd/data-structure-typed.min.js +3 -3
  16. package/dist/umd/data-structure-typed.min.js.map +1 -1
  17. package/package.json +5 -3
  18. package/src/common/index.ts +2 -4
  19. package/src/data-structures/base/iterable-entry-base.ts +9 -0
  20. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +3 -1
  21. package/src/data-structures/binary-tree/binary-tree.ts +67 -0
  22. package/src/data-structures/binary-tree/bst.ts +658 -71
  23. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +1 -1
  24. package/test/unit/data-structures/binary-tree/bst.test.ts +1385 -318
  25. package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
  26. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
  27. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +13 -11
  28. package/tsup.config.js +6 -0
  29. package/tsup.leetcode.config.js +3 -0
  30. package/tsup.node.config.js +12 -0
package/CHANGELOG.md CHANGED
@@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
8
8
  - [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
9
9
  - [`auto-changelog`](https://github.com/CookPete/auto-changelog)
10
10
 
11
- ## [v2.2.4](https://github.com/zrwusa/data-structure-typed/compare/v2.2.3...main) (upcoming)
11
+ ## [v2.2.6](https://github.com/zrwusa/data-structure-typed/compare/v2.2.3...main) (upcoming)
12
12
 
13
13
  ## [v2.2.3](https://github.com/zrwusa/data-structure-typed/compare/v2.2.2...v2.2.3) (6 January 2026)
14
14
 
@@ -160,6 +160,14 @@ var IterableEntryBase = class {
160
160
  }
161
161
  return accumulator;
162
162
  }
163
+ /**
164
+ * Converts data structure to `[key, value]` pairs.
165
+ * @returns Array of entries.
166
+ * @remarks Time O(n), Space O(n)
167
+ */
168
+ toArray() {
169
+ return [...this];
170
+ }
163
171
  /**
164
172
  * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
165
173
  * @returns Array of entries (default) or a string.
@@ -173,7 +181,6 @@ var IterableEntryBase = class {
173
181
  * @remarks Time O(n), Space O(n)
174
182
  */
175
183
  print() {
176
- console.log(this.toVisual());
177
184
  }
178
185
  };
179
186
 
@@ -396,7 +403,6 @@ var IterableElementBase = class {
396
403
  * Time O(n) due to materialization, Space O(n) for the intermediate representation.
397
404
  */
398
405
  print() {
399
- console.log(this.toVisual());
400
406
  }
401
407
  };
402
408
 
@@ -6830,8 +6836,6 @@ var Range = class {
6830
6836
  this.high = high;
6831
6837
  this.includeLow = includeLow;
6832
6838
  this.includeHigh = includeHigh;
6833
- if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
6834
- if (low > high) throw new RangeError("low must be less than or equal to high");
6835
6839
  }
6836
6840
  static {
6837
6841
  __name(this, "Range");
@@ -8075,7 +8079,6 @@ var BinaryTree = class extends IterableEntryBase {
8075
8079
  * @param [startNode=this._root] - The node to start printing from.
8076
8080
  */
8077
8081
  print(options, startNode = this._root) {
8078
- console.log(this.toVisual(startNode, options));
8079
8082
  }
8080
8083
  /**
8081
8084
  * (Protected) Core DFS implementation.
@@ -8656,27 +8659,6 @@ var BST = class extends BinaryTree {
8656
8659
  get root() {
8657
8660
  return this._root;
8658
8661
  }
8659
- /**
8660
- * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
8661
- * @remarks Time O(1) Space O(1)
8662
- * @returns The default comparator function.
8663
- */
8664
- _createDefaultComparator() {
8665
- return (a, b) => {
8666
- debugger;
8667
- if (isComparable(a) && isComparable(b)) {
8668
- if (a > b) return 1;
8669
- if (a < b) return -1;
8670
- return 0;
8671
- }
8672
- if (typeof a === "object" || typeof b === "object") {
8673
- throw TypeError(
8674
- `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
8675
- );
8676
- }
8677
- return 0;
8678
- };
8679
- }
8680
8662
  /**
8681
8663
  * The comparator function used to determine the order of keys in the tree.
8682
8664
 
@@ -8998,31 +8980,141 @@ var BST = class extends BinaryTree {
8998
8980
  else _iterate();
8999
8981
  return inserted;
9000
8982
  }
9001
- /**
9002
- * Returns the first node with a key greater than or equal to the given key.
9003
- * This is equivalent to C++ std::lower_bound on a BST.
9004
- * Supports RECURSIVE and ITERATIVE implementations.
9005
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
9006
- * Space Complexity: O(h) for recursion, O(1) for iteration.
9007
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9008
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
9009
- * @returns The first node with key >= given key, or undefined if no such node exists.
9010
- */
9011
- lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9012
- return this._bound(keyNodeEntryOrPredicate, true, iterationType);
8983
+ ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
8984
+ let actualCallback = void 0;
8985
+ let actualIterationType = this.iterationType;
8986
+ if (typeof callback === "string") {
8987
+ actualIterationType = callback;
8988
+ } else if (callback) {
8989
+ actualCallback = callback;
8990
+ if (iterationType) {
8991
+ actualIterationType = iterationType;
8992
+ }
8993
+ }
8994
+ const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
8995
+ if (!actualCallback) {
8996
+ return node?.key;
8997
+ }
8998
+ return node ? actualCallback(node) : void 0;
9013
8999
  }
9014
- /**
9015
- * Returns the first node with a key strictly greater than the given key.
9016
- * This is equivalent to C++ std::upper_bound on a BST.
9017
- * Supports RECURSIVE and ITERATIVE implementations.
9018
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
9019
- * Space Complexity: O(h) for recursion, O(1) for iteration.
9020
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9021
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
9022
- * @returns The first node with key > given key, or undefined if no such node exists.
9023
- */
9024
- upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9025
- return this._bound(keyNodeEntryOrPredicate, false, iterationType);
9000
+ higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
9001
+ let actualCallback = void 0;
9002
+ let actualIterationType = this.iterationType;
9003
+ if (typeof callback === "string") {
9004
+ actualIterationType = callback;
9005
+ } else if (callback) {
9006
+ actualCallback = callback;
9007
+ if (iterationType) {
9008
+ actualIterationType = iterationType;
9009
+ }
9010
+ }
9011
+ const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
9012
+ if (!actualCallback) {
9013
+ return node?.key;
9014
+ }
9015
+ return node ? actualCallback(node) : void 0;
9016
+ }
9017
+ floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
9018
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9019
+ if (typeof callback === "string" || !callback) {
9020
+ return void 0;
9021
+ }
9022
+ return void 0;
9023
+ }
9024
+ let actualCallback = void 0;
9025
+ let actualIterationType = this.iterationType;
9026
+ if (typeof callback === "string") {
9027
+ actualIterationType = callback;
9028
+ } else if (callback) {
9029
+ actualCallback = callback;
9030
+ if (iterationType) {
9031
+ actualIterationType = iterationType;
9032
+ }
9033
+ }
9034
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9035
+ const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
9036
+ if (!actualCallback) {
9037
+ return node?.key;
9038
+ }
9039
+ return node ? actualCallback(node) : void 0;
9040
+ }
9041
+ let targetKey;
9042
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9043
+ targetKey = keyNodeEntryOrPredicate.key;
9044
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9045
+ const key = keyNodeEntryOrPredicate[0];
9046
+ if (key === null || key === void 0) {
9047
+ if (typeof callback === "string" || !callback) {
9048
+ return void 0;
9049
+ }
9050
+ return void 0;
9051
+ }
9052
+ targetKey = key;
9053
+ } else {
9054
+ targetKey = keyNodeEntryOrPredicate;
9055
+ }
9056
+ if (targetKey !== void 0) {
9057
+ const node = this._floorByKey(targetKey, actualIterationType);
9058
+ if (!actualCallback) {
9059
+ return node?.key;
9060
+ }
9061
+ return node ? actualCallback(node) : void 0;
9062
+ }
9063
+ if (typeof callback === "string" || !callback) {
9064
+ return void 0;
9065
+ }
9066
+ return void 0;
9067
+ }
9068
+ lower(keyNodeEntryOrPredicate, callback, iterationType) {
9069
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9070
+ if (typeof callback === "string" || !callback) {
9071
+ return void 0;
9072
+ }
9073
+ return void 0;
9074
+ }
9075
+ let actualCallback = void 0;
9076
+ let actualIterationType = this.iterationType;
9077
+ if (typeof callback === "string") {
9078
+ actualIterationType = callback;
9079
+ } else if (callback) {
9080
+ actualCallback = callback;
9081
+ if (iterationType) {
9082
+ actualIterationType = iterationType;
9083
+ }
9084
+ }
9085
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9086
+ const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
9087
+ if (!actualCallback) {
9088
+ return node?.key;
9089
+ }
9090
+ return node ? actualCallback(node) : void 0;
9091
+ }
9092
+ let targetKey;
9093
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9094
+ targetKey = keyNodeEntryOrPredicate.key;
9095
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9096
+ const key = keyNodeEntryOrPredicate[0];
9097
+ if (key === null || key === void 0) {
9098
+ if (typeof callback === "string" || !callback) {
9099
+ return void 0;
9100
+ }
9101
+ return void 0;
9102
+ }
9103
+ targetKey = key;
9104
+ } else {
9105
+ targetKey = keyNodeEntryOrPredicate;
9106
+ }
9107
+ if (targetKey !== void 0) {
9108
+ const node = this._lowerByKey(targetKey, actualIterationType);
9109
+ if (!actualCallback) {
9110
+ return node?.key;
9111
+ }
9112
+ return node ? actualCallback(node) : void 0;
9113
+ }
9114
+ if (typeof callback === "string" || !callback) {
9115
+ return void 0;
9116
+ }
9117
+ return void 0;
9026
9118
  }
9027
9119
  /**
9028
9120
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
@@ -9194,13 +9286,7 @@ var BST = class extends BinaryTree {
9194
9286
  * - If no nodes match the search criteria, the returned map is empty.
9195
9287
  */
9196
9288
  deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
9197
- const toDelete = this.search(
9198
- keyNodeEntryOrPredicate,
9199
- onlyOne,
9200
- (node) => node,
9201
- startNode,
9202
- iterationType
9203
- );
9289
+ const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
9204
9290
  let results = [];
9205
9291
  for (const node of toDelete) {
9206
9292
  const deleteInfo = this.delete(node);
@@ -9208,6 +9294,190 @@ var BST = class extends BinaryTree {
9208
9294
  }
9209
9295
  return results;
9210
9296
  }
9297
+ /**
9298
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
9299
+ * @remarks Time O(1) Space O(1)
9300
+ * @returns The default comparator function.
9301
+ */
9302
+ _createDefaultComparator() {
9303
+ return (a, b) => {
9304
+ if (isComparable(a) && isComparable(b)) {
9305
+ if (a > b) return 1;
9306
+ if (a < b) return -1;
9307
+ return 0;
9308
+ }
9309
+ if (typeof a === "object" || typeof b === "object") {
9310
+ throw TypeError(
9311
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
9312
+ );
9313
+ }
9314
+ return 0;
9315
+ };
9316
+ }
9317
+ /**
9318
+ * (Protected) Binary search for floor by key with pruning optimization.
9319
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9320
+ * Finds first node where key <= target.
9321
+ * @remarks Time O(h) where h is tree height.
9322
+ *
9323
+ * @param key - The target key to search for.
9324
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9325
+ * @returns The first node with key <= target, or undefined if none exists.
9326
+ */
9327
+ _floorByKey(key, iterationType) {
9328
+ if (iterationType === "RECURSIVE") {
9329
+ const dfs = /* @__PURE__ */ __name((cur) => {
9330
+ if (!this.isRealNode(cur)) return void 0;
9331
+ const cmp = this.comparator(cur.key, key);
9332
+ if (cmp <= 0) {
9333
+ const rightResult = dfs(cur.right);
9334
+ return rightResult ?? cur;
9335
+ } else {
9336
+ return dfs(cur.left);
9337
+ }
9338
+ }, "dfs");
9339
+ return dfs(this.root);
9340
+ } else {
9341
+ let current = this.root;
9342
+ let result = void 0;
9343
+ while (this.isRealNode(current)) {
9344
+ const cmp = this.comparator(current.key, key);
9345
+ if (cmp <= 0) {
9346
+ result = current;
9347
+ current = current.right ?? void 0;
9348
+ } else {
9349
+ current = current.left ?? void 0;
9350
+ }
9351
+ }
9352
+ return result;
9353
+ }
9354
+ }
9355
+ /**
9356
+ * (Protected) In-order traversal search for floor by predicate.
9357
+ * Falls back to linear in-order traversal when predicate-based search is required.
9358
+ * Returns the last node that satisfies the predicate function.
9359
+ * @remarks Time Complexity: O(n) since it may visit every node.
9360
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
9361
+ *
9362
+ * @param predicate - The predicate function to test nodes.
9363
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9364
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
9365
+ */
9366
+ _floorByPredicate(predicate, iterationType) {
9367
+ if (iterationType === "RECURSIVE") {
9368
+ let result = void 0;
9369
+ const dfs = /* @__PURE__ */ __name((cur) => {
9370
+ if (!this.isRealNode(cur)) return;
9371
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9372
+ if (predicate(cur)) {
9373
+ result = cur;
9374
+ }
9375
+ if (this.isRealNode(cur.right)) dfs(cur.right);
9376
+ }, "dfs");
9377
+ dfs(this.root);
9378
+ return result;
9379
+ } else {
9380
+ const stack = [];
9381
+ let current = this.root;
9382
+ let result = void 0;
9383
+ while (stack.length > 0 || this.isRealNode(current)) {
9384
+ if (this.isRealNode(current)) {
9385
+ stack.push(current);
9386
+ current = current.left;
9387
+ } else {
9388
+ const node = stack.pop();
9389
+ if (!this.isRealNode(node)) break;
9390
+ if (predicate(node)) {
9391
+ result = node;
9392
+ }
9393
+ current = node.right;
9394
+ }
9395
+ }
9396
+ return result;
9397
+ }
9398
+ }
9399
+ /**
9400
+ * (Protected) Binary search for lower by key with pruning optimization.
9401
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9402
+ * Finds first node where key < target.
9403
+ * @remarks Time O(h) where h is tree height.
9404
+ *
9405
+ * @param key - The target key to search for.
9406
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9407
+ * @returns The first node with key < target, or undefined if none exists.
9408
+ */
9409
+ _lowerByKey(key, iterationType) {
9410
+ if (iterationType === "RECURSIVE") {
9411
+ const dfs = /* @__PURE__ */ __name((cur) => {
9412
+ if (!this.isRealNode(cur)) return void 0;
9413
+ const cmp = this.comparator(cur.key, key);
9414
+ if (cmp < 0) {
9415
+ const rightResult = dfs(cur.right);
9416
+ return rightResult ?? cur;
9417
+ } else {
9418
+ return dfs(cur.left);
9419
+ }
9420
+ }, "dfs");
9421
+ return dfs(this.root);
9422
+ } else {
9423
+ let current = this.root;
9424
+ let result = void 0;
9425
+ while (this.isRealNode(current)) {
9426
+ const cmp = this.comparator(current.key, key);
9427
+ if (cmp < 0) {
9428
+ result = current;
9429
+ current = current.right ?? void 0;
9430
+ } else {
9431
+ current = current.left ?? void 0;
9432
+ }
9433
+ }
9434
+ return result;
9435
+ }
9436
+ }
9437
+ /**
9438
+ * (Protected) In-order traversal search for lower by predicate.
9439
+ * Falls back to linear in-order traversal when predicate-based search is required.
9440
+ * Returns the node that satisfies the predicate and appears last in in-order traversal.
9441
+ * @remarks Time Complexity: O(n) since it may visit every node.
9442
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
9443
+ *
9444
+ * @param predicate - The predicate function to test nodes.
9445
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9446
+ * @returns The last node satisfying predicate (highest key < target), or undefined if none found.
9447
+ */
9448
+ _lowerByPredicate(predicate, iterationType) {
9449
+ if (iterationType === "RECURSIVE") {
9450
+ let result = void 0;
9451
+ const dfs = /* @__PURE__ */ __name((cur) => {
9452
+ if (!this.isRealNode(cur)) return;
9453
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9454
+ if (predicate(cur)) {
9455
+ result = cur;
9456
+ }
9457
+ if (this.isRealNode(cur.right)) dfs(cur.right);
9458
+ }, "dfs");
9459
+ dfs(this.root);
9460
+ return result;
9461
+ } else {
9462
+ const stack = [];
9463
+ let current = this.root;
9464
+ let result = void 0;
9465
+ while (stack.length > 0 || this.isRealNode(current)) {
9466
+ if (this.isRealNode(current)) {
9467
+ stack.push(current);
9468
+ current = current.left;
9469
+ } else {
9470
+ const node = stack.pop();
9471
+ if (!this.isRealNode(node)) break;
9472
+ if (predicate(node)) {
9473
+ result = node;
9474
+ }
9475
+ current = node.right;
9476
+ }
9477
+ }
9478
+ return result;
9479
+ }
9480
+ }
9211
9481
  /**
9212
9482
  * (Protected) Core bound search implementation supporting all parameter types.
9213
9483
  * Unified logic for both lowerBound and upperBound.