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.
- package/.github/workflows/ci.yml +9 -0
- package/CHANGELOG.md +1 -1
- package/README.md +14 -3
- package/README_CN.md +119 -275
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +20 -324
- package/dist/cjs/index.cjs +689 -182
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +693 -185
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +689 -182
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +693 -185
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/leetcode/avl-tree-counter.mjs +2957 -0
- package/dist/leetcode/avl-tree-multi-map.mjs +2889 -0
- package/dist/leetcode/avl-tree.mjs +2720 -0
- package/dist/leetcode/binary-tree.mjs +1594 -0
- package/dist/leetcode/bst.mjs +2398 -0
- package/dist/leetcode/deque.mjs +683 -0
- package/dist/leetcode/directed-graph.mjs +1733 -0
- package/dist/leetcode/doubly-linked-list.mjs +709 -0
- package/dist/leetcode/hash-map.mjs +493 -0
- package/dist/leetcode/heap.mjs +542 -0
- package/dist/leetcode/max-heap.mjs +375 -0
- package/dist/leetcode/max-priority-queue.mjs +383 -0
- package/dist/leetcode/min-heap.mjs +363 -0
- package/dist/leetcode/min-priority-queue.mjs +371 -0
- package/dist/leetcode/priority-queue.mjs +363 -0
- package/dist/leetcode/queue.mjs +943 -0
- package/dist/leetcode/red-black-tree.mjs +2765 -0
- package/dist/leetcode/singly-linked-list.mjs +754 -0
- package/dist/leetcode/stack.mjs +217 -0
- package/dist/leetcode/tree-counter.mjs +3039 -0
- package/dist/leetcode/tree-multi-map.mjs +2913 -0
- package/dist/leetcode/trie.mjs +413 -0
- package/dist/leetcode/undirected-graph.mjs +1650 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +25 -27
- package/dist/types/data-structures/binary-tree/bst.d.ts +13 -12
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +151 -21
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/types/interfaces/binary-tree.d.ts +1 -1
- package/dist/umd/data-structure-typed.js +689 -181
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +50 -172
- package/src/data-structures/base/linear-base.ts +2 -12
- package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
- package/src/data-structures/binary-tree/avl-tree.ts +15 -15
- package/src/data-structures/binary-tree/binary-tree.ts +57 -60
- package/src/data-structures/binary-tree/bst.ts +100 -26
- package/src/data-structures/binary-tree/red-black-tree.ts +586 -76
- package/src/data-structures/binary-tree/tree-counter.ts +25 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
- package/src/data-structures/queue/deque.ts +10 -0
- package/src/interfaces/binary-tree.ts +1 -1
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
- package/test/unit/data-structures/base/iterable-element-base.coverage.test.ts +106 -0
- package/test/unit/data-structures/base/iterable-element-base.more-branches.coverage.test.ts +61 -0
- package/test/unit/data-structures/base/linear-base.array.coverage.test.ts +168 -0
- package/test/unit/data-structures/base/linear-base.concat-else.coverage.test.ts +82 -0
- package/test/unit/data-structures/base/linear-base.coverage.test.ts +72 -0
- package/test/unit/data-structures/base/linear-base.more-branches.coverage.test.ts +417 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.more-branches-3.coverage.test.ts +146 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.more-branches.coverage.test.ts +93 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +30 -30
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.coverage.test.ts +108 -0
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.more-branches-2.coverage.test.ts +85 -0
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +46 -46
- package/test/unit/data-structures/binary-tree/avl-tree-node.familyPosition-root-left.coverage.test.ts +17 -0
- package/test/unit/data-structures/binary-tree/avl-tree.more-branches-2.coverage.test.ts +99 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
- package/test/unit/data-structures/binary-tree/binary-indexed-tree.more-branches.coverage.test.ts +18 -0
- package/test/unit/data-structures/binary-tree/binary-tree.more-branches.coverage.test.ts +56 -0
- package/test/unit/data-structures/binary-tree/binary-tree.remaining-branches.coverage.test.ts +229 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
- package/test/unit/data-structures/binary-tree/bst.bound-by-predicate.coverage.test.ts +33 -0
- package/test/unit/data-structures/binary-tree/bst.coverage.test.ts +94 -0
- package/test/unit/data-structures/binary-tree/bst.deletebykey.coverage.test.ts +70 -0
- package/test/unit/data-structures/binary-tree/bst.deletewhere.coverage.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/bst.floor-lower-predicate.coverage.test.ts +29 -0
- package/test/unit/data-structures/binary-tree/bst.floor-setmany.coverage.test.ts +72 -0
- package/test/unit/data-structures/binary-tree/bst.getnode.range-ensure.coverage.test.ts +22 -0
- package/test/unit/data-structures/binary-tree/bst.misc-branches.coverage.test.ts +100 -0
- package/test/unit/data-structures/binary-tree/bst.more-branches-2.coverage.test.ts +133 -0
- package/test/unit/data-structures/binary-tree/bst.more-branches-3.coverage.test.ts +45 -0
- package/test/unit/data-structures/binary-tree/bst.more-branches-4.coverage.test.ts +36 -0
- package/test/unit/data-structures/binary-tree/bst.more-branches-5.coverage.test.ts +40 -0
- package/test/unit/data-structures/binary-tree/bst.more.coverage.test.ts +39 -0
- package/test/unit/data-structures/binary-tree/bst.node-family.coverage.test.ts +29 -0
- package/test/unit/data-structures/binary-tree/bst.range-pruning.coverage.test.ts +43 -0
- package/test/unit/data-structures/binary-tree/bst.search-fastpath.coverage.test.ts +30 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +124 -154
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-corruption-repair.coverage.test.ts +66 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-max-update.coverage.test.ts +18 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-null.coverage.test.ts +53 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-stale-cache.coverage.test.ts +25 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-update.coverage.test.ts +23 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.cache-delete.coverage.test.ts +49 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.cache-edge.coverage.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.cache-stale-insert.coverage.test.ts +39 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.coverage.test.ts +334 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.delete-fixup.coverage.test.ts +68 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.delete-successor.coverage.test.ts +75 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.factories.coverage.test.ts +26 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-compare-update.coverage.test.ts +74 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-no-update.coverage.test.ts +44 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-nullish.coverage.test.ts +61 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-mapmode-defined.coverage.test.ts +35 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-mapmode-undefined.coverage.test.ts +43 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-more.coverage.test.ts +99 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint.coverage.test.ts +60 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.insert-cache-nullish.coverage.test.ts +29 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.insert-header-parent-nullish.coverage.test.ts +17 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.internal-walk.coverage.test.ts +57 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.minmax-cache.test.ts +65 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.misc-inputs.coverage.test.ts +17 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-2.coverage.test.ts +121 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-3.coverage.test.ts +55 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-4.coverage.test.ts +44 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.predsucc.coverage.test.ts +40 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.remaining-branches.coverage.test.ts +123 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.set-inputs.coverage.test.ts +64 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-parent-cache.coverage.test.ts +79 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-remaining.coverage.test.ts +44 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-uncovered.coverage.test.ts +74 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
- package/test/unit/data-structures/binary-tree/red-black-tree.update-branches.coverage.test.ts +30 -0
- package/test/unit/data-structures/binary-tree/segment-tree.more-branches.coverage.test.ts +31 -0
- package/test/unit/data-structures/binary-tree/tree-counter.coverage.test.ts +115 -0
- package/test/unit/data-structures/binary-tree/tree-counter.more-branches.coverage.test.ts +244 -0
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +41 -39
- package/test/unit/data-structures/binary-tree/tree-multi-map.coverage.test.ts +104 -0
- package/test/unit/data-structures/binary-tree/tree-multi-map.more-branches-2.coverage.test.ts +59 -0
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
- package/test/unit/data-structures/graph/abstract-graph.more-branches-2.coverage.test.ts +40 -0
- package/test/unit/data-structures/graph/abstract-graph.more-branches-3.coverage.test.ts +65 -0
- package/test/unit/data-structures/graph/abstract-graph.more-branches-4.coverage.test.ts +98 -0
- package/test/unit/data-structures/graph/abstract-graph.more-branches-5.coverage.test.ts +51 -0
- package/test/unit/data-structures/graph/abstract-graph.more-branches.coverage.test.ts +62 -0
- package/test/unit/data-structures/graph/directed-graph.more-branches-2.coverage.test.ts +38 -0
- package/test/unit/data-structures/graph/directed-graph.more-branches-3.coverage.test.ts +25 -0
- package/test/unit/data-structures/graph/directed-graph.more-branches.coverage.test.ts +82 -0
- package/test/unit/data-structures/graph/map-graph.more-branches.coverage.test.ts +22 -0
- package/test/unit/data-structures/graph/undirected-graph.more-branches-2.coverage.test.ts +35 -0
- package/test/unit/data-structures/graph/undirected-graph.more-branches.coverage.test.ts +87 -0
- package/test/unit/data-structures/hash/hash-map.more-branches.coverage.test.ts +64 -0
- package/test/unit/data-structures/hash/hash-map.toEntryFn-branch.coverage.test.ts +9 -0
- package/test/unit/data-structures/heap/heap.misc-branches.coverage.test.ts +110 -0
- package/test/unit/data-structures/heap/heap.remaining-branches.coverage.test.ts +22 -0
- package/test/unit/data-structures/heap/max-heap.coverage.test.ts +29 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.more-branches.coverage.test.ts +72 -0
- package/test/unit/data-structures/linked-list/linked-list.unshiftMany-else.coverage.test.ts +15 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.coverage.test.ts +221 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.more-branches.coverage.test.ts +86 -0
- package/test/unit/data-structures/linked-list/skip-linked-list.more-branches.coverage.test.ts +31 -0
- package/test/unit/data-structures/matrix/matrix.more-branches.coverage.test.ts +81 -0
- package/test/unit/data-structures/matrix/matrix.pivotElement-nullish.coverage.test.ts +28 -0
- package/test/unit/data-structures/priority-queue/max-priority-queue.more-branches.coverage.test.ts +10 -0
- package/test/unit/data-structures/priority-queue/priority-queue.coverage.test.ts +21 -0
- package/test/unit/data-structures/queue/deque.coverage.test.ts +173 -0
- package/test/unit/data-structures/queue/deque.more-branches-2.coverage.test.ts +39 -0
- package/test/unit/data-structures/queue/deque.more-branches-3.coverage.test.ts +9 -0
- package/test/unit/data-structures/queue/deque.more-branches.coverage.test.ts +95 -0
- package/test/unit/data-structures/queue/queue.coverage.test.ts +138 -0
- package/test/unit/data-structures/queue/queue.more-branches-2.coverage.test.ts +27 -0
- package/test/unit/data-structures/stack/stack.coverage.test.ts +112 -0
- package/test/unit/data-structures/tree/tree.more-branches.coverage.test.ts +9 -0
- package/test/unit/data-structures/trie/trie.more-branches-2.coverage.test.ts +51 -0
- package/test/utils/patch.ts +33 -0
- package/tsup.config.js +50 -21
- package/tsup.umd.config.js +29 -0
- package/tsup.node.config.js +0 -83
|
@@ -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
|
-
|
|
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
|
|
95
|
-
* @param items -
|
|
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
|
-
|
|
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).
|
|
@@ -147,7 +147,7 @@ export declare class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K
|
|
|
147
147
|
* @param [count] - How much to increase the node's count (default 1).
|
|
148
148
|
* @returns True if inserted/updated; false if ignored.
|
|
149
149
|
*/
|
|
150
|
-
|
|
150
|
+
set(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): boolean;
|
|
151
151
|
/**
|
|
152
152
|
* Delete a node (or decrement its count) and rebalance if needed.
|
|
153
153
|
* @remarks Time O(log N), Space O(1)
|
|
@@ -132,8 +132,8 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
132
132
|
* @returns True if it's a AVLTreeMultiMapNode, false otherwise.
|
|
133
133
|
*/
|
|
134
134
|
isNode(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): keyNodeOrEntry is AVLTreeMultiMapNode<K, V>;
|
|
135
|
-
|
|
136
|
-
|
|
135
|
+
set(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
|
|
136
|
+
set(key: K, value: V): boolean;
|
|
137
137
|
/**
|
|
138
138
|
* Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
|
|
139
139
|
* @remarks Time O(log N), Space O(1)
|
|
@@ -112,7 +112,7 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
112
112
|
}
|
|
113
113
|
/**
|
|
114
114
|
* Represents a self-balancing AVL (Adelson-Velsky and Landis) Tree.
|
|
115
|
-
* This tree extends BST and performs rotations on
|
|
115
|
+
* This tree extends BST and performs rotations on set/delete to maintain balance.
|
|
116
116
|
*
|
|
117
117
|
* @template K - The type of the key.
|
|
118
118
|
* @template V - The type of the value.
|
|
@@ -145,7 +145,7 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
145
145
|
* console.log(tree.size); // 5;
|
|
146
146
|
*
|
|
147
147
|
* // Add a new element
|
|
148
|
-
* tree.
|
|
148
|
+
* tree.set(3);
|
|
149
149
|
* console.log(tree.size); // 6;
|
|
150
150
|
* console.log([...tree.keys()]); // [1, 2, 3, 5, 8, 9];
|
|
151
151
|
* @example
|
|
@@ -208,7 +208,7 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
208
208
|
* console.log(universityTree.isAVLBalanced()); // true;
|
|
209
209
|
*
|
|
210
210
|
* // Add more universities
|
|
211
|
-
* universityTree.
|
|
211
|
+
* universityTree.set(6, { name: 'Oxford', rank: 6, students: 2000 });
|
|
212
212
|
* console.log(universityTree.isAVLBalanced()); // true;
|
|
213
213
|
*
|
|
214
214
|
* // Delete and verify balance is maintained
|
|
@@ -288,9 +288,9 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
288
288
|
export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
289
289
|
/**
|
|
290
290
|
* Creates an instance of AVLTree.
|
|
291
|
-
* @remarks Time O(N log N) (from `
|
|
291
|
+
* @remarks Time O(N log N) (from `setMany` with balanced set). Space O(N).
|
|
292
292
|
*
|
|
293
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
293
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
294
294
|
* @param [options] - Configuration options for the AVL tree.
|
|
295
295
|
*/
|
|
296
296
|
constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: AVLTreeOptions<K, V, R>);
|
|
@@ -312,14 +312,14 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
312
312
|
*/
|
|
313
313
|
isNode(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is AVLTreeNode<K, V>;
|
|
314
314
|
/**
|
|
315
|
-
*
|
|
316
|
-
* @remarks Time O(log N) (O(H) for BST
|
|
315
|
+
* Sets a new node to the AVL tree and balances the tree path.
|
|
316
|
+
* @remarks Time O(log N) (O(H) for BST set + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
317
317
|
*
|
|
318
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
318
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
319
319
|
* @param [value] - The value, if providing just a key.
|
|
320
320
|
* @returns True if the addition was successful, false otherwise.
|
|
321
321
|
*/
|
|
322
|
-
|
|
322
|
+
set(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
323
323
|
/**
|
|
324
324
|
* Deletes a node from the AVL tree and re-balances the tree.
|
|
325
325
|
* @remarks Time O(log N) (O(H) for BST delete + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
@@ -339,7 +339,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
339
339
|
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
340
340
|
/**
|
|
341
341
|
* Creates a new AVLTree by mapping each [key, value] pair.
|
|
342
|
-
* @remarks Time O(N log N) (O(N) iteration + O(log M) `
|
|
342
|
+
* @remarks Time O(N log N) (O(N) iteration + O(log M) `set` for each item into the new tree). Space O(N) for the new tree.
|
|
343
343
|
*
|
|
344
344
|
* @template MK - New key type.
|
|
345
345
|
* @template MV - New value type.
|
|
@@ -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';
|
|
@@ -113,7 +113,7 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
113
113
|
*
|
|
114
114
|
* @remarks
|
|
115
115
|
* This class implements a basic Binary Tree, not a Binary Search Tree.
|
|
116
|
-
* The `
|
|
116
|
+
* The `set` operation inserts nodes level-by-level (BFS) into the first available slot.
|
|
117
117
|
*
|
|
118
118
|
* @template K - The type of the key.
|
|
119
119
|
* @template V - The type of the value.
|
|
@@ -145,7 +145,7 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
145
145
|
* console.log(tree.size); // 9;
|
|
146
146
|
*
|
|
147
147
|
* // Add new element
|
|
148
|
-
* tree.
|
|
148
|
+
* tree.set(10, 'ten');
|
|
149
149
|
* console.log(tree.size); // 10;
|
|
150
150
|
* @example
|
|
151
151
|
* // BinaryTree get and has operations
|
|
@@ -269,9 +269,9 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
269
269
|
iterationType: IterationType;
|
|
270
270
|
/**
|
|
271
271
|
* Creates an instance of BinaryTree.
|
|
272
|
-
* @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) `
|
|
272
|
+
* @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.
|
|
273
273
|
*
|
|
274
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
274
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
275
275
|
* @param [options] - Configuration options for the tree.
|
|
276
276
|
*/
|
|
277
277
|
constructor(keysNodesEntriesOrRaws?: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BinaryTreeOptions<K, V, R>);
|
|
@@ -434,49 +434,47 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
434
434
|
* @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).
|
|
435
435
|
*
|
|
436
436
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
437
|
-
* @param [value] - The value, if providing just a key.
|
|
438
437
|
* @returns True if the addition was successful, false otherwise.
|
|
439
438
|
*/
|
|
440
|
-
add(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
439
|
+
add(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean;
|
|
441
440
|
/**
|
|
442
441
|
* Adds or updates a new node to the tree.
|
|
443
|
-
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation
|
|
442
|
+
* @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).
|
|
444
443
|
*
|
|
445
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
444
|
+
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
446
445
|
* @param [value] - The value, if providing just a key.
|
|
447
446
|
* @returns True if the addition was successful, false otherwise.
|
|
448
447
|
*/
|
|
449
448
|
set(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
450
449
|
/**
|
|
451
450
|
* Adds multiple items to the tree.
|
|
452
|
-
* @remarks Time O(N * M), where N is the number of items to
|
|
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).
|
|
453
452
|
*
|
|
454
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
455
|
-
* @
|
|
456
|
-
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
453
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
454
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
457
455
|
*/
|
|
458
|
-
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
456
|
+
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>): boolean[];
|
|
459
457
|
/**
|
|
460
458
|
* Adds or updates multiple items to the tree.
|
|
461
|
-
* @remarks Time O(N * M), where N is the number of items to
|
|
459
|
+
* @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).
|
|
462
460
|
*
|
|
463
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
461
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
464
462
|
* @param [values] - An optional parallel iterable of values.
|
|
465
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
463
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
466
464
|
*/
|
|
467
465
|
setMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
|
|
468
466
|
/**
|
|
469
|
-
* Merges another tree into this one by
|
|
470
|
-
* @remarks Time O(N * M), same as `
|
|
467
|
+
* Merges another tree into this one by seting all its nodes.
|
|
468
|
+
* @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`).
|
|
471
469
|
*
|
|
472
470
|
* @param anotherTree - The tree to merge.
|
|
473
471
|
*/
|
|
474
472
|
merge(anotherTree: BinaryTree<K, V, R>): void;
|
|
475
473
|
/**
|
|
476
474
|
* Clears the tree and refills it with new items.
|
|
477
|
-
* @remarks Time O(N) (for `clear`) + O(N * M) (for `
|
|
475
|
+
* @remarks Time O(N) (for `clear`) + O(N * M) (for `setMany`) = O(N * M). Space O(M) (from `setMany`).
|
|
478
476
|
*
|
|
479
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
477
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
480
478
|
* @param [values] - An optional parallel iterable of values.
|
|
481
479
|
*/
|
|
482
480
|
refill(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): void;
|
|
@@ -484,10 +482,10 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
484
482
|
* Deletes a node from the tree.
|
|
485
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).
|
|
486
484
|
*
|
|
487
|
-
* @param
|
|
485
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
488
486
|
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
489
487
|
*/
|
|
490
|
-
delete(
|
|
488
|
+
delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
|
|
491
489
|
search(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne?: boolean): (K | undefined)[];
|
|
492
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>[];
|
|
493
491
|
/**
|
|
@@ -624,14 +622,14 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
624
622
|
morris<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): ReturnType<C>[];
|
|
625
623
|
/**
|
|
626
624
|
* Clones the tree.
|
|
627
|
-
* @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `
|
|
625
|
+
* @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.
|
|
628
626
|
*
|
|
629
627
|
* @returns A new, cloned instance of the tree.
|
|
630
628
|
*/
|
|
631
629
|
clone(): this;
|
|
632
630
|
/**
|
|
633
631
|
* Creates a new tree containing only the entries that satisfy the predicate.
|
|
634
|
-
* @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) `
|
|
632
|
+
* @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.
|
|
635
633
|
*
|
|
636
634
|
* @param predicate - A function to test each [key, value] pair.
|
|
637
635
|
* @param [thisArg] - `this` context for the predicate.
|
|
@@ -722,8 +720,8 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
722
720
|
*/
|
|
723
721
|
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): [BinaryTreeNode<K, V> | null | undefined, V | undefined];
|
|
724
722
|
/**
|
|
725
|
-
* (Protected) Helper for cloning. Performs a BFS and
|
|
726
|
-
* @remarks Time O(N * M) (O(N) BFS + O(M) `
|
|
723
|
+
* (Protected) Helper for cloning. Performs a BFS and sets all nodes to the new tree.
|
|
724
|
+
* @remarks Time O(N * M) (O(N) BFS + O(M) `set` for each node).
|
|
727
725
|
*
|
|
728
726
|
* @param cloned - The new, empty tree instance to populate.
|
|
729
727
|
*/
|
|
@@ -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
|
-
*
|
|
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_____
|
|
@@ -145,8 +146,8 @@ export declare class BSTNode<K = any, V = any> {
|
|
|
145
146
|
* console.log(bst.size); // 16;
|
|
146
147
|
*
|
|
147
148
|
* // Add new elements
|
|
148
|
-
* bst.
|
|
149
|
-
* bst.
|
|
149
|
+
* bst.set(17);
|
|
150
|
+
* bst.set(0);
|
|
150
151
|
* console.log(bst.size); // 18;
|
|
151
152
|
*
|
|
152
153
|
* // Verify keys are searchable
|
|
@@ -192,7 +193,7 @@ export declare class BSTNode<K = any, V = any> {
|
|
|
192
193
|
*
|
|
193
194
|
* // Merge datasets into a single BinarySearchTree
|
|
194
195
|
* const merged = new BST<number, string>(dataset1);
|
|
195
|
-
* merged.
|
|
196
|
+
* merged.setMany(dataset2);
|
|
196
197
|
* merged.merge(dataset3);
|
|
197
198
|
*
|
|
198
199
|
* // Verify merged dataset is in sorted order
|
|
@@ -276,7 +277,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
276
277
|
* Creates an instance of BST.
|
|
277
278
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
278
279
|
*
|
|
279
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
280
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
280
281
|
* @param [options] - Configuration options for the BST, including comparator.
|
|
281
282
|
*/
|
|
282
283
|
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BSTOptions<K, V, R>);
|
|
@@ -359,24 +360,24 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
359
360
|
* Adds a new node to the BST based on key comparison.
|
|
360
361
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
361
362
|
*
|
|
362
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
363
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
363
364
|
* @param [value] - The value, if providing just a key.
|
|
364
365
|
* @returns True if the addition was successful, false otherwise.
|
|
365
366
|
*/
|
|
366
|
-
|
|
367
|
+
set(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
367
368
|
/**
|
|
368
369
|
* Adds multiple items to the tree.
|
|
369
|
-
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced
|
|
370
|
+
* @remarks If `isBalanceAdd` is true, sorts the input and builds a balanced tree. Time O(N log N) (due to sort and balanced set).
|
|
370
371
|
* If false, adds items one by one. Time O(N * H), which is O(N^2) worst-case.
|
|
371
372
|
* Space O(N) for sorting and recursion/iteration stack.
|
|
372
373
|
*
|
|
373
|
-
* @param keysNodesEntriesOrRaws - An iterable of items to
|
|
374
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
374
375
|
* @param [values] - An optional parallel iterable of values.
|
|
375
376
|
* @param [isBalanceAdd=true] - If true, builds a balanced tree from the items.
|
|
376
|
-
* @param [iterationType=this.iterationType] - The traversal method for balanced
|
|
377
|
-
* @returns An array of booleans indicating the success of each individual `
|
|
377
|
+
* @param [iterationType=this.iterationType] - The traversal method for balanced set (recursive or iterative).
|
|
378
|
+
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
378
379
|
*/
|
|
379
|
-
|
|
380
|
+
setMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
380
381
|
/**
|
|
381
382
|
* Returns the first key with a value >= target.
|
|
382
383
|
* Equivalent to Java TreeMap.ceiling.
|
|
@@ -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
|
|
17
|
-
* @remarks Time O(
|
|
18
|
-
* @param key -
|
|
19
|
-
* @param [value]-
|
|
20
|
-
* @param color -
|
|
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
|
|
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
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
* @
|
|
243
|
-
|
|
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)
|
|
244
302
|
*/
|
|
245
|
-
|
|
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)
|
|
358
|
+
*/
|
|
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
|
|
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(
|
|
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';
|
|
@@ -154,15 +154,15 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
154
154
|
* @param [count] - How much to increase the node's count (default 1).
|
|
155
155
|
* @returns True if inserted/updated; false if ignored.
|
|
156
156
|
*/
|
|
157
|
-
|
|
157
|
+
set(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): boolean;
|
|
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
|
|
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(
|
|
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)
|
|
@@ -297,8 +297,8 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
297
297
|
* @returns True if it's a TreeMultiMapNode, false otherwise.
|
|
298
298
|
*/
|
|
299
299
|
isNode(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): keyNodeOrEntry is TreeMultiMapNode<K, V>;
|
|
300
|
-
|
|
301
|
-
|
|
300
|
+
set(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
|
|
301
|
+
set(key: K, value: V): boolean;
|
|
302
302
|
/**
|
|
303
303
|
* Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
|
|
304
304
|
* @remarks Time O(log N), Space O(1)
|