red-black-tree-typed 2.2.2 → 2.2.3

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 (46) hide show
  1. package/README.md +92 -37
  2. package/dist/cjs/index.cjs +163 -0
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +164 -0
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +163 -0
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +164 -0
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
  11. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  12. package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
  14. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  15. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  16. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  17. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  18. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  19. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  20. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  21. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  22. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  23. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  24. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  25. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  26. package/dist/umd/red-black-tree-typed.js +164 -0
  27. package/dist/umd/red-black-tree-typed.js.map +1 -1
  28. package/dist/umd/red-black-tree-typed.min.js +3 -3
  29. package/dist/umd/red-black-tree-typed.min.js.map +1 -1
  30. package/package.json +2 -2
  31. package/src/data-structures/binary-tree/avl-tree.ts +96 -2
  32. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  33. package/src/data-structures/binary-tree/bst.ts +322 -13
  34. package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
  35. package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
  36. package/src/data-structures/graph/directed-graph.ts +126 -1
  37. package/src/data-structures/graph/undirected-graph.ts +160 -1
  38. package/src/data-structures/hash/hash-map.ts +110 -27
  39. package/src/data-structures/heap/heap.ts +107 -58
  40. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  41. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  42. package/src/data-structures/queue/deque.ts +95 -67
  43. package/src/data-structures/queue/queue.ts +90 -34
  44. package/src/data-structures/stack/stack.ts +58 -40
  45. package/src/data-structures/trie/trie.ts +109 -47
  46. package/src/interfaces/binary-tree.ts +2 -0
@@ -1466,6 +1466,17 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1466
1466
  }
1467
1467
  return false;
1468
1468
  }
1469
+ /**
1470
+ * Adds or updates a new node to the tree.
1471
+ * @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).
1472
+ *
1473
+ * @param keyNodeOrEntry - The key, node, or entry to add or update.
1474
+ * @param [value] - The value, if providing just a key.
1475
+ * @returns True if the addition was successful, false otherwise.
1476
+ */
1477
+ set(keyNodeOrEntry, value) {
1478
+ return this.add(keyNodeOrEntry, value);
1479
+ }
1469
1480
  /**
1470
1481
  * Adds multiple items to the tree.
1471
1482
  * @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).
@@ -1493,6 +1504,17 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
1493
1504
  }
1494
1505
  return inserted;
1495
1506
  }
1507
+ /**
1508
+ * Adds or updates multiple items to the tree.
1509
+ * @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).
1510
+ *
1511
+ * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
1512
+ * @param [values] - An optional parallel iterable of values.
1513
+ * @returns An array of booleans indicating the success of each individual `add` operation.
1514
+ */
1515
+ setMany(keysNodesEntriesOrRaws, values) {
1516
+ return this.addMany(keysNodesEntriesOrRaws, values);
1517
+ }
1496
1518
  /**
1497
1519
  * Merges another tree into this one by adding all its nodes.
1498
1520
  * @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`).
@@ -3186,6 +3208,32 @@ var _BST = class _BST extends BinaryTree {
3186
3208
  else _iterate();
3187
3209
  return inserted;
3188
3210
  }
3211
+ /**
3212
+ * Returns the first node with a key greater than or equal to the given key.
3213
+ * This is equivalent to C++ std::lower_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
+ lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3222
+ return this._bound(keyNodeEntryOrPredicate, true, iterationType);
3223
+ }
3224
+ /**
3225
+ * Returns the first node with a key strictly greater than the given key.
3226
+ * This is equivalent to C++ std::upper_bound on a BST.
3227
+ * Supports RECURSIVE and ITERATIVE implementations.
3228
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
3229
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
3230
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3231
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3232
+ * @returns The first node with key > given key, or undefined if no such node exists.
3233
+ */
3234
+ upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3235
+ return this._bound(keyNodeEntryOrPredicate, false, iterationType);
3236
+ }
3189
3237
  /**
3190
3238
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
3191
3239
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -3346,6 +3394,122 @@ var _BST = class _BST extends BinaryTree {
3346
3394
  }
3347
3395
  return false;
3348
3396
  }
3397
+ /**
3398
+ * (Protected) Core bound search implementation supporting all parameter types.
3399
+ * Unified logic for both lowerBound and upperBound.
3400
+ * Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
3401
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3402
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
3403
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3404
+ * @returns The first matching node, or undefined if no such node exists.
3405
+ */
3406
+ _bound(keyNodeEntryOrPredicate, isLower, iterationType) {
3407
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3408
+ return void 0;
3409
+ }
3410
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3411
+ return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
3412
+ }
3413
+ let targetKey;
3414
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3415
+ targetKey = keyNodeEntryOrPredicate.key;
3416
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3417
+ const key = keyNodeEntryOrPredicate[0];
3418
+ if (key === null || key === void 0) {
3419
+ return void 0;
3420
+ }
3421
+ targetKey = key;
3422
+ } else {
3423
+ targetKey = keyNodeEntryOrPredicate;
3424
+ }
3425
+ if (targetKey !== void 0) {
3426
+ return this._boundByKey(targetKey, isLower, iterationType);
3427
+ }
3428
+ return void 0;
3429
+ }
3430
+ /**
3431
+ * (Protected) Binary search for bound by key with pruning optimization.
3432
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3433
+ * For lowerBound: finds first node where key >= target.
3434
+ * For upperBound: finds first node where key > target.
3435
+ * @param key - The target key to search for.
3436
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
3437
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3438
+ * @returns The first node matching the bound condition, or undefined if none exists.
3439
+ */
3440
+ _boundByKey(key, isLower, iterationType) {
3441
+ var _a, _b;
3442
+ if (iterationType === "RECURSIVE") {
3443
+ const dfs = /* @__PURE__ */ __name((cur) => {
3444
+ if (!this.isRealNode(cur)) return void 0;
3445
+ const cmp = this.comparator(cur.key, key);
3446
+ const condition = isLower ? cmp >= 0 : cmp > 0;
3447
+ if (condition) {
3448
+ const leftResult = dfs(cur.left);
3449
+ return leftResult != null ? leftResult : cur;
3450
+ } else {
3451
+ return dfs(cur.right);
3452
+ }
3453
+ }, "dfs");
3454
+ return dfs(this.root);
3455
+ } else {
3456
+ let current = this.root;
3457
+ let result = void 0;
3458
+ while (this.isRealNode(current)) {
3459
+ const cmp = this.comparator(current.key, key);
3460
+ const condition = isLower ? cmp >= 0 : cmp > 0;
3461
+ if (condition) {
3462
+ result = current;
3463
+ current = (_a = current.left) != null ? _a : void 0;
3464
+ } else {
3465
+ current = (_b = current.right) != null ? _b : void 0;
3466
+ }
3467
+ }
3468
+ return result;
3469
+ }
3470
+ }
3471
+ /**
3472
+ * (Protected) In-order traversal search by predicate.
3473
+ * Falls back to linear in-order traversal when predicate-based search is required.
3474
+ * Returns the first node that satisfies the predicate function.
3475
+ * Note: Predicate-based search cannot leverage BST's binary search optimization.
3476
+ * Time Complexity: O(n) since it may visit every node.
3477
+ * @param predicate - The predicate function to test nodes.
3478
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3479
+ * @returns The first node satisfying predicate, or undefined if none found.
3480
+ */
3481
+ _boundByPredicate(predicate, iterationType) {
3482
+ if (iterationType === "RECURSIVE") {
3483
+ let result = void 0;
3484
+ const dfs = /* @__PURE__ */ __name((cur) => {
3485
+ if (result || !this.isRealNode(cur)) return;
3486
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3487
+ if (!result && predicate(cur)) {
3488
+ result = cur;
3489
+ }
3490
+ if (!result && this.isRealNode(cur.right)) dfs(cur.right);
3491
+ }, "dfs");
3492
+ dfs(this.root);
3493
+ return result;
3494
+ } else {
3495
+ const stack = [];
3496
+ let current = this.root;
3497
+ while (stack.length > 0 || this.isRealNode(current)) {
3498
+ if (this.isRealNode(current)) {
3499
+ stack.push(current);
3500
+ current = current.left;
3501
+ } else {
3502
+ const node = stack.pop();
3503
+ if (!this.isRealNode(node)) break;
3504
+ if (predicate(node)) {
3505
+ return node;
3506
+ }
3507
+ current = node.right;
3508
+ }
3509
+ }
3510
+ return void 0;
3511
+ }
3512
+ }
3349
3513
  /**
3350
3514
  * (Protected) Creates a new, empty instance of the same BST constructor.
3351
3515
  * @remarks Time O(1)