data-structure-typed 2.2.7 → 2.3.0

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 (181) hide show
  1. package/.github/workflows/ci.yml +9 -0
  2. package/CHANGELOG.md +1 -1
  3. package/README.md +14 -3
  4. package/README_CN.md +119 -275
  5. package/benchmark/report.html +1 -1
  6. package/benchmark/report.json +20 -324
  7. package/dist/cjs/index.cjs +689 -182
  8. package/dist/cjs/index.cjs.map +1 -1
  9. package/dist/cjs-legacy/index.cjs +693 -185
  10. package/dist/cjs-legacy/index.cjs.map +1 -1
  11. package/dist/esm/index.mjs +689 -182
  12. package/dist/esm/index.mjs.map +1 -1
  13. package/dist/esm-legacy/index.mjs +693 -185
  14. package/dist/esm-legacy/index.mjs.map +1 -1
  15. package/dist/leetcode/avl-tree-counter.mjs +2957 -0
  16. package/dist/leetcode/avl-tree-multi-map.mjs +2889 -0
  17. package/dist/leetcode/avl-tree.mjs +2720 -0
  18. package/dist/leetcode/binary-tree.mjs +1594 -0
  19. package/dist/leetcode/bst.mjs +2398 -0
  20. package/dist/leetcode/deque.mjs +683 -0
  21. package/dist/leetcode/directed-graph.mjs +1733 -0
  22. package/dist/leetcode/doubly-linked-list.mjs +709 -0
  23. package/dist/leetcode/hash-map.mjs +493 -0
  24. package/dist/leetcode/heap.mjs +542 -0
  25. package/dist/leetcode/max-heap.mjs +375 -0
  26. package/dist/leetcode/max-priority-queue.mjs +383 -0
  27. package/dist/leetcode/min-heap.mjs +363 -0
  28. package/dist/leetcode/min-priority-queue.mjs +371 -0
  29. package/dist/leetcode/priority-queue.mjs +363 -0
  30. package/dist/leetcode/queue.mjs +943 -0
  31. package/dist/leetcode/red-black-tree.mjs +2765 -0
  32. package/dist/leetcode/singly-linked-list.mjs +754 -0
  33. package/dist/leetcode/stack.mjs +217 -0
  34. package/dist/leetcode/tree-counter.mjs +3039 -0
  35. package/dist/leetcode/tree-multi-map.mjs +2913 -0
  36. package/dist/leetcode/trie.mjs +413 -0
  37. package/dist/leetcode/undirected-graph.mjs +1650 -0
  38. package/dist/types/data-structures/base/linear-base.d.ts +6 -6
  39. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
  40. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  41. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
  42. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +25 -27
  43. package/dist/types/data-structures/binary-tree/bst.d.ts +13 -12
  44. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +151 -21
  45. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -4
  46. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  47. package/dist/types/interfaces/binary-tree.d.ts +1 -1
  48. package/dist/umd/data-structure-typed.js +689 -181
  49. package/dist/umd/data-structure-typed.js.map +1 -1
  50. package/dist/umd/data-structure-typed.min.js +3 -3
  51. package/dist/umd/data-structure-typed.min.js.map +1 -1
  52. package/package.json +50 -172
  53. package/src/data-structures/base/linear-base.ts +2 -12
  54. package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
  55. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
  56. package/src/data-structures/binary-tree/avl-tree.ts +15 -15
  57. package/src/data-structures/binary-tree/binary-tree.ts +57 -60
  58. package/src/data-structures/binary-tree/bst.ts +100 -26
  59. package/src/data-structures/binary-tree/red-black-tree.ts +586 -76
  60. package/src/data-structures/binary-tree/tree-counter.ts +25 -13
  61. package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
  62. package/src/data-structures/queue/deque.ts +10 -0
  63. package/src/interfaces/binary-tree.ts +1 -1
  64. package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
  65. package/test/unit/data-structures/base/iterable-element-base.coverage.test.ts +106 -0
  66. package/test/unit/data-structures/base/iterable-element-base.more-branches.coverage.test.ts +61 -0
  67. package/test/unit/data-structures/base/linear-base.array.coverage.test.ts +168 -0
  68. package/test/unit/data-structures/base/linear-base.concat-else.coverage.test.ts +82 -0
  69. package/test/unit/data-structures/base/linear-base.coverage.test.ts +72 -0
  70. package/test/unit/data-structures/base/linear-base.more-branches.coverage.test.ts +417 -0
  71. package/test/unit/data-structures/binary-tree/avl-tree-counter.more-branches-3.coverage.test.ts +146 -0
  72. package/test/unit/data-structures/binary-tree/avl-tree-counter.more-branches.coverage.test.ts +93 -0
  73. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +30 -30
  74. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.coverage.test.ts +108 -0
  75. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.more-branches-2.coverage.test.ts +85 -0
  76. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +46 -46
  77. package/test/unit/data-structures/binary-tree/avl-tree-node.familyPosition-root-left.coverage.test.ts +17 -0
  78. package/test/unit/data-structures/binary-tree/avl-tree.more-branches-2.coverage.test.ts +99 -0
  79. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
  80. package/test/unit/data-structures/binary-tree/binary-indexed-tree.more-branches.coverage.test.ts +18 -0
  81. package/test/unit/data-structures/binary-tree/binary-tree.more-branches.coverage.test.ts +56 -0
  82. package/test/unit/data-structures/binary-tree/binary-tree.remaining-branches.coverage.test.ts +229 -0
  83. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
  84. package/test/unit/data-structures/binary-tree/bst.bound-by-predicate.coverage.test.ts +33 -0
  85. package/test/unit/data-structures/binary-tree/bst.coverage.test.ts +94 -0
  86. package/test/unit/data-structures/binary-tree/bst.deletebykey.coverage.test.ts +70 -0
  87. package/test/unit/data-structures/binary-tree/bst.deletewhere.coverage.test.ts +37 -0
  88. package/test/unit/data-structures/binary-tree/bst.floor-lower-predicate.coverage.test.ts +29 -0
  89. package/test/unit/data-structures/binary-tree/bst.floor-setmany.coverage.test.ts +72 -0
  90. package/test/unit/data-structures/binary-tree/bst.getnode.range-ensure.coverage.test.ts +22 -0
  91. package/test/unit/data-structures/binary-tree/bst.misc-branches.coverage.test.ts +100 -0
  92. package/test/unit/data-structures/binary-tree/bst.more-branches-2.coverage.test.ts +133 -0
  93. package/test/unit/data-structures/binary-tree/bst.more-branches-3.coverage.test.ts +45 -0
  94. package/test/unit/data-structures/binary-tree/bst.more-branches-4.coverage.test.ts +36 -0
  95. package/test/unit/data-structures/binary-tree/bst.more-branches-5.coverage.test.ts +40 -0
  96. package/test/unit/data-structures/binary-tree/bst.more.coverage.test.ts +39 -0
  97. package/test/unit/data-structures/binary-tree/bst.node-family.coverage.test.ts +29 -0
  98. package/test/unit/data-structures/binary-tree/bst.range-pruning.coverage.test.ts +43 -0
  99. package/test/unit/data-structures/binary-tree/bst.search-fastpath.coverage.test.ts +30 -0
  100. package/test/unit/data-structures/binary-tree/bst.test.ts +124 -154
  101. package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
  102. package/test/unit/data-structures/binary-tree/red-black-tree.boundary-corruption-repair.coverage.test.ts +66 -0
  103. package/test/unit/data-structures/binary-tree/red-black-tree.boundary-max-update.coverage.test.ts +18 -0
  104. package/test/unit/data-structures/binary-tree/red-black-tree.boundary-null.coverage.test.ts +53 -0
  105. package/test/unit/data-structures/binary-tree/red-black-tree.boundary-stale-cache.coverage.test.ts +25 -0
  106. package/test/unit/data-structures/binary-tree/red-black-tree.boundary-update.coverage.test.ts +23 -0
  107. package/test/unit/data-structures/binary-tree/red-black-tree.cache-delete.coverage.test.ts +49 -0
  108. package/test/unit/data-structures/binary-tree/red-black-tree.cache-edge.coverage.test.ts +37 -0
  109. package/test/unit/data-structures/binary-tree/red-black-tree.cache-stale-insert.coverage.test.ts +39 -0
  110. package/test/unit/data-structures/binary-tree/red-black-tree.coverage.test.ts +334 -0
  111. package/test/unit/data-structures/binary-tree/red-black-tree.delete-fixup.coverage.test.ts +68 -0
  112. package/test/unit/data-structures/binary-tree/red-black-tree.delete-successor.coverage.test.ts +75 -0
  113. package/test/unit/data-structures/binary-tree/red-black-tree.factories.coverage.test.ts +26 -0
  114. package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-compare-update.coverage.test.ts +74 -0
  115. package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-no-update.coverage.test.ts +44 -0
  116. package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-nullish.coverage.test.ts +61 -0
  117. package/test/unit/data-structures/binary-tree/red-black-tree.hint-mapmode-defined.coverage.test.ts +35 -0
  118. package/test/unit/data-structures/binary-tree/red-black-tree.hint-mapmode-undefined.coverage.test.ts +43 -0
  119. package/test/unit/data-structures/binary-tree/red-black-tree.hint-more.coverage.test.ts +99 -0
  120. package/test/unit/data-structures/binary-tree/red-black-tree.hint.coverage.test.ts +60 -0
  121. package/test/unit/data-structures/binary-tree/red-black-tree.insert-cache-nullish.coverage.test.ts +29 -0
  122. package/test/unit/data-structures/binary-tree/red-black-tree.insert-header-parent-nullish.coverage.test.ts +17 -0
  123. package/test/unit/data-structures/binary-tree/red-black-tree.internal-walk.coverage.test.ts +57 -0
  124. package/test/unit/data-structures/binary-tree/red-black-tree.minmax-cache.test.ts +65 -0
  125. package/test/unit/data-structures/binary-tree/red-black-tree.misc-inputs.coverage.test.ts +17 -0
  126. package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-2.coverage.test.ts +121 -0
  127. package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-3.coverage.test.ts +55 -0
  128. package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-4.coverage.test.ts +44 -0
  129. package/test/unit/data-structures/binary-tree/red-black-tree.predsucc.coverage.test.ts +40 -0
  130. package/test/unit/data-structures/binary-tree/red-black-tree.remaining-branches.coverage.test.ts +123 -0
  131. package/test/unit/data-structures/binary-tree/red-black-tree.set-inputs.coverage.test.ts +64 -0
  132. package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-parent-cache.coverage.test.ts +79 -0
  133. package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-remaining.coverage.test.ts +44 -0
  134. package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-uncovered.coverage.test.ts +74 -0
  135. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
  136. package/test/unit/data-structures/binary-tree/red-black-tree.update-branches.coverage.test.ts +30 -0
  137. package/test/unit/data-structures/binary-tree/segment-tree.more-branches.coverage.test.ts +31 -0
  138. package/test/unit/data-structures/binary-tree/tree-counter.coverage.test.ts +115 -0
  139. package/test/unit/data-structures/binary-tree/tree-counter.more-branches.coverage.test.ts +244 -0
  140. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +41 -39
  141. package/test/unit/data-structures/binary-tree/tree-multi-map.coverage.test.ts +104 -0
  142. package/test/unit/data-structures/binary-tree/tree-multi-map.more-branches-2.coverage.test.ts +59 -0
  143. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
  144. package/test/unit/data-structures/graph/abstract-graph.more-branches-2.coverage.test.ts +40 -0
  145. package/test/unit/data-structures/graph/abstract-graph.more-branches-3.coverage.test.ts +65 -0
  146. package/test/unit/data-structures/graph/abstract-graph.more-branches-4.coverage.test.ts +98 -0
  147. package/test/unit/data-structures/graph/abstract-graph.more-branches-5.coverage.test.ts +51 -0
  148. package/test/unit/data-structures/graph/abstract-graph.more-branches.coverage.test.ts +62 -0
  149. package/test/unit/data-structures/graph/directed-graph.more-branches-2.coverage.test.ts +38 -0
  150. package/test/unit/data-structures/graph/directed-graph.more-branches-3.coverage.test.ts +25 -0
  151. package/test/unit/data-structures/graph/directed-graph.more-branches.coverage.test.ts +82 -0
  152. package/test/unit/data-structures/graph/map-graph.more-branches.coverage.test.ts +22 -0
  153. package/test/unit/data-structures/graph/undirected-graph.more-branches-2.coverage.test.ts +35 -0
  154. package/test/unit/data-structures/graph/undirected-graph.more-branches.coverage.test.ts +87 -0
  155. package/test/unit/data-structures/hash/hash-map.more-branches.coverage.test.ts +64 -0
  156. package/test/unit/data-structures/hash/hash-map.toEntryFn-branch.coverage.test.ts +9 -0
  157. package/test/unit/data-structures/heap/heap.misc-branches.coverage.test.ts +110 -0
  158. package/test/unit/data-structures/heap/heap.remaining-branches.coverage.test.ts +22 -0
  159. package/test/unit/data-structures/heap/max-heap.coverage.test.ts +29 -0
  160. package/test/unit/data-structures/linked-list/doubly-linked-list.more-branches.coverage.test.ts +72 -0
  161. package/test/unit/data-structures/linked-list/linked-list.unshiftMany-else.coverage.test.ts +15 -0
  162. package/test/unit/data-structures/linked-list/singly-linked-list.coverage.test.ts +221 -0
  163. package/test/unit/data-structures/linked-list/singly-linked-list.more-branches.coverage.test.ts +86 -0
  164. package/test/unit/data-structures/linked-list/skip-linked-list.more-branches.coverage.test.ts +31 -0
  165. package/test/unit/data-structures/matrix/matrix.more-branches.coverage.test.ts +81 -0
  166. package/test/unit/data-structures/matrix/matrix.pivotElement-nullish.coverage.test.ts +28 -0
  167. package/test/unit/data-structures/priority-queue/max-priority-queue.more-branches.coverage.test.ts +10 -0
  168. package/test/unit/data-structures/priority-queue/priority-queue.coverage.test.ts +21 -0
  169. package/test/unit/data-structures/queue/deque.coverage.test.ts +173 -0
  170. package/test/unit/data-structures/queue/deque.more-branches-2.coverage.test.ts +39 -0
  171. package/test/unit/data-structures/queue/deque.more-branches-3.coverage.test.ts +9 -0
  172. package/test/unit/data-structures/queue/deque.more-branches.coverage.test.ts +95 -0
  173. package/test/unit/data-structures/queue/queue.coverage.test.ts +138 -0
  174. package/test/unit/data-structures/queue/queue.more-branches-2.coverage.test.ts +27 -0
  175. package/test/unit/data-structures/stack/stack.coverage.test.ts +112 -0
  176. package/test/unit/data-structures/tree/tree.more-branches.coverage.test.ts +9 -0
  177. package/test/unit/data-structures/trie/trie.more-branches-2.coverage.test.ts +51 -0
  178. package/test/utils/patch.ts +33 -0
  179. package/tsup.config.js +50 -21
  180. package/tsup.umd.config.js +29 -0
  181. package/tsup.node.config.js +0 -83
@@ -10,7 +10,7 @@ import type {
10
10
  BinaryTreeDeleteResult,
11
11
  BinaryTreeOptions,
12
12
  BinaryTreePrintOptions,
13
- BTNEntry,
13
+ BTNEntry, BTNRep,
14
14
  DFSOrderPattern,
15
15
  DFSStackItem,
16
16
  EntryCallback,
@@ -71,7 +71,7 @@ export class BinaryTreeNode<K = any, V = any> {
71
71
  */
72
72
  set left(v: BinaryTreeNode<K, V> | null | undefined) {
73
73
  if (v) {
74
- v.parent = this as unknown as BinaryTreeNode<K, V>;
74
+ v.parent = this;
75
75
  }
76
76
  this._left = v;
77
77
  }
@@ -193,7 +193,7 @@ export class BinaryTreeNode<K = any, V = any> {
193
193
  *
194
194
  * @remarks
195
195
  * This class implements a basic Binary Tree, not a Binary Search Tree.
196
- * The `add` operation inserts nodes level-by-level (BFS) into the first available slot.
196
+ * The `set` operation inserts nodes level-by-level (BFS) into the first available slot.
197
197
  *
198
198
  * @template K - The type of the key.
199
199
  * @template V - The type of the value.
@@ -225,7 +225,7 @@ export class BinaryTreeNode<K = any, V = any> {
225
225
  * console.log(tree.size); // 9;
226
226
  *
227
227
  * // Add new element
228
- * tree.add(10, 'ten');
228
+ * tree.set(10, 'ten');
229
229
  * console.log(tree.size); // 10;
230
230
  * @example
231
231
  * // BinaryTree get and has operations
@@ -353,9 +353,9 @@ export class BinaryTree<K = any, V = any, R = any>
353
353
 
354
354
  /**
355
355
  * Creates an instance of BinaryTree.
356
- * @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `add` operation). Space O(N) for storing the nodes.
356
+ * @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `set` operation). Space O(N) for storing the nodes.
357
357
  *
358
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
358
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
359
359
  * @param [options] - Configuration options for the tree.
360
360
  */
361
361
  constructor(
@@ -374,7 +374,7 @@ export class BinaryTree<K = any, V = any, R = any>
374
374
  else if (toEntryFn) throw TypeError('toEntryFn must be a function type');
375
375
  }
376
376
 
377
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
377
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
378
378
  }
379
379
 
380
380
  protected _isMapMode = true;
@@ -640,13 +640,27 @@ export class BinaryTree<K = any, V = any, R = any>
640
640
  * @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).
641
641
  *
642
642
  * @param keyNodeOrEntry - The key, node, or entry to add.
643
- * @param [value] - The value, if providing just a key.
644
643
  * @returns True if the addition was successful, false otherwise.
645
644
  */
646
645
  add(
646
+ keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
647
+ ): boolean {
648
+ return this.set(keyNodeOrEntry);
649
+ }
650
+
651
+ /**
652
+ * Adds or updates a new node to the tree.
653
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation sets 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).
654
+ *
655
+ * @param keyNodeOrEntry - The key, node, or entry to set or update.
656
+ * @param [value] - The value, if providing just a key.
657
+ * @returns True if the addition was successful, false otherwise.
658
+ */
659
+ set(
647
660
  keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
648
661
  value?: V
649
662
  ): boolean {
663
+
650
664
  const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
651
665
  if (newNode === undefined) return false;
652
666
 
@@ -699,29 +713,29 @@ export class BinaryTree<K = any, V = any, R = any>
699
713
  }
700
714
 
701
715
  /**
702
- * Adds or updates a new node to the tree.
703
- * @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).
716
+ * Adds multiple items to the tree.
717
+ * @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
704
718
  *
705
- * @param keyNodeOrEntry - The key, node, or entry to add or update.
706
- * @param [value] - The value, if providing just a key.
707
- * @returns True if the addition was successful, false otherwise.
719
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
720
+ * @returns An array of booleans indicating the success of each individual `set` operation.
708
721
  */
709
- set(
710
- keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
711
- value?: V
712
- ): boolean {
713
- return this.add(keyNodeOrEntry, value);
722
+ addMany(
723
+ keysNodesEntriesOrRaws: Iterable<
724
+ K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
725
+ >
726
+ ): boolean[] {
727
+ return this.setMany(keysNodesEntriesOrRaws);
714
728
  }
715
729
 
716
730
  /**
717
- * Adds multiple items to the tree.
718
- * @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).
731
+ * Adds or updates multiple items to the tree.
732
+ * @remarks Time O(N * M), where N is the number of items to set and M is the size of the tree at insertion (due to O(M) `set` operation). Space O(M) (from `set`) + O(N) (for the `inserted` array).
719
733
  *
720
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
734
+ * @param keysNodesEntriesOrRaws - An iterable of items to set or update.
721
735
  * @param [values] - An optional parallel iterable of values.
722
- * @returns An array of booleans indicating the success of each individual `add` operation.
736
+ * @returns An array of booleans indicating the success of each individual `set` operation.
723
737
  */
724
- addMany(
738
+ setMany(
725
739
  keysNodesEntriesOrRaws: Iterable<
726
740
  K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
727
741
  >,
@@ -744,44 +758,27 @@ export class BinaryTree<K = any, V = any, R = any>
744
758
  }
745
759
  }
746
760
  if (this.isRaw(keyNodeEntryOrRaw)) keyNodeEntryOrRaw = this._toEntryFn!(keyNodeEntryOrRaw);
747
- inserted.push(this.add(keyNodeEntryOrRaw, value));
761
+ inserted.push(this.set(keyNodeEntryOrRaw, value));
748
762
  }
749
763
 
750
764
  return inserted;
751
765
  }
752
766
 
753
767
  /**
754
- * Adds or updates multiple items to the tree.
755
- * @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).
756
- *
757
- * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
758
- * @param [values] - An optional parallel iterable of values.
759
- * @returns An array of booleans indicating the success of each individual `add` operation.
760
- */
761
- setMany(
762
- keysNodesEntriesOrRaws: Iterable<
763
- K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
764
- >,
765
- values?: Iterable<V | undefined>
766
- ): boolean[] {
767
- return this.addMany(keysNodesEntriesOrRaws, values);
768
- }
769
-
770
- /**
771
- * Merges another tree into this one by adding all its nodes.
772
- * @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`).
768
+ * Merges another tree into this one by seting all its nodes.
769
+ * @remarks Time O(N * M), same as `setMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `set`).
773
770
  *
774
771
  * @param anotherTree - The tree to merge.
775
772
  */
776
773
  merge(anotherTree: BinaryTree<K, V, R>) {
777
- this.addMany(anotherTree, []);
774
+ this.setMany(anotherTree, []);
778
775
  }
779
776
 
780
777
  /**
781
778
  * Clears the tree and refills it with new items.
782
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `addMany`) = O(N * M). Space O(M) (from `addMany`).
779
+ * @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
783
780
  *
784
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
781
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
785
782
  * @param [values] - An optional parallel iterable of values.
786
783
  */
787
784
  refill(
@@ -791,23 +788,23 @@ export class BinaryTree<K = any, V = any, R = any>
791
788
  values?: Iterable<V | undefined>
792
789
  ): void {
793
790
  this.clear();
794
- this.addMany(keysNodesEntriesOrRaws, values);
791
+ this.setMany(keysNodesEntriesOrRaws, values);
795
792
  }
796
793
 
797
794
  /**
798
795
  * Deletes a node from the tree.
799
796
  * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
800
797
  *
801
- * @param keyNodeOrEntry - The node to delete.
798
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
802
799
  * @returns An array containing deletion results (for compatibility with self-balancing trees).
803
800
  */
804
801
  delete(
805
- keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
802
+ keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>
806
803
  ): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[] {
807
804
  const deletedResult: BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[] = [];
808
805
  if (!this._root) return deletedResult;
809
806
 
810
- const curr = this.getNode(keyNodeOrEntry);
807
+ const curr = this.getNode(keyNodeEntryRawOrPredicate);
811
808
  if (!curr) return deletedResult;
812
809
 
813
810
  const parent: BinaryTreeNode<K, V> | undefined = curr?.parent;
@@ -1142,7 +1139,7 @@ export class BinaryTree<K = any, V = any, R = any>
1142
1139
  }
1143
1140
  return true;
1144
1141
  };
1145
- const isStandardBST = checkBST(false);
1142
+ const isStandardBST = checkBST();
1146
1143
  const isInverseBST = checkBST(true);
1147
1144
  return isStandardBST || isInverseBST;
1148
1145
  }
@@ -1830,7 +1827,7 @@ export class BinaryTree<K = any, V = any, R = any>
1830
1827
 
1831
1828
  /**
1832
1829
  * Clones the tree.
1833
- * @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `add`, and `add` is O(M)). Space O(N) for the new tree and the BFS queue.
1830
+ * @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `set`, and `set` is O(M)). Space O(N) for the new tree and the BFS queue.
1834
1831
  *
1835
1832
  * @returns A new, cloned instance of the tree.
1836
1833
  */
@@ -1842,7 +1839,7 @@ export class BinaryTree<K = any, V = any, R = any>
1842
1839
 
1843
1840
  /**
1844
1841
  * Creates a new tree containing only the entries that satisfy the predicate.
1845
- * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `add` for each item). Space O(N) for the new tree.
1842
+ * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `set` for each item). Space O(N) for the new tree.
1846
1843
  *
1847
1844
  * @param predicate - A function to test each [key, value] pair.
1848
1845
  * @param [thisArg] - `this` context for the predicate.
@@ -1851,7 +1848,7 @@ export class BinaryTree<K = any, V = any, R = any>
1851
1848
  filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this {
1852
1849
  const out = this._createInstance<K, V, R>();
1853
1850
  let i = 0;
1854
- for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.add([k, v]);
1851
+ for (const [k, v] of this) if (predicate.call(thisArg, v, k, i++, this)) out.set([k, v]);
1855
1852
  return out;
1856
1853
  }
1857
1854
 
@@ -1874,7 +1871,7 @@ export class BinaryTree<K = any, V = any, R = any>
1874
1871
  ): BinaryTree<MK, MV, MR> {
1875
1872
  const out = this._createLike<MK, MV, MR>([], options);
1876
1873
  let i = 0;
1877
- for (const [k, v] of this) out.add(cb.call(thisArg, v, k, i++, this));
1874
+ for (const [k, v] of this) out.set(cb.call(thisArg, v, k, i++, this));
1878
1875
  return out;
1879
1876
  }
1880
1877
 
@@ -2204,8 +2201,8 @@ export class BinaryTree<K = any, V = any, R = any>
2204
2201
  }
2205
2202
 
2206
2203
  /**
2207
- * (Protected) Helper for cloning. Performs a BFS and adds all nodes to the new tree.
2208
- * @remarks Time O(N * M) (O(N) BFS + O(M) `add` for each node).
2204
+ * (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
2205
+ * @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
2209
2206
  *
2210
2207
  * @param cloned - The new, empty tree instance to populate.
2211
2208
  */
@@ -2213,10 +2210,10 @@ export class BinaryTree<K = any, V = any, R = any>
2213
2210
  // Use BFS with nulls to preserve the tree structure
2214
2211
  this.bfs(
2215
2212
  node => {
2216
- if (node === null) cloned.add(null);
2213
+ if (node === null) cloned.set(null);
2217
2214
  else {
2218
- if (this._isMapMode) cloned.add([node.key, this._store.get(node.key)]);
2219
- else cloned.add([node.key, node.value]);
2215
+ if (this._isMapMode) cloned.set([node.key, this._store.get(node.key)]);
2216
+ else cloned.set([node.key, node.value]);
2220
2217
  }
2221
2218
  },
2222
2219
  this._root,
@@ -204,7 +204,8 @@ export class BSTNode<K = any, V = any> {
204
204
  * // Create a simple BST with numeric keys
205
205
  * const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
206
206
  *
207
- * bst.print();
207
+ * // Keep the example output in source comments but avoid noisy test logs.
208
+ * await withMutedConsole(() => bst.print());
208
209
  * // _______8__________
209
210
  * // / \
210
211
  * // ___4___ ____12_____
@@ -219,8 +220,8 @@ export class BSTNode<K = any, V = any> {
219
220
  * console.log(bst.size); // 16;
220
221
  *
221
222
  * // Add new elements
222
- * bst.add(17);
223
- * bst.add(0);
223
+ * bst.set(17);
224
+ * bst.set(0);
224
225
  * console.log(bst.size); // 18;
225
226
  *
226
227
  * // Verify keys are searchable
@@ -266,7 +267,7 @@ export class BSTNode<K = any, V = any> {
266
267
  *
267
268
  * // Merge datasets into a single BinarySearchTree
268
269
  * const merged = new BST<number, string>(dataset1);
269
- * merged.addMany(dataset2);
270
+ * merged.setMany(dataset2);
270
271
  * merged.merge(dataset3);
271
272
  *
272
273
  * // Verify merged dataset is in sorted order
@@ -350,7 +351,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
350
351
  * Creates an instance of BST.
351
352
  * @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
352
353
  *
353
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
354
+ * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
354
355
  * @param [options] - Configuration options for the BST, including comparator.
355
356
  */
356
357
  constructor(
@@ -370,7 +371,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
370
371
  this._comparator = this._createDefaultComparator();
371
372
  }
372
373
 
373
- if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
374
+ if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
374
375
  }
375
376
 
376
377
  protected override _root?: BSTNode<K, V> = undefined;
@@ -556,9 +557,55 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
556
557
  startNode: BSTNOptKeyOrNode<K, BSTNode<K, V>> = this._root,
557
558
  iterationType: IterationType = this.iterationType
558
559
  ): OptNode<BSTNode<K, V>> {
559
- return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? undefined;
560
+ // Fast-path: key lookup should not allocate arrays or build predicate closures.
561
+ // (This is a hot path for get/has in Node Mode.)
562
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined) return undefined;
563
+
564
+ // If a predicate is provided, defer to the full search logic.
565
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
566
+ return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? undefined;
567
+ }
568
+
569
+ // NOTE: Range<K> is not part of this overload, but callers may still pass it at runtime.
570
+ // Let search handle it.
571
+ if (keyNodeEntryOrPredicate instanceof Range) {
572
+ return (
573
+ this.getNodes(
574
+ keyNodeEntryOrPredicate,
575
+ true,
576
+ startNode as K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
577
+ iterationType
578
+ )[0] ?? undefined
579
+ );
580
+ }
581
+
582
+ let targetKey: K | undefined;
583
+ if (this.isNode(keyNodeEntryOrPredicate)) {
584
+ targetKey = keyNodeEntryOrPredicate.key;
585
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
586
+ const k = keyNodeEntryOrPredicate[0];
587
+ if (k === null || k === undefined) return undefined;
588
+ targetKey = k;
589
+ } else {
590
+ targetKey = keyNodeEntryOrPredicate;
591
+ }
592
+
593
+ const start = this.ensureNode(startNode);
594
+ if (!start) return undefined;
595
+
596
+ const NIL = this._NIL as unknown as BSTNode<K, V> | null | undefined;
597
+ let cur: BSTNode<K, V> | null | undefined = start;
598
+ const cmpFn = this._comparator;
599
+ while (cur && cur !== NIL) {
600
+ const c = cmpFn(targetKey, cur.key);
601
+ if (c === 0) return cur;
602
+ cur = c < 0 ? cur._left : cur._right;
603
+ }
604
+
605
+ return undefined;
560
606
  }
561
607
 
608
+
562
609
  override search(
563
610
  keyNodeEntryOrPredicate:
564
611
  | K
@@ -619,13 +666,41 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
619
666
  startNode = this.ensureNode(startNode);
620
667
  if (!startNode) return [];
621
668
 
622
- let predicate: NodePredicate<BSTNode<K, V>>;
669
+ // Fast-path: key lookup (unique keys) using a tight BST walk (no allocations).
670
+ // This is the hot path for get/has/search by key.
623
671
  const isRange = this.isRange(keyNodeEntryOrPredicate);
672
+ const isPred = !isRange && this._isPredicate(keyNodeEntryOrPredicate);
673
+
674
+ if (!isRange && !isPred) {
675
+ let targetKey: K | undefined;
676
+ if (this.isNode(keyNodeEntryOrPredicate)) {
677
+ targetKey = keyNodeEntryOrPredicate.key;
678
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
679
+ const k = keyNodeEntryOrPredicate[0];
680
+ if (k !== null && k !== undefined) targetKey = k;
681
+ } else {
682
+ targetKey = keyNodeEntryOrPredicate;
683
+ }
684
+ if (targetKey === undefined) return [];
685
+
686
+ const NIL = this._NIL as unknown as BSTNode<K, V> | null | undefined;
687
+ const cmpFn = this._comparator;
688
+ let cur: BSTNode<K, V> | null | undefined = startNode;
689
+
690
+ // Loop intentionally avoids getters and extra type checks.
691
+ while (cur && cur !== NIL) {
692
+ const c = cmpFn(targetKey, cur.key);
693
+ if (c === 0) return [callback(cur)];
694
+ cur = c < 0 ? cur._left : cur._right;
695
+ }
696
+ return [];
697
+ }
624
698
 
699
+ let predicate: NodePredicate<BSTNode<K, V>>;
625
700
  if (isRange) {
626
701
  predicate = node => {
627
702
  if (!node) return false;
628
- return (keyNodeEntryOrPredicate as Range<K>).isInRange(node.key, this._comparator);
703
+ return (keyNodeEntryOrPredicate).isInRange(node.key, this._comparator);
629
704
  };
630
705
  } else {
631
706
  predicate = this._ensurePredicate(keyNodeEntryOrPredicate);
@@ -637,7 +712,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
637
712
  if (!this.isRealNode(cur.left)) return false;
638
713
  if (isRange) {
639
714
  // Range search: Only go left if the current key is >= the lower bound
640
- const range = keyNodeEntryOrPredicate as Range<K>;
715
+ const range = keyNodeEntryOrPredicate;
641
716
  const leftS = range.low;
642
717
  const leftI = range.includeLow;
643
718
  return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
@@ -655,7 +730,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
655
730
  if (!this.isRealNode(cur.right)) return false;
656
731
  if (isRange) {
657
732
  // Range search: Only go right if current key <= upper bound
658
- const range = keyNodeEntryOrPredicate as Range<K>;
733
+ const range = keyNodeEntryOrPredicate;
659
734
  const rightS = range.high;
660
735
  const rightI = range.includeHigh;
661
736
  return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
@@ -716,11 +791,11 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
716
791
  * Adds a new node to the BST based on key comparison.
717
792
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
718
793
  *
719
- * @param keyNodeOrEntry - The key, node, or entry to add.
794
+ * @param keyNodeOrEntry - The key, node, or entry to set.
720
795
  * @param [value] - The value, if providing just a key.
721
796
  * @returns True if the addition was successful, false otherwise.
722
797
  */
723
- override add(
798
+ override set(
724
799
  keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
725
800
  value?: V
726
801
  ): boolean {
@@ -766,17 +841,17 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
766
841
 
767
842
  /**
768
843
  * Adds multiple items to the tree.
769
- * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced add).
844
+ * @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
770
845
  * If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
771
846
  * Space O(N) for sorting and recursion/iteration stack.
772
847
  *
773
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
848
+ * @param keysNodesEntriesOrRaws - An iterable of items to set.
774
849
  * @param [values] - An optional parallel iterable of values.
775
850
  * @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
776
- * @param [iterationType=this.iterationType] - The traversal method for balanced add (recursive or iterative).
777
- * @returns An array of booleans indicating the success of each individual `add` operation.
851
+ * @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
852
+ * @returns An array of booleans indicating the success of each individual `set` operation.
778
853
  */
779
- override addMany(
854
+ override setMany(
780
855
  keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>,
781
856
  values?: Iterable<V | undefined>,
782
857
  isBalanceAdd = true,
@@ -790,7 +865,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
790
865
  for (let kve of keysNodesEntriesOrRaws) {
791
866
  const val = valuesIterator?.next().value;
792
867
  if (this.isRaw(kve)) kve = this._toEntryFn!(kve);
793
- inserted.push(this.add(kve, val));
868
+ inserted.push(this.set(kve, val));
794
869
  }
795
870
  return inserted;
796
871
  }
@@ -831,9 +906,9 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
831
906
  const { key, value, orgIndex } = arr[mid];
832
907
  if (this.isRaw(key)) {
833
908
  const entry = this._toEntryFn!(key);
834
- inserted[orgIndex] = this.add(entry);
909
+ inserted[orgIndex] = this.set(entry);
835
910
  } else {
836
- inserted[orgIndex] = this.add(key, value);
911
+ inserted[orgIndex] = this.set(key, value);
837
912
  }
838
913
  _dfs(arr.slice(0, mid));
839
914
  _dfs(arr.slice(mid + 1));
@@ -852,9 +927,9 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
852
927
  const { key, value, orgIndex } = sorted[m];
853
928
  if (this.isRaw(key)) {
854
929
  const entry = this._toEntryFn!(key);
855
- inserted[orgIndex] = this.add(entry);
930
+ inserted[orgIndex] = this.set(entry);
856
931
  } else {
857
- inserted[orgIndex] = this.add(key, value);
932
+ inserted[orgIndex] = this.set(key, value);
858
933
  }
859
934
  stack.push([m + 1, r]);
860
935
  stack.push([l, m - 1]);
@@ -1369,7 +1444,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
1369
1444
  let index = 0;
1370
1445
  // Iterates in-order
1371
1446
  for (const [key, value] of this) {
1372
- out.add(callback.call(thisArg, value, key, index++, this));
1447
+ out.set(callback.call(thisArg, value, key, index++, this));
1373
1448
  }
1374
1449
  return out;
1375
1450
  }
@@ -1441,7 +1516,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
1441
1516
  */
1442
1517
  protected _createDefaultComparator(): Comparator<K> {
1443
1518
  return (a: K, b: K): number => {
1444
- debugger;
1445
1519
  // If both keys are comparable (primitive types), use direct comparison
1446
1520
  if (isComparable(a) && isComparable(b)) {
1447
1521
  if (a > b) return 1;
@@ -2019,7 +2093,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
2019
2093
  if (succ.left) (succ.left as BSTNode<K, V>).parent = succ;
2020
2094
  }
2021
2095
 
2022
- this._size = Math.max(0, ((this as any)._size ?? 0) - 1);
2096
+ this._size = Math.max(0, this._size - 1);
2023
2097
  return true;
2024
2098
  }
2025
2099
  }