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
@@ -159,6 +159,14 @@ var _IterableEntryBase = class _IterableEntryBase {
159
159
  }
160
160
  return accumulator;
161
161
  }
162
+ /**
163
+ * Converts data structure to `[key, value]` pairs.
164
+ * @returns Array of entries.
165
+ * @remarks Time O(n), Space O(n)
166
+ */
167
+ toArray() {
168
+ return [...this];
169
+ }
162
170
  /**
163
171
  * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
164
172
  * @returns Array of entries (default) or a string.
@@ -172,7 +180,6 @@ var _IterableEntryBase = class _IterableEntryBase {
172
180
  * @remarks Time O(n), Space O(n)
173
181
  */
174
182
  print() {
175
- console.log(this.toVisual());
176
183
  }
177
184
  };
178
185
  __name(_IterableEntryBase, "IterableEntryBase");
@@ -394,7 +401,6 @@ var _IterableElementBase = class _IterableElementBase {
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
  __name(_IterableElementBase, "IterableElementBase");
@@ -6814,8 +6820,6 @@ var _Range = class _Range {
6814
6820
  this.high = high;
6815
6821
  this.includeLow = includeLow;
6816
6822
  this.includeHigh = includeHigh;
6817
- if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
6818
- if (low > high) throw new RangeError("low must be less than or equal to high");
6819
6823
  }
6820
6824
  // Determine whether a key is within the range
6821
6825
  isInRange(key, comparator) {
@@ -8063,7 +8067,6 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
8063
8067
  * @param [startNode=this._root] - The node to start printing from.
8064
8068
  */
8065
8069
  print(options, startNode = this._root) {
8066
- console.log(this.toVisual(startNode, options));
8067
8070
  }
8068
8071
  /**
8069
8072
  * (Protected) Core DFS implementation.
@@ -8642,27 +8645,6 @@ var _BST = class _BST extends BinaryTree {
8642
8645
  get root() {
8643
8646
  return this._root;
8644
8647
  }
8645
- /**
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.
8649
- */
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
- };
8665
- }
8666
8648
  /**
8667
8649
  * Gets the comparator function used by the tree.
8668
8650
  * @remarks Time O(1)
@@ -8980,31 +8962,141 @@ var _BST = class _BST extends BinaryTree {
8980
8962
  else _iterate();
8981
8963
  return inserted;
8982
8964
  }
8983
- /**
8984
- * Returns the first node with a key greater than or equal to the given key.
8985
- * This is equivalent to C++ std::lower_bound on a BST.
8986
- * Supports RECURSIVE and ITERATIVE implementations.
8987
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
8988
- * Space Complexity: O(h) for recursion, O(1) for iteration.
8989
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
8990
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
8991
- * @returns The first node with key >= given key, or undefined if no such node exists.
8992
- */
8993
- lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
8994
- return this._bound(keyNodeEntryOrPredicate, true, iterationType);
8965
+ ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
8966
+ let actualCallback = void 0;
8967
+ let actualIterationType = this.iterationType;
8968
+ if (typeof callback === "string") {
8969
+ actualIterationType = callback;
8970
+ } else if (callback) {
8971
+ actualCallback = callback;
8972
+ if (iterationType) {
8973
+ actualIterationType = iterationType;
8974
+ }
8975
+ }
8976
+ const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
8977
+ if (!actualCallback) {
8978
+ return node == null ? void 0 : node.key;
8979
+ }
8980
+ return node ? actualCallback(node) : void 0;
8995
8981
  }
8996
- /**
8997
- * Returns the first node with a key strictly greater than the given key.
8998
- * This is equivalent to C++ std::upper_bound on a BST.
8999
- * Supports RECURSIVE and ITERATIVE implementations.
9000
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
9001
- * Space Complexity: O(h) for recursion, O(1) for iteration.
9002
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9003
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
9004
- * @returns The first node with key > given key, or undefined if no such node exists.
9005
- */
9006
- upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9007
- return this._bound(keyNodeEntryOrPredicate, false, iterationType);
8982
+ higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
8983
+ let actualCallback = void 0;
8984
+ let actualIterationType = this.iterationType;
8985
+ if (typeof callback === "string") {
8986
+ actualIterationType = callback;
8987
+ } else if (callback) {
8988
+ actualCallback = callback;
8989
+ if (iterationType) {
8990
+ actualIterationType = iterationType;
8991
+ }
8992
+ }
8993
+ const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
8994
+ if (!actualCallback) {
8995
+ return node == null ? void 0 : node.key;
8996
+ }
8997
+ return node ? actualCallback(node) : void 0;
8998
+ }
8999
+ floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
9000
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9001
+ if (typeof callback === "string" || !callback) {
9002
+ return void 0;
9003
+ }
9004
+ return void 0;
9005
+ }
9006
+ let actualCallback = void 0;
9007
+ let actualIterationType = this.iterationType;
9008
+ if (typeof callback === "string") {
9009
+ actualIterationType = callback;
9010
+ } else if (callback) {
9011
+ actualCallback = callback;
9012
+ if (iterationType) {
9013
+ actualIterationType = iterationType;
9014
+ }
9015
+ }
9016
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9017
+ const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
9018
+ if (!actualCallback) {
9019
+ return node == null ? void 0 : node.key;
9020
+ }
9021
+ return node ? actualCallback(node) : void 0;
9022
+ }
9023
+ let targetKey;
9024
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9025
+ targetKey = keyNodeEntryOrPredicate.key;
9026
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9027
+ const key = keyNodeEntryOrPredicate[0];
9028
+ if (key === null || key === void 0) {
9029
+ if (typeof callback === "string" || !callback) {
9030
+ return void 0;
9031
+ }
9032
+ return void 0;
9033
+ }
9034
+ targetKey = key;
9035
+ } else {
9036
+ targetKey = keyNodeEntryOrPredicate;
9037
+ }
9038
+ if (targetKey !== void 0) {
9039
+ const node = this._floorByKey(targetKey, actualIterationType);
9040
+ if (!actualCallback) {
9041
+ return node == null ? void 0 : node.key;
9042
+ }
9043
+ return node ? actualCallback(node) : void 0;
9044
+ }
9045
+ if (typeof callback === "string" || !callback) {
9046
+ return void 0;
9047
+ }
9048
+ return void 0;
9049
+ }
9050
+ lower(keyNodeEntryOrPredicate, callback, iterationType) {
9051
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9052
+ if (typeof callback === "string" || !callback) {
9053
+ return void 0;
9054
+ }
9055
+ return void 0;
9056
+ }
9057
+ let actualCallback = void 0;
9058
+ let actualIterationType = this.iterationType;
9059
+ if (typeof callback === "string") {
9060
+ actualIterationType = callback;
9061
+ } else if (callback) {
9062
+ actualCallback = callback;
9063
+ if (iterationType) {
9064
+ actualIterationType = iterationType;
9065
+ }
9066
+ }
9067
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9068
+ const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
9069
+ if (!actualCallback) {
9070
+ return node == null ? void 0 : node.key;
9071
+ }
9072
+ return node ? actualCallback(node) : void 0;
9073
+ }
9074
+ let targetKey;
9075
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9076
+ targetKey = keyNodeEntryOrPredicate.key;
9077
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9078
+ const key = keyNodeEntryOrPredicate[0];
9079
+ if (key === null || key === void 0) {
9080
+ if (typeof callback === "string" || !callback) {
9081
+ return void 0;
9082
+ }
9083
+ return void 0;
9084
+ }
9085
+ targetKey = key;
9086
+ } else {
9087
+ targetKey = keyNodeEntryOrPredicate;
9088
+ }
9089
+ if (targetKey !== void 0) {
9090
+ const node = this._lowerByKey(targetKey, actualIterationType);
9091
+ if (!actualCallback) {
9092
+ return node == null ? void 0 : node.key;
9093
+ }
9094
+ return node ? actualCallback(node) : void 0;
9095
+ }
9096
+ if (typeof callback === "string" || !callback) {
9097
+ return void 0;
9098
+ }
9099
+ return void 0;
9008
9100
  }
9009
9101
  /**
9010
9102
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
@@ -9176,13 +9268,7 @@ var _BST = class _BST extends BinaryTree {
9176
9268
  * - If no nodes match the search criteria, the returned map is empty.
9177
9269
  */
9178
9270
  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
- );
9271
+ const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
9186
9272
  let results = [];
9187
9273
  for (const node of toDelete) {
9188
9274
  const deleteInfo = this.delete(node);
@@ -9190,6 +9276,192 @@ var _BST = class _BST extends BinaryTree {
9190
9276
  }
9191
9277
  return results;
9192
9278
  }
9279
+ /**
9280
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
9281
+ * @remarks Time O(1) Space O(1)
9282
+ * @returns The default comparator function.
9283
+ */
9284
+ _createDefaultComparator() {
9285
+ return (a, b) => {
9286
+ if (isComparable(a) && isComparable(b)) {
9287
+ if (a > b) return 1;
9288
+ if (a < b) return -1;
9289
+ return 0;
9290
+ }
9291
+ if (typeof a === "object" || typeof b === "object") {
9292
+ throw TypeError(
9293
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
9294
+ );
9295
+ }
9296
+ return 0;
9297
+ };
9298
+ }
9299
+ /**
9300
+ * (Protected) Binary search for floor by key with pruning optimization.
9301
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9302
+ * Finds first node where key <= target.
9303
+ * @remarks Time O(h) where h is tree height.
9304
+ *
9305
+ * @param key - The target key to search for.
9306
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9307
+ * @returns The first node with key <= target, or undefined if none exists.
9308
+ */
9309
+ _floorByKey(key, iterationType) {
9310
+ var _a, _b;
9311
+ if (iterationType === "RECURSIVE") {
9312
+ const dfs = /* @__PURE__ */ __name((cur) => {
9313
+ if (!this.isRealNode(cur)) return void 0;
9314
+ const cmp = this.comparator(cur.key, key);
9315
+ if (cmp <= 0) {
9316
+ const rightResult = dfs(cur.right);
9317
+ return rightResult != null ? rightResult : cur;
9318
+ } else {
9319
+ return dfs(cur.left);
9320
+ }
9321
+ }, "dfs");
9322
+ return dfs(this.root);
9323
+ } else {
9324
+ let current = this.root;
9325
+ let result = void 0;
9326
+ while (this.isRealNode(current)) {
9327
+ const cmp = this.comparator(current.key, key);
9328
+ if (cmp <= 0) {
9329
+ result = current;
9330
+ current = (_a = current.right) != null ? _a : void 0;
9331
+ } else {
9332
+ current = (_b = current.left) != null ? _b : void 0;
9333
+ }
9334
+ }
9335
+ return result;
9336
+ }
9337
+ }
9338
+ /**
9339
+ * (Protected) In-order traversal search for floor by predicate.
9340
+ * Falls back to linear in-order traversal when predicate-based search is required.
9341
+ * Returns the last node that satisfies the predicate function.
9342
+ * @remarks Time Complexity: O(n) since it may visit every node.
9343
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
9344
+ *
9345
+ * @param predicate - The predicate function to test nodes.
9346
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9347
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
9348
+ */
9349
+ _floorByPredicate(predicate, iterationType) {
9350
+ if (iterationType === "RECURSIVE") {
9351
+ let result = void 0;
9352
+ const dfs = /* @__PURE__ */ __name((cur) => {
9353
+ if (!this.isRealNode(cur)) return;
9354
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9355
+ if (predicate(cur)) {
9356
+ result = cur;
9357
+ }
9358
+ if (this.isRealNode(cur.right)) dfs(cur.right);
9359
+ }, "dfs");
9360
+ dfs(this.root);
9361
+ return result;
9362
+ } else {
9363
+ const stack = [];
9364
+ let current = this.root;
9365
+ let result = void 0;
9366
+ while (stack.length > 0 || this.isRealNode(current)) {
9367
+ if (this.isRealNode(current)) {
9368
+ stack.push(current);
9369
+ current = current.left;
9370
+ } else {
9371
+ const node = stack.pop();
9372
+ if (!this.isRealNode(node)) break;
9373
+ if (predicate(node)) {
9374
+ result = node;
9375
+ }
9376
+ current = node.right;
9377
+ }
9378
+ }
9379
+ return result;
9380
+ }
9381
+ }
9382
+ /**
9383
+ * (Protected) Binary search for lower by key with pruning optimization.
9384
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9385
+ * Finds first node where key < target.
9386
+ * @remarks Time O(h) where h is tree height.
9387
+ *
9388
+ * @param key - The target key to search for.
9389
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9390
+ * @returns The first node with key < target, or undefined if none exists.
9391
+ */
9392
+ _lowerByKey(key, iterationType) {
9393
+ var _a, _b;
9394
+ if (iterationType === "RECURSIVE") {
9395
+ const dfs = /* @__PURE__ */ __name((cur) => {
9396
+ if (!this.isRealNode(cur)) return void 0;
9397
+ const cmp = this.comparator(cur.key, key);
9398
+ if (cmp < 0) {
9399
+ const rightResult = dfs(cur.right);
9400
+ return rightResult != null ? rightResult : cur;
9401
+ } else {
9402
+ return dfs(cur.left);
9403
+ }
9404
+ }, "dfs");
9405
+ return dfs(this.root);
9406
+ } else {
9407
+ let current = this.root;
9408
+ let result = void 0;
9409
+ while (this.isRealNode(current)) {
9410
+ const cmp = this.comparator(current.key, key);
9411
+ if (cmp < 0) {
9412
+ result = current;
9413
+ current = (_a = current.right) != null ? _a : void 0;
9414
+ } else {
9415
+ current = (_b = current.left) != null ? _b : void 0;
9416
+ }
9417
+ }
9418
+ return result;
9419
+ }
9420
+ }
9421
+ /**
9422
+ * (Protected) In-order traversal search for lower by predicate.
9423
+ * Falls back to linear in-order traversal when predicate-based search is required.
9424
+ * Returns the node that satisfies the predicate and appears last in in-order traversal.
9425
+ * @remarks Time Complexity: O(n) since it may visit every node.
9426
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
9427
+ *
9428
+ * @param predicate - The predicate function to test nodes.
9429
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9430
+ * @returns The last node satisfying predicate (highest key < target), or undefined if none found.
9431
+ */
9432
+ _lowerByPredicate(predicate, iterationType) {
9433
+ if (iterationType === "RECURSIVE") {
9434
+ let result = void 0;
9435
+ const dfs = /* @__PURE__ */ __name((cur) => {
9436
+ if (!this.isRealNode(cur)) return;
9437
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9438
+ if (predicate(cur)) {
9439
+ result = cur;
9440
+ }
9441
+ if (this.isRealNode(cur.right)) dfs(cur.right);
9442
+ }, "dfs");
9443
+ dfs(this.root);
9444
+ return result;
9445
+ } else {
9446
+ const stack = [];
9447
+ let current = this.root;
9448
+ let result = void 0;
9449
+ while (stack.length > 0 || this.isRealNode(current)) {
9450
+ if (this.isRealNode(current)) {
9451
+ stack.push(current);
9452
+ current = current.left;
9453
+ } else {
9454
+ const node = stack.pop();
9455
+ if (!this.isRealNode(node)) break;
9456
+ if (predicate(node)) {
9457
+ result = node;
9458
+ }
9459
+ current = node.right;
9460
+ }
9461
+ }
9462
+ return result;
9463
+ }
9464
+ }
9193
9465
  /**
9194
9466
  * (Protected) Core bound search implementation supporting all parameter types.
9195
9467
  * Unified logic for both lowerBound and upperBound.