data-structure-typed 2.2.8 → 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 (137) hide show
  1. package/.github/workflows/ci.yml +9 -0
  2. package/CHANGELOG.md +1 -1
  3. package/README.md +1 -1
  4. package/README_CN.md +1 -1
  5. package/dist/cjs/index.cjs +584 -76
  6. package/dist/cjs/index.cjs.map +1 -1
  7. package/dist/cjs-legacy/index.cjs +588 -79
  8. package/dist/cjs-legacy/index.cjs.map +1 -1
  9. package/dist/esm/index.mjs +584 -76
  10. package/dist/esm/index.mjs.map +1 -1
  11. package/dist/esm-legacy/index.mjs +588 -79
  12. package/dist/esm-legacy/index.mjs.map +1 -1
  13. package/dist/types/data-structures/base/linear-base.d.ts +6 -6
  14. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +3 -4
  15. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -1
  16. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +150 -20
  17. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +3 -3
  18. package/dist/types/interfaces/binary-tree.d.ts +1 -1
  19. package/dist/umd/data-structure-typed.js +588 -79
  20. package/dist/umd/data-structure-typed.js.map +1 -1
  21. package/dist/umd/data-structure-typed.min.js +3 -3
  22. package/dist/umd/data-structure-typed.min.js.map +1 -1
  23. package/package.json +4 -3
  24. package/src/data-structures/base/linear-base.ts +2 -12
  25. package/src/data-structures/binary-tree/binary-tree.ts +5 -6
  26. package/src/data-structures/binary-tree/bst.ts +79 -4
  27. package/src/data-structures/binary-tree/red-black-tree.ts +583 -73
  28. package/src/data-structures/binary-tree/tree-counter.ts +21 -9
  29. package/src/data-structures/queue/deque.ts +10 -0
  30. package/src/interfaces/binary-tree.ts +1 -1
  31. package/test/unit/data-structures/base/iterable-element-base.coverage.test.ts +106 -0
  32. package/test/unit/data-structures/base/iterable-element-base.more-branches.coverage.test.ts +61 -0
  33. package/test/unit/data-structures/base/linear-base.array.coverage.test.ts +168 -0
  34. package/test/unit/data-structures/base/linear-base.concat-else.coverage.test.ts +82 -0
  35. package/test/unit/data-structures/base/linear-base.coverage.test.ts +72 -0
  36. package/test/unit/data-structures/base/linear-base.more-branches.coverage.test.ts +417 -0
  37. package/test/unit/data-structures/binary-tree/avl-tree-counter.more-branches-3.coverage.test.ts +146 -0
  38. package/test/unit/data-structures/binary-tree/avl-tree-counter.more-branches.coverage.test.ts +93 -0
  39. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.coverage.test.ts +108 -0
  40. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.more-branches-2.coverage.test.ts +85 -0
  41. package/test/unit/data-structures/binary-tree/avl-tree-node.familyPosition-root-left.coverage.test.ts +17 -0
  42. package/test/unit/data-structures/binary-tree/avl-tree.more-branches-2.coverage.test.ts +99 -0
  43. package/test/unit/data-structures/binary-tree/binary-indexed-tree.more-branches.coverage.test.ts +18 -0
  44. package/test/unit/data-structures/binary-tree/binary-tree.more-branches.coverage.test.ts +56 -0
  45. package/test/unit/data-structures/binary-tree/binary-tree.remaining-branches.coverage.test.ts +229 -0
  46. package/test/unit/data-structures/binary-tree/bst.bound-by-predicate.coverage.test.ts +33 -0
  47. package/test/unit/data-structures/binary-tree/bst.coverage.test.ts +94 -0
  48. package/test/unit/data-structures/binary-tree/bst.deletebykey.coverage.test.ts +70 -0
  49. package/test/unit/data-structures/binary-tree/bst.deletewhere.coverage.test.ts +37 -0
  50. package/test/unit/data-structures/binary-tree/bst.floor-lower-predicate.coverage.test.ts +29 -0
  51. package/test/unit/data-structures/binary-tree/bst.floor-setmany.coverage.test.ts +72 -0
  52. package/test/unit/data-structures/binary-tree/bst.getnode.range-ensure.coverage.test.ts +22 -0
  53. package/test/unit/data-structures/binary-tree/bst.misc-branches.coverage.test.ts +100 -0
  54. package/test/unit/data-structures/binary-tree/bst.more-branches-2.coverage.test.ts +133 -0
  55. package/test/unit/data-structures/binary-tree/bst.more-branches-3.coverage.test.ts +45 -0
  56. package/test/unit/data-structures/binary-tree/bst.more-branches-4.coverage.test.ts +36 -0
  57. package/test/unit/data-structures/binary-tree/bst.more-branches-5.coverage.test.ts +40 -0
  58. package/test/unit/data-structures/binary-tree/bst.more.coverage.test.ts +39 -0
  59. package/test/unit/data-structures/binary-tree/bst.node-family.coverage.test.ts +29 -0
  60. package/test/unit/data-structures/binary-tree/bst.range-pruning.coverage.test.ts +43 -0
  61. package/test/unit/data-structures/binary-tree/bst.search-fastpath.coverage.test.ts +30 -0
  62. package/test/unit/data-structures/binary-tree/bst.test.ts +25 -55
  63. package/test/unit/data-structures/binary-tree/red-black-tree.boundary-corruption-repair.coverage.test.ts +66 -0
  64. package/test/unit/data-structures/binary-tree/red-black-tree.boundary-max-update.coverage.test.ts +18 -0
  65. package/test/unit/data-structures/binary-tree/red-black-tree.boundary-null.coverage.test.ts +53 -0
  66. package/test/unit/data-structures/binary-tree/red-black-tree.boundary-stale-cache.coverage.test.ts +25 -0
  67. package/test/unit/data-structures/binary-tree/red-black-tree.boundary-update.coverage.test.ts +23 -0
  68. package/test/unit/data-structures/binary-tree/red-black-tree.cache-delete.coverage.test.ts +49 -0
  69. package/test/unit/data-structures/binary-tree/red-black-tree.cache-edge.coverage.test.ts +37 -0
  70. package/test/unit/data-structures/binary-tree/red-black-tree.cache-stale-insert.coverage.test.ts +39 -0
  71. package/test/unit/data-structures/binary-tree/red-black-tree.coverage.test.ts +334 -0
  72. package/test/unit/data-structures/binary-tree/red-black-tree.delete-fixup.coverage.test.ts +68 -0
  73. package/test/unit/data-structures/binary-tree/red-black-tree.delete-successor.coverage.test.ts +75 -0
  74. package/test/unit/data-structures/binary-tree/red-black-tree.factories.coverage.test.ts +26 -0
  75. package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-compare-update.coverage.test.ts +74 -0
  76. package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-no-update.coverage.test.ts +44 -0
  77. package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-nullish.coverage.test.ts +61 -0
  78. package/test/unit/data-structures/binary-tree/red-black-tree.hint-mapmode-defined.coverage.test.ts +35 -0
  79. package/test/unit/data-structures/binary-tree/red-black-tree.hint-mapmode-undefined.coverage.test.ts +43 -0
  80. package/test/unit/data-structures/binary-tree/red-black-tree.hint-more.coverage.test.ts +99 -0
  81. package/test/unit/data-structures/binary-tree/red-black-tree.hint.coverage.test.ts +60 -0
  82. package/test/unit/data-structures/binary-tree/red-black-tree.insert-cache-nullish.coverage.test.ts +29 -0
  83. package/test/unit/data-structures/binary-tree/red-black-tree.insert-header-parent-nullish.coverage.test.ts +17 -0
  84. package/test/unit/data-structures/binary-tree/red-black-tree.internal-walk.coverage.test.ts +57 -0
  85. package/test/unit/data-structures/binary-tree/red-black-tree.minmax-cache.test.ts +65 -0
  86. package/test/unit/data-structures/binary-tree/red-black-tree.misc-inputs.coverage.test.ts +17 -0
  87. package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-2.coverage.test.ts +121 -0
  88. package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-3.coverage.test.ts +55 -0
  89. package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-4.coverage.test.ts +44 -0
  90. package/test/unit/data-structures/binary-tree/red-black-tree.predsucc.coverage.test.ts +40 -0
  91. package/test/unit/data-structures/binary-tree/red-black-tree.remaining-branches.coverage.test.ts +123 -0
  92. package/test/unit/data-structures/binary-tree/red-black-tree.set-inputs.coverage.test.ts +64 -0
  93. package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-parent-cache.coverage.test.ts +79 -0
  94. package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-remaining.coverage.test.ts +44 -0
  95. package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-uncovered.coverage.test.ts +74 -0
  96. package/test/unit/data-structures/binary-tree/red-black-tree.update-branches.coverage.test.ts +30 -0
  97. package/test/unit/data-structures/binary-tree/segment-tree.more-branches.coverage.test.ts +31 -0
  98. package/test/unit/data-structures/binary-tree/tree-counter.coverage.test.ts +115 -0
  99. package/test/unit/data-structures/binary-tree/tree-counter.more-branches.coverage.test.ts +244 -0
  100. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +4 -2
  101. package/test/unit/data-structures/binary-tree/tree-multi-map.coverage.test.ts +104 -0
  102. package/test/unit/data-structures/binary-tree/tree-multi-map.more-branches-2.coverage.test.ts +59 -0
  103. package/test/unit/data-structures/graph/abstract-graph.more-branches-2.coverage.test.ts +40 -0
  104. package/test/unit/data-structures/graph/abstract-graph.more-branches-3.coverage.test.ts +65 -0
  105. package/test/unit/data-structures/graph/abstract-graph.more-branches-4.coverage.test.ts +98 -0
  106. package/test/unit/data-structures/graph/abstract-graph.more-branches-5.coverage.test.ts +51 -0
  107. package/test/unit/data-structures/graph/abstract-graph.more-branches.coverage.test.ts +62 -0
  108. package/test/unit/data-structures/graph/directed-graph.more-branches-2.coverage.test.ts +38 -0
  109. package/test/unit/data-structures/graph/directed-graph.more-branches-3.coverage.test.ts +25 -0
  110. package/test/unit/data-structures/graph/directed-graph.more-branches.coverage.test.ts +82 -0
  111. package/test/unit/data-structures/graph/map-graph.more-branches.coverage.test.ts +22 -0
  112. package/test/unit/data-structures/graph/undirected-graph.more-branches-2.coverage.test.ts +35 -0
  113. package/test/unit/data-structures/graph/undirected-graph.more-branches.coverage.test.ts +87 -0
  114. package/test/unit/data-structures/hash/hash-map.more-branches.coverage.test.ts +64 -0
  115. package/test/unit/data-structures/hash/hash-map.toEntryFn-branch.coverage.test.ts +9 -0
  116. package/test/unit/data-structures/heap/heap.misc-branches.coverage.test.ts +110 -0
  117. package/test/unit/data-structures/heap/heap.remaining-branches.coverage.test.ts +22 -0
  118. package/test/unit/data-structures/heap/max-heap.coverage.test.ts +29 -0
  119. package/test/unit/data-structures/linked-list/doubly-linked-list.more-branches.coverage.test.ts +72 -0
  120. package/test/unit/data-structures/linked-list/linked-list.unshiftMany-else.coverage.test.ts +15 -0
  121. package/test/unit/data-structures/linked-list/singly-linked-list.coverage.test.ts +221 -0
  122. package/test/unit/data-structures/linked-list/singly-linked-list.more-branches.coverage.test.ts +86 -0
  123. package/test/unit/data-structures/linked-list/skip-linked-list.more-branches.coverage.test.ts +31 -0
  124. package/test/unit/data-structures/matrix/matrix.more-branches.coverage.test.ts +81 -0
  125. package/test/unit/data-structures/matrix/matrix.pivotElement-nullish.coverage.test.ts +28 -0
  126. package/test/unit/data-structures/priority-queue/max-priority-queue.more-branches.coverage.test.ts +10 -0
  127. package/test/unit/data-structures/priority-queue/priority-queue.coverage.test.ts +21 -0
  128. package/test/unit/data-structures/queue/deque.coverage.test.ts +173 -0
  129. package/test/unit/data-structures/queue/deque.more-branches-2.coverage.test.ts +39 -0
  130. package/test/unit/data-structures/queue/deque.more-branches-3.coverage.test.ts +9 -0
  131. package/test/unit/data-structures/queue/deque.more-branches.coverage.test.ts +95 -0
  132. package/test/unit/data-structures/queue/queue.coverage.test.ts +138 -0
  133. package/test/unit/data-structures/queue/queue.more-branches-2.coverage.test.ts +27 -0
  134. package/test/unit/data-structures/stack/stack.coverage.test.ts +112 -0
  135. package/test/unit/data-structures/tree/tree.more-branches.coverage.test.ts +9 -0
  136. package/test/unit/data-structures/trie/trie.more-branches-2.coverage.test.ts +51 -0
  137. package/test/utils/patch.ts +33 -0
@@ -52,7 +52,7 @@ export declare abstract class LinearBase<E, R = any, NODE extends LinkedListNode
52
52
  * @param options - `{ maxLen?, ... }` bounds/behavior options.
53
53
  * @remarks Time O(1), Space O(1)
54
54
  */
55
- protected constructor(options?: LinearBaseOptions<E, R>);
55
+ constructor(options?: LinearBaseOptions<E, R>);
56
56
  /**
57
57
  * Element count.
58
58
  * @returns Number of elements.
@@ -91,12 +91,12 @@ export declare abstract class LinearBase<E, R = any, NODE extends LinkedListNode
91
91
  */
92
92
  findIndex(predicate: ElementCallback<E, R, boolean>, thisArg?: any): number;
93
93
  /**
94
- * Concatenate multiple containers of the same species.
95
- * @param items - Other lists to append.
94
+ * Concatenate elements and/or containers.
95
+ * @param items - Elements or other containers.
96
96
  * @returns New container with combined elements (`this` type).
97
97
  * @remarks Time O(sum(length)), Space O(sum(length))
98
98
  */
99
- concat(...items: this[]): this;
99
+ concat(...items: (E | this)[]): this;
100
100
  /**
101
101
  * In-place stable order via array sort semantics.
102
102
  * @param compareFn - Comparator `(a, b) => number`.
@@ -238,7 +238,7 @@ export declare abstract class LinearBase<E, R = any, NODE extends LinkedListNode
238
238
  * @remarks Time O(1), Space O(1)
239
239
  */
240
240
  export declare abstract class LinearLinkedBase<E, R = any, NODE extends LinkedListNode<E> = LinkedListNode<E>> extends LinearBase<E, R, NODE> {
241
- protected constructor(options?: LinearBaseOptions<E, R>);
241
+ constructor(options?: LinearBaseOptions<E, R>);
242
242
  /**
243
243
  * Linked-list optimized `indexOf` (forwards scan).
244
244
  * @param searchElement - Value to match.
@@ -261,7 +261,7 @@ export declare abstract class LinearLinkedBase<E, R = any, NODE extends LinkedLi
261
261
  * @returns New list with combined elements (`this` type).
262
262
  * @remarks Time O(sum(length)), Space O(sum(length))
263
263
  */
264
- concat(...items: LinearBase<E, R>[]): this;
264
+ concat(...items: (E | LinearBase<E, R>)[]): this;
265
265
  /**
266
266
  * Slice via forward iteration (no random access required).
267
267
  * @param start - Inclusive start (supports negative index).
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BinaryTreeDeleteResult, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, RBTNColor, ToEntryFn } from '../../types';
8
+ import type { BinaryTreeDeleteResult, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, BTNRep, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, RBTNColor, ToEntryFn } from '../../types';
9
9
  import { IBinaryTree } from '../../interfaces';
10
10
  import { IterableEntryBase } from '../base';
11
11
  import { Range } from '../../common';
@@ -451,7 +451,6 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
451
451
  * @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).
452
452
  *
453
453
  * @param keysNodesEntriesOrRaws - An iterable of items to set.
454
- * @param [values] - An optional parallel iterable of values.
455
454
  * @returns An array of booleans indicating the success of each individual `set` operation.
456
455
  */
457
456
  addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>): boolean[];
@@ -483,10 +482,10 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
483
482
  * Deletes a node from the tree.
484
483
  * @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).
485
484
  *
486
- * @param keyNodeOrEntry - The node to delete.
485
+ * @param keyNodeEntryRawOrPredicate - The node to delete.
487
486
  * @returns An array containing deletion results (for compatibility with self-balancing trees).
488
487
  */
489
- delete(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
488
+ delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
490
489
  search(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne?: boolean): (K | undefined)[];
491
490
  search<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne: boolean, callback: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
492
491
  /**
@@ -130,7 +130,8 @@ export declare class BSTNode<K = any, V = any> {
130
130
  * // Create a simple BST with numeric keys
131
131
  * const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
132
132
  *
133
- * bst.print();
133
+ * // Keep the example output in source comments but avoid noisy test logs.
134
+ * await withMutedConsole(() => bst.print());
134
135
  * // _______8__________
135
136
  * // / \
136
137
  * // ___4___ ____12_____
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BinaryTreeDeleteResult, CRUD, EntryCallback, FamilyPosition, RBTNColor, RedBlackTreeOptions } from '../../types';
8
+ import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, FamilyPosition, NodePredicate, RBTNColor, RedBlackTreeOptions } from '../../types';
9
9
  import { BST } from './bst';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  export declare class RedBlackTreeNode<K = any, V = any> {
@@ -13,12 +13,11 @@ export declare class RedBlackTreeNode<K = any, V = any> {
13
13
  value?: V;
14
14
  parent?: RedBlackTreeNode<K, V>;
15
15
  /**
16
- * Create a Red-Black Tree and optionally bulk-insert items.
17
- * @remarks Time O(n log n), Space O(n)
18
- * @param key - See parameter type for details.
19
- * @param [value]- See parameter type for details.
20
- * @param color - See parameter type for details.
21
- * @returns New RedBlackTree instance.
16
+ * Create a Red-Black Tree node.
17
+ * @remarks Time O(1), Space O(1)
18
+ * @param key - Node key.
19
+ * @param [value] - Node value (unused in map mode trees).
20
+ * @param color - Node color.
22
21
  */
23
22
  constructor(key: K, value?: V, color?: RBTNColor);
24
23
  _left?: RedBlackTreeNode<K, V> | null | undefined;
@@ -104,7 +103,7 @@ export declare class RedBlackTreeNode<K = any, V = any> {
104
103
  }
105
104
  /**
106
105
  * Represents a Red-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
107
- * @remarks Time O(1), Space O(1)
106
+ * @remarks Operation complexity depends on the method; see each method's docs.
108
107
  * @template K
109
108
  * @template V
110
109
  * @template R
@@ -207,6 +206,24 @@ export declare class RedBlackTreeNode<K = any, V = any> {
207
206
  export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
208
207
  constructor(keysNodesEntriesOrRaws?: Iterable<K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: RedBlackTreeOptions<K, V, R>);
209
208
  protected _root: RedBlackTreeNode<K, V> | undefined;
209
+ /**
210
+ * (Internal) Header sentinel:
211
+ * - header.parent -> root
212
+ * - header._left -> min (or NIL)
213
+ * - header._right -> max (or NIL)
214
+ *
215
+ * IMPORTANT:
216
+ * - This header is NOT part of the actual tree.
217
+ * - Do NOT use `header.left` / `header.right` accessors for wiring: those setters update `NIL.parent`
218
+ * and can corrupt sentinel invariants / cause hangs. Only touch `header._left/_right`.
219
+ */
220
+ protected _header: RedBlackTreeNode<K, V>;
221
+ /**
222
+ * (Internal) Cache of the current minimum and maximum nodes.
223
+ * Used for fast-path insert/update when keys are monotonic or near-boundary.
224
+ */
225
+ protected _minNode: RedBlackTreeNode<K, V> | undefined;
226
+ protected _maxNode: RedBlackTreeNode<K, V> | undefined;
210
227
  /**
211
228
  * Get the current root node.
212
229
  * @remarks Time O(1), Space O(1)
@@ -234,25 +251,122 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
234
251
  * @remarks Time O(n), Space O(1)
235
252
  * @returns void
236
253
  */
254
+ /**
255
+ * Remove all nodes and clear internal caches.
256
+ * @remarks Time O(n) average, Space O(1)
257
+ */
237
258
  clear(): void;
238
259
  /**
239
- * Insert or replace an entry using BST order and red-black fix-up.
240
- * @remarks Time O(log n), Space O(1)
241
- * @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
242
- * @param [value]- See parameter type for details.
243
- * @returns True if inserted or updated; false if ignored.
260
+ * (Internal) Find a node by key using a tight BST walk (no allocations).
261
+ *
262
+ * NOTE: This uses `header.parent` as the canonical root pointer.
263
+ * @remarks Time O(log n) average, Space O(1)
264
+ */
265
+ protected _findNodeByKey(key: K): RedBlackTreeNode<K, V> | undefined;
266
+ /**
267
+ * (Internal) In-order predecessor of a node in a BST.
268
+ * @remarks Time O(log n) average, Space O(1)
269
+ */
270
+ protected _predecessorOf(node: RedBlackTreeNode<K, V>): RedBlackTreeNode<K, V> | undefined;
271
+ /**
272
+ * (Internal) In-order successor of a node in a BST.
273
+ * @remarks Time O(log n) average, Space O(1)
274
+ */
275
+ protected _successorOf(node: RedBlackTreeNode<K, V>): RedBlackTreeNode<K, V> | undefined;
276
+ /**
277
+ * (Internal) Attach a new node directly under a known parent/side (no search).
278
+ *
279
+ * This is a performance-oriented helper used by boundary fast paths and hinted insertion.
280
+ * It will:
281
+ * - wire parent/child pointers (using accessors, so parent pointers are updated)
282
+ * - initialize children to NIL
283
+ * - mark the new node RED, then run insert fix-up
284
+ *
285
+ * Precondition: the chosen slot (parent.left/parent.right) is empty (NIL/null/undefined).
286
+ * @remarks Time O(log n) average, Space O(1)
287
+ */
288
+ protected _attachNewNode(parent: RedBlackTreeNode<K, V>, side: 'left' | 'right', node: RedBlackTreeNode<K, V>): void;
289
+ /**
290
+ * (Internal) a single source of truth for min/max is header._left/_right.
291
+ * Keep legacy _minNode/_maxNode mirrored for compatibility.
292
+ * @remarks Time O(1), Space O(1)
293
+ */
294
+ /**
295
+ * (Internal) Update min cache pointers (header._left is the canonical min pointer).
296
+ * @remarks Time O(1), Space O(1)
297
+ */
298
+ protected _setMinCache(node: RedBlackTreeNode<K, V> | undefined): void;
299
+ /**
300
+ * (Internal) Update max cache pointers (header._right is the canonical max pointer).
301
+ * @remarks Time O(1), Space O(1)
302
+ */
303
+ protected _setMaxCache(node: RedBlackTreeNode<K, V> | undefined): void;
304
+ /**
305
+ * (Internal) Core set implementation returning the affected node.
306
+ *
307
+ * Hot path goals:
308
+ * - Avoid double walks (search+insert): do a single traversal that either updates or inserts.
309
+ * - Use header min/max caches to fast-path boundary inserts.
310
+ * - Keep header._left/_right as canonical min/max pointers.
311
+ *
312
+ * Return value:
313
+ * - `{ node, created:false }` when an existing key is updated
314
+ * - `{ node, created:true }` when a new node is inserted
315
+ * - `undefined` only on unexpected internal failure.
316
+ * @remarks Time O(log n) average, Space O(1)
317
+ */
318
+ protected _setKVNode(key: K, nextValue?: V): {
319
+ node: RedBlackTreeNode<K, V>;
320
+ created: boolean;
321
+ } | undefined;
322
+ /**
323
+ * (Internal) Boolean wrapper around `_setKVNode`.
324
+ *
325
+ * Includes a map-mode update fast-path:
326
+ * - If `isMapMode=true` and the key already exists in `_store`, then updating the value does not
327
+ * require any tree search/rotation (tree shape depends only on key).
328
+ * - This path is intentionally limited to `nextValue !== undefined` to preserve existing
329
+ * semantics for `undefined` values.
330
+ * @remarks Time O(log n) average, Space O(1)
331
+ */
332
+ protected _setKV(key: K, nextValue?: V): boolean;
333
+ /**
334
+ * Insert/update using a hint node to speed up nearby insertions.
335
+ *
336
+ * close to the expected insertion position (often the previously returned node in a loop).
337
+ *
338
+ * When the hint is a good fit (sorted / nearly-sorted insertion), this can avoid most of the
339
+ * normal root-to-leaf search and reduce constant factors.
340
+ *
341
+ * When the hint does not match (random workloads), this will fall back to the normal set path.
342
+ * @remarks Time O(log n) average, Space O(1)
343
+ */
344
+ setWithHintNode(key: K, value: V, hint?: RedBlackTreeNode<K, V>): RedBlackTreeNode<K, V> | undefined;
345
+ /**
346
+ * Boolean wrapper for setWithHintNode.
347
+ * @remarks Time O(log n) average, Space O(1)
348
+ */
349
+ setWithHint(key: K, value: V, hint?: RedBlackTreeNode<K, V>): boolean;
350
+ /**
351
+ * Insert or update a key/value (map mode) or key-only (set mode).
352
+ *
353
+ * This method is optimized for:
354
+ * - monotonic inserts via min/max boundary fast paths
355
+ * - updates via a single-pass search (no double walk)
356
+ *
357
+ * @remarks Time O(log n) average, Space O(1)
244
358
  */
245
359
  set(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
246
360
  /**
247
361
  * Delete a node by key/node/entry and rebalance as needed.
248
- * @remarks Time O(log n), Space O(1)
249
- * @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node to delete.
362
+ * @remarks Time O(log n) average, Space O(1)
363
+ * @param keyNodeEntryRawOrPredicate - Key, node, or [key, value] entry identifying the node to delete.
250
364
  * @returns Array with deletion metadata (removed node, rebalancing hint if any).
251
365
  */
252
- delete(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[];
366
+ delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, RedBlackTreeNode<K, V>> | NodePredicate<RedBlackTreeNode<K, V> | null>): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[];
253
367
  /**
254
368
  * Transform entries into a like-kind red-black tree with possibly different key/value types.
255
- * @remarks Time O(n), Space O(n)
369
+ * @remarks Time O(n) average, Space O(n)
256
370
  * @template MK
257
371
  * @template MV
258
372
  * @template MR
@@ -262,13 +376,29 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
262
376
  * @returns A new RedBlackTree with mapped entries.
263
377
  */
264
378
  map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<RedBlackTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
379
+ /**
380
+ * (Internal) Create an empty instance of the same concrete tree type.
381
+ * @remarks Time O(1) average, Space O(1)
382
+ */
265
383
  protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this;
384
+ /**
385
+ * (Internal) Create a like-kind tree (same concrete class) populated from an iterable.
386
+ * @remarks Time O(m log m) average (m = iterable length), Space O(m)
387
+ */
266
388
  protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
389
+ /**
390
+ * (Internal) Set the root pointer and keep header.parent in sync.
391
+ * @remarks Time O(1), Space O(1)
392
+ */
267
393
  protected _setRoot(v: RedBlackTreeNode<K, V> | undefined): void;
394
+ /**
395
+ * (Internal) Replace a node in place while preserving its color.
396
+ * @remarks Time O(1) average, Space O(1)
397
+ */
268
398
  protected _replaceNode(oldNode: RedBlackTreeNode<K, V>, newNode: RedBlackTreeNode<K, V>): RedBlackTreeNode<K, V>;
269
399
  /**
270
400
  * (Protected) Standard BST insert followed by red-black fix-up.
271
- * @remarks Time O(log n), Space O(1)
401
+ * @remarks Time O(log n) average, Space O(1)
272
402
  * @param node - Node to insert.
273
403
  * @returns Status string: 'CREATED' or 'UPDATED'.
274
404
  */
@@ -283,14 +413,14 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
283
413
  protected _transplant(u: RedBlackTreeNode<K, V>, v: RedBlackTreeNode<K, V> | undefined): void;
284
414
  /**
285
415
  * (Protected) Restore red-black properties after insertion (recolor/rotate).
286
- * @remarks Time O(log n), Space O(1)
416
+ * @remarks Time O(log n) average, Space O(1)
287
417
  * @param z - Recently inserted node.
288
418
  * @returns void
289
419
  */
290
420
  protected _insertFixup(z: RedBlackTreeNode<K, V> | undefined): void;
291
421
  /**
292
422
  * (Protected) Restore red-black properties after deletion (recolor/rotate).
293
- * @remarks Time O(log n), Space O(1)
423
+ * @remarks Time O(log n) average, Space O(1)
294
424
  * @param node - Child that replaced the deleted node (may be undefined).
295
425
  * @returns void
296
426
  */
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor, TreeCounterOptions } from '../../types';
8
+ import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, FamilyPosition, IterationType, NodePredicate, RBTNColor, TreeCounterOptions } from '../../types';
9
9
  import { BSTNode } from './bst';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { RedBlackTree } from './red-black-tree';
@@ -158,11 +158,11 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
158
158
  /**
159
159
  * Delete a node (or decrement its count) and rebalance if needed.
160
160
  * @remarks Time O(log N), Space O(1)
161
- * @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node.
161
+ * @param keyNodeEntryRawOrPredicate - Key, node, or [key, value] entry identifying the node.
162
162
  * @param [ignoreCount] - If true, remove the node regardless of its count.
163
163
  * @returns Array of deletion results including deleted node and a rebalance hint when present.
164
164
  */
165
- delete(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, ignoreCount?: boolean): BinaryTreeDeleteResult<TreeCounterNode<K, V>>[];
165
+ delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, TreeCounterNode<K, V>> | NodePredicate<TreeCounterNode<K, V> | null>, ignoreCount?: boolean): BinaryTreeDeleteResult<TreeCounterNode<K, V>>[];
166
166
  /**
167
167
  * Remove all nodes and reset aggregate counters.
168
168
  * @remarks Time O(N), Space O(1)
@@ -19,7 +19,7 @@ export interface IBinaryTree<K = any, V = any, R = any> {
19
19
  add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
20
20
  set(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
21
21
  addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
22
- delete(keyNodeEntryRawOrPredicate: R | BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
22
+ delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
23
23
  clear(): void;
24
24
  isEmpty(): boolean;
25
25
  get(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): V | undefined;