avl-tree-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/avl-tree-typed.js +246 -72
  30. package/dist/umd/avl-tree-typed.js.map +1 -1
  31. package/dist/umd/avl-tree-typed.min.js +3 -3
  32. package/dist/umd/avl-tree-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
@@ -1458,6 +1458,17 @@ var BinaryTree = class extends IterableEntryBase {
1458
1458
  }
1459
1459
  return false;
1460
1460
  }
1461
+ /**
1462
+ * Adds or updates a new node to the tree.
1463
+ * @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).
1464
+ *
1465
+ * @param keyNodeOrEntry - The key, node, or entry to add or update.
1466
+ * @param [value] - The value, if providing just a key.
1467
+ * @returns True if the addition was successful, false otherwise.
1468
+ */
1469
+ set(keyNodeOrEntry, value) {
1470
+ return this.add(keyNodeOrEntry, value);
1471
+ }
1461
1472
  /**
1462
1473
  * Adds multiple items to the tree.
1463
1474
  * @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).
@@ -1485,6 +1496,17 @@ var BinaryTree = class extends IterableEntryBase {
1485
1496
  }
1486
1497
  return inserted;
1487
1498
  }
1499
+ /**
1500
+ * Adds or updates multiple items to the tree.
1501
+ * @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).
1502
+ *
1503
+ * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
1504
+ * @param [values] - An optional parallel iterable of values.
1505
+ * @returns An array of booleans indicating the success of each individual `add` operation.
1506
+ */
1507
+ setMany(keysNodesEntriesOrRaws, values) {
1508
+ return this.addMany(keysNodesEntriesOrRaws, values);
1509
+ }
1488
1510
  /**
1489
1511
  * Merges another tree into this one by adding all its nodes.
1490
1512
  * @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`).
@@ -2808,9 +2830,13 @@ var BST = class extends BinaryTree {
2808
2830
  constructor(keysNodesEntriesOrRaws = [], options) {
2809
2831
  super([], options);
2810
2832
  if (options) {
2811
- const { specifyComparable, isReverse } = options;
2812
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
2813
- if (isReverse !== void 0) this._isReverse = isReverse;
2833
+ if ("comparator" in options && options.comparator !== void 0) {
2834
+ this._comparator = options.comparator;
2835
+ } else {
2836
+ this._comparator = this._createDefaultComparator();
2837
+ }
2838
+ } else {
2839
+ this._comparator = this._createDefaultComparator();
2814
2840
  }
2815
2841
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
2816
2842
  }
@@ -2824,40 +2850,33 @@ var BST = class extends BinaryTree {
2824
2850
  get root() {
2825
2851
  return this._root;
2826
2852
  }
2827
- _isReverse = false;
2828
2853
  /**
2829
- * Gets whether the tree's comparison logic is reversed.
2830
- * @remarks Time O(1)
2831
- *
2832
- * @returns True if the tree is reversed (e.g., a max-heap logic).
2854
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
2855
+ * @remarks Time O(1) Space O(1)
2856
+ * @returns The default comparator function.
2833
2857
  */
2834
- get isReverse() {
2835
- return this._isReverse;
2858
+ _createDefaultComparator() {
2859
+ return (a, b) => {
2860
+ debugger;
2861
+ if (isComparable(a) && isComparable(b)) {
2862
+ if (a > b) return 1;
2863
+ if (a < b) return -1;
2864
+ return 0;
2865
+ }
2866
+ if (typeof a === "object" || typeof b === "object") {
2867
+ throw TypeError(
2868
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
2869
+ );
2870
+ }
2871
+ return 0;
2872
+ };
2836
2873
  }
2837
2874
  /**
2838
- * The default comparator function.
2839
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
2840
- */
2841
- _comparator = /* @__PURE__ */ __name((a, b) => {
2842
- if (isComparable(a) && isComparable(b)) {
2843
- if (a > b) return 1;
2844
- if (a < b) return -1;
2845
- return 0;
2846
- }
2847
- if (this._specifyComparable) {
2848
- const va = this._specifyComparable(a);
2849
- const vb = this._specifyComparable(b);
2850
- if (va > vb) return 1;
2851
- if (va < vb) return -1;
2852
- return 0;
2853
- }
2854
- if (typeof a === "object" || typeof b === "object") {
2855
- throw TypeError(
2856
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
2857
- );
2858
- }
2859
- return 0;
2860
- }, "_comparator");
2875
+ * The comparator function used to determine the order of keys in the tree.
2876
+
2877
+ * @remarks Time O(1) Space O(1)
2878
+ */
2879
+ _comparator;
2861
2880
  /**
2862
2881
  * Gets the comparator function used by the tree.
2863
2882
  * @remarks Time O(1)
@@ -2867,16 +2886,6 @@ var BST = class extends BinaryTree {
2867
2886
  get comparator() {
2868
2887
  return this._comparator;
2869
2888
  }
2870
- _specifyComparable;
2871
- /**
2872
- * Gets the function used to extract a comparable value from a complex key.
2873
- * @remarks Time O(1)
2874
- *
2875
- * @returns The key-to-comparable conversion function.
2876
- */
2877
- get specifyComparable() {
2878
- return this._specifyComparable;
2879
- }
2880
2889
  /**
2881
2890
  * (Protected) Creates a new BST node.
2882
2891
  * @remarks Time O(1), Space O(1)
@@ -2917,7 +2926,7 @@ var BST = class extends BinaryTree {
2917
2926
  * @returns True if the key is valid, false otherwise.
2918
2927
  */
2919
2928
  isValidKey(key) {
2920
- return isComparable(key, this._specifyComparable !== void 0);
2929
+ return isComparable(key);
2921
2930
  }
2922
2931
  /**
2923
2932
  * Performs a Depth-First Search (DFS) traversal.
@@ -3006,8 +3015,8 @@ var BST = class extends BinaryTree {
3006
3015
  if (!this.isRealNode(cur.left)) return false;
3007
3016
  if (isRange) {
3008
3017
  const range = keyNodeEntryOrPredicate;
3009
- const leftS = this.isReverse ? range.high : range.low;
3010
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
3018
+ const leftS = range.low;
3019
+ const leftI = range.includeLow;
3011
3020
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
3012
3021
  }
3013
3022
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3021,8 +3030,8 @@ var BST = class extends BinaryTree {
3021
3030
  if (!this.isRealNode(cur.right)) return false;
3022
3031
  if (isRange) {
3023
3032
  const range = keyNodeEntryOrPredicate;
3024
- const rightS = this.isReverse ? range.low : range.high;
3025
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
3033
+ const rightS = range.high;
3034
+ const rightI = range.includeHigh;
3026
3035
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
3027
3036
  }
3028
3037
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3183,6 +3192,32 @@ var BST = class extends BinaryTree {
3183
3192
  else _iterate();
3184
3193
  return inserted;
3185
3194
  }
3195
+ /**
3196
+ * Returns the first node with a key greater than or equal to the given key.
3197
+ * This is equivalent to C++ std::lower_bound on a BST.
3198
+ * Supports RECURSIVE and ITERATIVE implementations.
3199
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
3200
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
3201
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3202
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3203
+ * @returns The first node with key >= given key, or undefined if no such node exists.
3204
+ */
3205
+ lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3206
+ return this._bound(keyNodeEntryOrPredicate, true, iterationType);
3207
+ }
3208
+ /**
3209
+ * Returns the first node with a key strictly greater than the given key.
3210
+ * This is equivalent to C++ std::upper_bound on a BST.
3211
+ * Supports RECURSIVE and ITERATIVE implementations.
3212
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
3213
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
3214
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3215
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3216
+ * @returns The first node with key > given key, or undefined if no such node exists.
3217
+ */
3218
+ upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3219
+ return this._bound(keyNodeEntryOrPredicate, false, iterationType);
3220
+ }
3186
3221
  /**
3187
3222
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
3188
3223
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -3317,31 +3352,170 @@ var BST = class extends BinaryTree {
3317
3352
  return out;
3318
3353
  }
3319
3354
  /**
3320
- * Deletes the first node found that satisfies the predicate.
3321
- * @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.
3355
+ * Deletes nodes that match a key, node, entry, predicate, or range.
3322
3356
  *
3323
- * @param predicate - A function to test each [key, value] pair.
3324
- * @returns True if a node was deleted, false otherwise.
3357
+ * @remarks
3358
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
3359
+ * Space Complexity: O(M) for storing matched nodes and result map.
3360
+ *
3361
+ * @template K - The key type.
3362
+ * @template V - The value type.
3363
+ *
3364
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
3365
+ * - A key (type K): searches for exact key match using the comparator.
3366
+ * - A BSTNode: searches for the matching node in the tree.
3367
+ * - An entry tuple: searches for the key-value pair.
3368
+ * - A NodePredicate function: tests each node and returns true for matches.
3369
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
3370
+ * - null or undefined: treated as no match, returns empty results.
3371
+ *
3372
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
3373
+ * If false (default), searches for and deletes all matching nodes.
3374
+ *
3375
+ * @param startNode - The node to start the search from. Can be:
3376
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
3377
+ * - null or undefined: defaults to the root, searching the entire tree.
3378
+ * - Default value: this._root (the tree's root).
3379
+ *
3380
+ * @param iterationType - Controls the internal traversal implementation:
3381
+ * - 'RECURSIVE': uses recursive function calls for traversal.
3382
+ * - 'ITERATIVE': uses explicit stack-based iteration.
3383
+ * - Default: this.iterationType (the tree's default iteration mode).
3384
+ *
3385
+ * @returns A Map<K, boolean> containing the deletion results:
3386
+ * - Key: the matched node's key.
3387
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
3388
+ * - If no nodes match the search criteria, the returned map is empty.
3389
+ */
3390
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
3391
+ const toDelete = this.search(
3392
+ keyNodeEntryOrPredicate,
3393
+ onlyOne,
3394
+ (node) => node,
3395
+ startNode,
3396
+ iterationType
3397
+ );
3398
+ let results = [];
3399
+ for (const node of toDelete) {
3400
+ const deleteInfo = this.delete(node);
3401
+ results = results.concat(deleteInfo);
3402
+ }
3403
+ return results;
3404
+ }
3405
+ /**
3406
+ * (Protected) Core bound search implementation supporting all parameter types.
3407
+ * Unified logic for both lowerBound and upperBound.
3408
+ * Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
3409
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3410
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
3411
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3412
+ * @returns The first matching node, or undefined if no such node exists.
3325
3413
  */
3326
- deleteWhere(predicate) {
3327
- const stack = [];
3328
- let cur = this._root;
3329
- let index = 0;
3330
- while (stack.length > 0 || cur !== void 0) {
3331
- while (cur !== void 0 && cur !== null) {
3332
- stack.push(cur);
3333
- cur = cur.left;
3414
+ _bound(keyNodeEntryOrPredicate, isLower, iterationType) {
3415
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3416
+ return void 0;
3417
+ }
3418
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3419
+ return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
3420
+ }
3421
+ let targetKey;
3422
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3423
+ targetKey = keyNodeEntryOrPredicate.key;
3424
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3425
+ const key = keyNodeEntryOrPredicate[0];
3426
+ if (key === null || key === void 0) {
3427
+ return void 0;
3334
3428
  }
3335
- const node = stack.pop();
3336
- if (!node) break;
3337
- const key = node.key;
3338
- const val = node.value;
3339
- if (predicate(key, val, index++, this)) {
3340
- return this._deleteByKey(key);
3429
+ targetKey = key;
3430
+ } else {
3431
+ targetKey = keyNodeEntryOrPredicate;
3432
+ }
3433
+ if (targetKey !== void 0) {
3434
+ return this._boundByKey(targetKey, isLower, iterationType);
3435
+ }
3436
+ return void 0;
3437
+ }
3438
+ /**
3439
+ * (Protected) Binary search for bound by key with pruning optimization.
3440
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3441
+ * For lowerBound: finds first node where key >= target.
3442
+ * For upperBound: finds first node where key > target.
3443
+ * @param key - The target key to search for.
3444
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
3445
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3446
+ * @returns The first node matching the bound condition, or undefined if none exists.
3447
+ */
3448
+ _boundByKey(key, isLower, iterationType) {
3449
+ if (iterationType === "RECURSIVE") {
3450
+ const dfs = /* @__PURE__ */ __name((cur) => {
3451
+ if (!this.isRealNode(cur)) return void 0;
3452
+ const cmp = this.comparator(cur.key, key);
3453
+ const condition = isLower ? cmp >= 0 : cmp > 0;
3454
+ if (condition) {
3455
+ const leftResult = dfs(cur.left);
3456
+ return leftResult ?? cur;
3457
+ } else {
3458
+ return dfs(cur.right);
3459
+ }
3460
+ }, "dfs");
3461
+ return dfs(this.root);
3462
+ } else {
3463
+ let current = this.root;
3464
+ let result = void 0;
3465
+ while (this.isRealNode(current)) {
3466
+ const cmp = this.comparator(current.key, key);
3467
+ const condition = isLower ? cmp >= 0 : cmp > 0;
3468
+ if (condition) {
3469
+ result = current;
3470
+ current = current.left ?? void 0;
3471
+ } else {
3472
+ current = current.right ?? void 0;
3473
+ }
3341
3474
  }
3342
- cur = node.right;
3475
+ return result;
3476
+ }
3477
+ }
3478
+ /**
3479
+ * (Protected) In-order traversal search by predicate.
3480
+ * Falls back to linear in-order traversal when predicate-based search is required.
3481
+ * Returns the first node that satisfies the predicate function.
3482
+ * Note: Predicate-based search cannot leverage BST's binary search optimization.
3483
+ * Time Complexity: O(n) since it may visit every node.
3484
+ * @param predicate - The predicate function to test nodes.
3485
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3486
+ * @returns The first node satisfying predicate, or undefined if none found.
3487
+ */
3488
+ _boundByPredicate(predicate, iterationType) {
3489
+ if (iterationType === "RECURSIVE") {
3490
+ let result = void 0;
3491
+ const dfs = /* @__PURE__ */ __name((cur) => {
3492
+ if (result || !this.isRealNode(cur)) return;
3493
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3494
+ if (!result && predicate(cur)) {
3495
+ result = cur;
3496
+ }
3497
+ if (!result && this.isRealNode(cur.right)) dfs(cur.right);
3498
+ }, "dfs");
3499
+ dfs(this.root);
3500
+ return result;
3501
+ } else {
3502
+ const stack = [];
3503
+ let current = this.root;
3504
+ while (stack.length > 0 || this.isRealNode(current)) {
3505
+ if (this.isRealNode(current)) {
3506
+ stack.push(current);
3507
+ current = current.left;
3508
+ } else {
3509
+ const node = stack.pop();
3510
+ if (!this.isRealNode(node)) break;
3511
+ if (predicate(node)) {
3512
+ return node;
3513
+ }
3514
+ current = node.right;
3515
+ }
3516
+ }
3517
+ return void 0;
3343
3518
  }
3344
- return false;
3345
3519
  }
3346
3520
  /**
3347
3521
  * (Protected) Creates a new, empty instance of the same BST constructor.
@@ -3378,8 +3552,7 @@ var BST = class extends BinaryTree {
3378
3552
  _snapshotOptions() {
3379
3553
  return {
3380
3554
  ...super._snapshotOptions(),
3381
- specifyComparable: this.specifyComparable,
3382
- isReverse: this.isReverse
3555
+ comparator: this._comparator
3383
3556
  };
3384
3557
  }
3385
3558
  /**
@@ -3407,14 +3580,14 @@ var BST = class extends BinaryTree {
3407
3580
  }
3408
3581
  /**
3409
3582
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
3410
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
3583
+ * @remarks Time O(1) Space O(1)
3411
3584
  *
3412
3585
  * @param a - The first key.
3413
3586
  * @param b - The second key.
3414
3587
  * @returns A number (1, -1, or 0) representing the comparison.
3415
3588
  */
3416
3589
  _compare(a, b) {
3417
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
3590
+ return this._comparator(a, b);
3418
3591
  }
3419
3592
  /**
3420
3593
  * (Private) Deletes a node by its key.