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