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