tree-multimap-typed 2.2.2 → 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 (53) hide show
  1. package/dist/cjs/index.cjs +245 -72
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +246 -72
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +245 -72
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +246 -72
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  10. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -5
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
  15. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  16. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
  17. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  18. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  19. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  20. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  21. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  22. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  23. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  24. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  25. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  26. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  27. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  28. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  29. package/dist/umd/tree-multimap-typed.js +246 -72
  30. package/dist/umd/tree-multimap-typed.js.map +1 -1
  31. package/dist/umd/tree-multimap-typed.min.js +3 -3
  32. package/dist/umd/tree-multimap-typed.min.js.map +1 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  35. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
  36. package/src/data-structures/binary-tree/avl-tree.ts +100 -7
  37. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  38. package/src/data-structures/binary-tree/bst.ts +431 -93
  39. package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
  40. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  41. package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
  42. package/src/data-structures/graph/directed-graph.ts +126 -1
  43. package/src/data-structures/graph/undirected-graph.ts +160 -1
  44. package/src/data-structures/hash/hash-map.ts +110 -27
  45. package/src/data-structures/heap/heap.ts +107 -58
  46. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  47. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  48. package/src/data-structures/queue/deque.ts +95 -67
  49. package/src/data-structures/queue/queue.ts +90 -34
  50. package/src/data-structures/stack/stack.ts +58 -40
  51. package/src/data-structures/trie/trie.ts +109 -47
  52. package/src/interfaces/binary-tree.ts +2 -0
  53. package/src/types/data-structures/binary-tree/bst.ts +5 -5
@@ -1461,6 +1461,17 @@ var BinaryTree = class extends IterableEntryBase {
1461
1461
  }
1462
1462
  return false;
1463
1463
  }
1464
+ /**
1465
+ * Adds or updates a new node to the tree.
1466
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
1467
+ *
1468
+ * @param keyNodeOrEntry - The key, node, or entry to add or update.
1469
+ * @param [value] - The value, if providing just a key.
1470
+ * @returns True if the addition was successful, false otherwise.
1471
+ */
1472
+ set(keyNodeOrEntry, value) {
1473
+ return this.add(keyNodeOrEntry, value);
1474
+ }
1464
1475
  /**
1465
1476
  * Adds multiple items to the tree.
1466
1477
  * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
@@ -1488,6 +1499,17 @@ var BinaryTree = class extends IterableEntryBase {
1488
1499
  }
1489
1500
  return inserted;
1490
1501
  }
1502
+ /**
1503
+ * Adds or updates multiple items to the tree.
1504
+ * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
1505
+ *
1506
+ * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
1507
+ * @param [values] - An optional parallel iterable of values.
1508
+ * @returns An array of booleans indicating the success of each individual `add` operation.
1509
+ */
1510
+ setMany(keysNodesEntriesOrRaws, values) {
1511
+ return this.addMany(keysNodesEntriesOrRaws, values);
1512
+ }
1491
1513
  /**
1492
1514
  * Merges another tree into this one by adding all its nodes.
1493
1515
  * @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).
@@ -2811,9 +2833,13 @@ var BST = class extends BinaryTree {
2811
2833
  constructor(keysNodesEntriesOrRaws = [], options) {
2812
2834
  super([], options);
2813
2835
  if (options) {
2814
- const { specifyComparable, isReverse } = options;
2815
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
2816
- if (isReverse !== void 0) this._isReverse = isReverse;
2836
+ if ("comparator" in options && options.comparator !== void 0) {
2837
+ this._comparator = options.comparator;
2838
+ } else {
2839
+ this._comparator = this._createDefaultComparator();
2840
+ }
2841
+ } else {
2842
+ this._comparator = this._createDefaultComparator();
2817
2843
  }
2818
2844
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
2819
2845
  }
@@ -2827,40 +2853,33 @@ var BST = class extends BinaryTree {
2827
2853
  get root() {
2828
2854
  return this._root;
2829
2855
  }
2830
- _isReverse = false;
2831
2856
  /**
2832
- * Gets whether the tree's comparison logic is reversed.
2833
- * @remarks Time O(1)
2834
- *
2835
- * @returns True if the tree is reversed (e.g., a max-heap logic).
2857
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
2858
+ * @remarks Time O(1) Space O(1)
2859
+ * @returns The default comparator function.
2836
2860
  */
2837
- get isReverse() {
2838
- return this._isReverse;
2861
+ _createDefaultComparator() {
2862
+ return (a, b) => {
2863
+ debugger;
2864
+ if (isComparable(a) && isComparable(b)) {
2865
+ if (a > b) return 1;
2866
+ if (a < b) return -1;
2867
+ return 0;
2868
+ }
2869
+ if (typeof a === "object" || typeof b === "object") {
2870
+ throw TypeError(
2871
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
2872
+ );
2873
+ }
2874
+ return 0;
2875
+ };
2839
2876
  }
2840
2877
  /**
2841
- * The default comparator function.
2842
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
2843
- */
2844
- _comparator = /* @__PURE__ */ __name((a, b) => {
2845
- if (isComparable(a) && isComparable(b)) {
2846
- if (a > b) return 1;
2847
- if (a < b) return -1;
2848
- return 0;
2849
- }
2850
- if (this._specifyComparable) {
2851
- const va = this._specifyComparable(a);
2852
- const vb = this._specifyComparable(b);
2853
- if (va > vb) return 1;
2854
- if (va < vb) return -1;
2855
- return 0;
2856
- }
2857
- if (typeof a === "object" || typeof b === "object") {
2858
- throw TypeError(
2859
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
2860
- );
2861
- }
2862
- return 0;
2863
- }, "_comparator");
2878
+ * The comparator function used to determine the order of keys in the tree.
2879
+
2880
+ * @remarks Time O(1) Space O(1)
2881
+ */
2882
+ _comparator;
2864
2883
  /**
2865
2884
  * Gets the comparator function used by the tree.
2866
2885
  * @remarks Time O(1)
@@ -2870,16 +2889,6 @@ var BST = class extends BinaryTree {
2870
2889
  get comparator() {
2871
2890
  return this._comparator;
2872
2891
  }
2873
- _specifyComparable;
2874
- /**
2875
- * Gets the function used to extract a comparable value from a complex key.
2876
- * @remarks Time O(1)
2877
- *
2878
- * @returns The key-to-comparable conversion function.
2879
- */
2880
- get specifyComparable() {
2881
- return this._specifyComparable;
2882
- }
2883
2892
  /**
2884
2893
  * (Protected) Creates a new BST node.
2885
2894
  * @remarks Time O(1), Space O(1)
@@ -2920,7 +2929,7 @@ var BST = class extends BinaryTree {
2920
2929
  * @returns True if the key is valid, false otherwise.
2921
2930
  */
2922
2931
  isValidKey(key) {
2923
- return isComparable(key, this._specifyComparable !== void 0);
2932
+ return isComparable(key);
2924
2933
  }
2925
2934
  /**
2926
2935
  * Performs a Depth-First Search (DFS) traversal.
@@ -3009,8 +3018,8 @@ var BST = class extends BinaryTree {
3009
3018
  if (!this.isRealNode(cur.left)) return false;
3010
3019
  if (isRange) {
3011
3020
  const range = keyNodeEntryOrPredicate;
3012
- const leftS = this.isReverse ? range.high : range.low;
3013
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
3021
+ const leftS = range.low;
3022
+ const leftI = range.includeLow;
3014
3023
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
3015
3024
  }
3016
3025
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3024,8 +3033,8 @@ var BST = class extends BinaryTree {
3024
3033
  if (!this.isRealNode(cur.right)) return false;
3025
3034
  if (isRange) {
3026
3035
  const range = keyNodeEntryOrPredicate;
3027
- const rightS = this.isReverse ? range.low : range.high;
3028
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
3036
+ const rightS = range.high;
3037
+ const rightI = range.includeHigh;
3029
3038
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
3030
3039
  }
3031
3040
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3186,6 +3195,32 @@ var BST = class extends BinaryTree {
3186
3195
  else _iterate();
3187
3196
  return inserted;
3188
3197
  }
3198
+ /**
3199
+ * Returns the first node with a key greater than or equal to the given key.
3200
+ * This is equivalent to C++ std::lower_bound on a BST.
3201
+ * Supports RECURSIVE and ITERATIVE implementations.
3202
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
3203
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
3204
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3205
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3206
+ * @returns The first node with key >= given key, or undefined if no such node exists.
3207
+ */
3208
+ lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3209
+ return this._bound(keyNodeEntryOrPredicate, true, iterationType);
3210
+ }
3211
+ /**
3212
+ * Returns the first node with a key strictly greater than the given key.
3213
+ * This is equivalent to C++ std::upper_bound on a BST.
3214
+ * Supports RECURSIVE and ITERATIVE implementations.
3215
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
3216
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
3217
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3218
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3219
+ * @returns The first node with key > given key, or undefined if no such node exists.
3220
+ */
3221
+ upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3222
+ return this._bound(keyNodeEntryOrPredicate, false, iterationType);
3223
+ }
3189
3224
  /**
3190
3225
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
3191
3226
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -3320,31 +3355,170 @@ var BST = class extends BinaryTree {
3320
3355
  return out;
3321
3356
  }
3322
3357
  /**
3323
- * Deletes the first node found that satisfies the predicate.
3324
- * @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.
3358
+ * Deletes nodes that match a key, node, entry, predicate, or range.
3325
3359
  *
3326
- * @param predicate - A function to test each [key, value] pair.
3327
- * @returns True if a node was deleted, false otherwise.
3360
+ * @remarks
3361
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
3362
+ * Space Complexity: O(M) for storing matched nodes and result map.
3363
+ *
3364
+ * @template K - The key type.
3365
+ * @template V - The value type.
3366
+ *
3367
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
3368
+ * - A key (type K): searches for exact key match using the comparator.
3369
+ * - A BSTNode: searches for the matching node in the tree.
3370
+ * - An entry tuple: searches for the key-value pair.
3371
+ * - A NodePredicate function: tests each node and returns true for matches.
3372
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
3373
+ * - null or undefined: treated as no match, returns empty results.
3374
+ *
3375
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
3376
+ * If false (default), searches for and deletes all matching nodes.
3377
+ *
3378
+ * @param startNode - The node to start the search from. Can be:
3379
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
3380
+ * - null or undefined: defaults to the root, searching the entire tree.
3381
+ * - Default value: this._root (the tree's root).
3382
+ *
3383
+ * @param iterationType - Controls the internal traversal implementation:
3384
+ * - 'RECURSIVE': uses recursive function calls for traversal.
3385
+ * - 'ITERATIVE': uses explicit stack-based iteration.
3386
+ * - Default: this.iterationType (the tree's default iteration mode).
3387
+ *
3388
+ * @returns A Map<K, boolean> containing the deletion results:
3389
+ * - Key: the matched node's key.
3390
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
3391
+ * - If no nodes match the search criteria, the returned map is empty.
3392
+ */
3393
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
3394
+ const toDelete = this.search(
3395
+ keyNodeEntryOrPredicate,
3396
+ onlyOne,
3397
+ (node) => node,
3398
+ startNode,
3399
+ iterationType
3400
+ );
3401
+ let results = [];
3402
+ for (const node of toDelete) {
3403
+ const deleteInfo = this.delete(node);
3404
+ results = results.concat(deleteInfo);
3405
+ }
3406
+ return results;
3407
+ }
3408
+ /**
3409
+ * (Protected) Core bound search implementation supporting all parameter types.
3410
+ * Unified logic for both lowerBound and upperBound.
3411
+ * Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
3412
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3413
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
3414
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3415
+ * @returns The first matching node, or undefined if no such node exists.
3328
3416
  */
3329
- deleteWhere(predicate) {
3330
- const stack = [];
3331
- let cur = this._root;
3332
- let index = 0;
3333
- while (stack.length > 0 || cur !== void 0) {
3334
- while (cur !== void 0 && cur !== null) {
3335
- stack.push(cur);
3336
- cur = cur.left;
3417
+ _bound(keyNodeEntryOrPredicate, isLower, iterationType) {
3418
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3419
+ return void 0;
3420
+ }
3421
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3422
+ return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
3423
+ }
3424
+ let targetKey;
3425
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3426
+ targetKey = keyNodeEntryOrPredicate.key;
3427
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3428
+ const key = keyNodeEntryOrPredicate[0];
3429
+ if (key === null || key === void 0) {
3430
+ return void 0;
3337
3431
  }
3338
- const node = stack.pop();
3339
- if (!node) break;
3340
- const key = node.key;
3341
- const val = node.value;
3342
- if (predicate(key, val, index++, this)) {
3343
- return this._deleteByKey(key);
3432
+ targetKey = key;
3433
+ } else {
3434
+ targetKey = keyNodeEntryOrPredicate;
3435
+ }
3436
+ if (targetKey !== void 0) {
3437
+ return this._boundByKey(targetKey, isLower, iterationType);
3438
+ }
3439
+ return void 0;
3440
+ }
3441
+ /**
3442
+ * (Protected) Binary search for bound by key with pruning optimization.
3443
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3444
+ * For lowerBound: finds first node where key >= target.
3445
+ * For upperBound: finds first node where key > target.
3446
+ * @param key - The target key to search for.
3447
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
3448
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3449
+ * @returns The first node matching the bound condition, or undefined if none exists.
3450
+ */
3451
+ _boundByKey(key, isLower, iterationType) {
3452
+ if (iterationType === "RECURSIVE") {
3453
+ const dfs = /* @__PURE__ */ __name((cur) => {
3454
+ if (!this.isRealNode(cur)) return void 0;
3455
+ const cmp = this.comparator(cur.key, key);
3456
+ const condition = isLower ? cmp >= 0 : cmp > 0;
3457
+ if (condition) {
3458
+ const leftResult = dfs(cur.left);
3459
+ return leftResult ?? cur;
3460
+ } else {
3461
+ return dfs(cur.right);
3462
+ }
3463
+ }, "dfs");
3464
+ return dfs(this.root);
3465
+ } else {
3466
+ let current = this.root;
3467
+ let result = void 0;
3468
+ while (this.isRealNode(current)) {
3469
+ const cmp = this.comparator(current.key, key);
3470
+ const condition = isLower ? cmp >= 0 : cmp > 0;
3471
+ if (condition) {
3472
+ result = current;
3473
+ current = current.left ?? void 0;
3474
+ } else {
3475
+ current = current.right ?? void 0;
3476
+ }
3344
3477
  }
3345
- cur = node.right;
3478
+ return result;
3479
+ }
3480
+ }
3481
+ /**
3482
+ * (Protected) In-order traversal search by predicate.
3483
+ * Falls back to linear in-order traversal when predicate-based search is required.
3484
+ * Returns the first node that satisfies the predicate function.
3485
+ * Note: Predicate-based search cannot leverage BST's binary search optimization.
3486
+ * Time Complexity: O(n) since it may visit every node.
3487
+ * @param predicate - The predicate function to test nodes.
3488
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3489
+ * @returns The first node satisfying predicate, or undefined if none found.
3490
+ */
3491
+ _boundByPredicate(predicate, iterationType) {
3492
+ if (iterationType === "RECURSIVE") {
3493
+ let result = void 0;
3494
+ const dfs = /* @__PURE__ */ __name((cur) => {
3495
+ if (result || !this.isRealNode(cur)) return;
3496
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3497
+ if (!result && predicate(cur)) {
3498
+ result = cur;
3499
+ }
3500
+ if (!result && this.isRealNode(cur.right)) dfs(cur.right);
3501
+ }, "dfs");
3502
+ dfs(this.root);
3503
+ return result;
3504
+ } else {
3505
+ const stack = [];
3506
+ let current = this.root;
3507
+ while (stack.length > 0 || this.isRealNode(current)) {
3508
+ if (this.isRealNode(current)) {
3509
+ stack.push(current);
3510
+ current = current.left;
3511
+ } else {
3512
+ const node = stack.pop();
3513
+ if (!this.isRealNode(node)) break;
3514
+ if (predicate(node)) {
3515
+ return node;
3516
+ }
3517
+ current = node.right;
3518
+ }
3519
+ }
3520
+ return void 0;
3346
3521
  }
3347
- return false;
3348
3522
  }
3349
3523
  /**
3350
3524
  * (Protected) Creates a new, empty instance of the same BST constructor.
@@ -3381,8 +3555,7 @@ var BST = class extends BinaryTree {
3381
3555
  _snapshotOptions() {
3382
3556
  return {
3383
3557
  ...super._snapshotOptions(),
3384
- specifyComparable: this.specifyComparable,
3385
- isReverse: this.isReverse
3558
+ comparator: this._comparator
3386
3559
  };
3387
3560
  }
3388
3561
  /**
@@ -3410,14 +3583,14 @@ var BST = class extends BinaryTree {
3410
3583
  }
3411
3584
  /**
3412
3585
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
3413
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
3586
+ * @remarks Time O(1) Space O(1)
3414
3587
  *
3415
3588
  * @param a - The first key.
3416
3589
  * @param b - The second key.
3417
3590
  * @returns A number (1, -1, or 0) representing the comparison.
3418
3591
  */
3419
3592
  _compare(a, b) {
3420
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
3593
+ return this._comparator(a, b);
3421
3594
  }
3422
3595
  /**
3423
3596
  * (Private) Deletes a node by its key.