max-priority-queue-typed 2.3.0 → 2.4.1

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 (43) hide show
  1. package/dist/types/data-structures/base/linear-base.d.ts +6 -6
  2. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -6
  3. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -1
  4. package/dist/types/data-structures/binary-tree/index.d.ts +3 -3
  5. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +150 -20
  6. package/dist/types/data-structures/binary-tree/tree-map.d.ts +188 -0
  7. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +238 -147
  8. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +270 -0
  9. package/dist/types/data-structures/binary-tree/tree-set.d.ts +181 -0
  10. package/dist/types/interfaces/binary-tree.d.ts +2 -2
  11. package/dist/types/types/data-structures/binary-tree/index.d.ts +3 -3
  12. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +33 -0
  13. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +16 -0
  14. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +33 -0
  15. package/package.json +2 -2
  16. package/src/data-structures/base/linear-base.ts +2 -12
  17. package/src/data-structures/binary-tree/avl-tree.ts +1 -1
  18. package/src/data-structures/binary-tree/binary-tree.ts +45 -21
  19. package/src/data-structures/binary-tree/bst.ts +85 -10
  20. package/src/data-structures/binary-tree/index.ts +3 -3
  21. package/src/data-structures/binary-tree/red-black-tree.ts +568 -76
  22. package/src/data-structures/binary-tree/tree-map.ts +439 -0
  23. package/src/data-structures/binary-tree/tree-multi-map.ts +488 -325
  24. package/src/data-structures/binary-tree/tree-multi-set.ts +502 -0
  25. package/src/data-structures/binary-tree/tree-set.ts +407 -0
  26. package/src/data-structures/queue/deque.ts +10 -0
  27. package/src/interfaces/binary-tree.ts +2 -2
  28. package/src/types/data-structures/binary-tree/index.ts +3 -3
  29. package/src/types/data-structures/binary-tree/tree-map.ts +45 -0
  30. package/src/types/data-structures/binary-tree/tree-multi-set.ts +19 -0
  31. package/src/types/data-structures/binary-tree/tree-set.ts +39 -0
  32. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -236
  33. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -197
  34. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +0 -243
  35. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
  36. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
  37. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
  38. package/src/data-structures/binary-tree/avl-tree-counter.ts +0 -539
  39. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +0 -438
  40. package/src/data-structures/binary-tree/tree-counter.ts +0 -575
  41. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +0 -3
  42. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +0 -3
  43. package/src/types/data-structures/binary-tree/tree-counter.ts +0 -3
@@ -1,236 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Pablo Zeng
5
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import type { AVLTreeCounterOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
9
- import { IBinaryTree } from '../../interfaces';
10
- import { AVLTree } from './avl-tree';
11
- /**
12
- * AVL node with an extra 'count' field; keeps parent/child links.
13
- * @remarks Time O(1), Space O(1)
14
- * @template K
15
- * @template V
16
- */
17
- export declare class AVLTreeCounterNode<K = any, V = any> {
18
- key: K;
19
- value?: V;
20
- parent?: AVLTreeCounterNode<K, V>;
21
- /**
22
- * Create an AVL counter node.
23
- * @remarks Time O(1), Space O(1)
24
- * @param key - Key of the node.
25
- * @param [value] - Associated value (ignored in map mode).
26
- * @param [count] - Initial count for this node (default 1).
27
- * @returns New AVLTreeCounterNode instance.
28
- */
29
- constructor(key: K, value?: V, count?: number);
30
- _left?: AVLTreeCounterNode<K, V> | null | undefined;
31
- /**
32
- * Get the left child pointer.
33
- * @remarks Time O(1), Space O(1)
34
- * @returns Left child node, or null/undefined.
35
- */
36
- get left(): AVLTreeCounterNode<K, V> | null | undefined;
37
- /**
38
- * Set the left child and update its parent pointer.
39
- * @remarks Time O(1), Space O(1)
40
- * @param v - New left child node, or null/undefined.
41
- * @returns void
42
- */
43
- set left(v: AVLTreeCounterNode<K, V> | null | undefined);
44
- _right?: AVLTreeCounterNode<K, V> | null | undefined;
45
- /**
46
- * Get the right child pointer.
47
- * @remarks Time O(1), Space O(1)
48
- * @returns Right child node, or null/undefined.
49
- */
50
- get right(): AVLTreeCounterNode<K, V> | null | undefined;
51
- /**
52
- * Set the right child and update its parent pointer.
53
- * @remarks Time O(1), Space O(1)
54
- * @param v - New right child node, or null/undefined.
55
- * @returns void
56
- */
57
- set right(v: AVLTreeCounterNode<K, V> | null | undefined);
58
- _height: number;
59
- /**
60
- * Gets the height of the node (used in self-balancing trees).
61
- * @remarks Time O(1), Space O(1)
62
- *
63
- * @returns The height.
64
- */
65
- get height(): number;
66
- /**
67
- * Sets the height of the node.
68
- * @remarks Time O(1), Space O(1)
69
- *
70
- * @param value - The new height.
71
- */
72
- set height(value: number);
73
- _color: RBTNColor;
74
- /**
75
- * Gets the color of the node (used in Red-Black trees).
76
- * @remarks Time O(1), Space O(1)
77
- *
78
- * @returns The node's color.
79
- */
80
- get color(): RBTNColor;
81
- /**
82
- * Sets the color of the node.
83
- * @remarks Time O(1), Space O(1)
84
- *
85
- * @param value - The new color.
86
- */
87
- set color(value: RBTNColor);
88
- _count: number;
89
- /**
90
- * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
91
- * @remarks Time O(1), Space O(1)
92
- *
93
- * @returns The subtree node count.
94
- */
95
- get count(): number;
96
- /**
97
- * Sets the count of nodes in the subtree.
98
- * @remarks Time O(1), Space O(1)
99
- *
100
- * @param value - The new count.
101
- */
102
- set count(value: number);
103
- /**
104
- * Gets the position of the node relative to its parent.
105
- * @remarks Time O(1), Space O(1)
106
- *
107
- * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
108
- */
109
- get familyPosition(): FamilyPosition;
110
- }
111
- /**
112
- * AVL tree that tracks an aggregate 'count' across nodes; supports balanced insert/delete and map-like mode.
113
- * @remarks Time O(1), Space O(1)
114
- * @template K
115
- * @template V
116
- * @template R
117
- */
118
- export declare class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R> implements IBinaryTree<K, V, R> {
119
- /**
120
- * Create a AVLTreeCounter instance
121
- * @remarks Time O(n), Space O(n)
122
- * @param keysNodesEntriesOrRaws
123
- * @param options
124
- * @returns New AVLTreeCounterNode instance.
125
- */
126
- constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: AVLTreeCounterOptions<K, V, R>);
127
- protected _count: number;
128
- get count(): number;
129
- /**
130
- * Compute the total count by traversing the tree (sums node.count).
131
- * @remarks Time O(N), Space O(H)
132
- * @returns Total count recomputed from nodes.
133
- */
134
- getComputedCount(): number;
135
- createNode(key: K, value?: V, count?: number): AVLTreeCounterNode<K, V>;
136
- /**
137
- * Type guard: check whether the input is an AVLTreeCounterNode.
138
- * @remarks Time O(1), Space O(1)
139
- * @returns True if the value is an AVLTreeCounterNode.
140
- */
141
- isNode(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is AVLTreeCounterNode<K, V>;
142
- /**
143
- * Insert or increment a node and update aggregate count.
144
- * @remarks Time O(log N), Space O(1)
145
- * @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
146
- * @param [value] - Value when a bare key is provided (ignored in map mode).
147
- * @param [count] - How much to increase the node's count (default 1).
148
- * @returns True if inserted/updated; false if ignored.
149
- */
150
- set(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): boolean;
151
- /**
152
- * Delete a node (or decrement its count) and rebalance if needed.
153
- * @remarks Time O(log N), Space O(1)
154
- * @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node.
155
- * @param [ignoreCount] - If true, remove the node regardless of its count.
156
- * @returns Array of deletion results including deleted node and a rebalance hint when present.
157
- */
158
- delete(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, ignoreCount?: boolean): BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[];
159
- /**
160
- * Remove all nodes and reset aggregate counters.
161
- * @remarks Time O(N), Space O(1)
162
- * @returns void
163
- */
164
- clear(): void;
165
- /**
166
- * Rebuild the tree into a perfectly balanced form using in-order nodes.
167
- * @remarks Time O(N), Space O(N)
168
- * @param [iterationType] - Traversal style to use when constructing the balanced tree.
169
- * @returns True if rebalancing succeeded (tree not empty).
170
- */
171
- perfectlyBalance(iterationType?: IterationType): boolean;
172
- /**
173
- * Deep copy this tree, preserving map mode and aggregate counts.
174
- * @remarks Time O(N), Space O(N)
175
- * @returns A deep copy of this tree.
176
- */
177
- clone(): this;
178
- /**
179
- * Create a new AVLTreeCounter by mapping each [key, value] entry.
180
- * @remarks Time O(N log N), Space O(N)
181
- * @template MK
182
- * @template MV
183
- * @template MR
184
- * @param callback - Function mapping (key, value, index, tree) → [newKey, newValue].
185
- * @param [options] - Options for the output tree.
186
- * @param [thisArg] - Value for `this` inside the callback.
187
- * @returns A new AVLTreeCounter with mapped entries.
188
- */
189
- map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<AVLTreeCounterOptions<MK, MV, MR>>, thisArg?: unknown): AVLTreeCounter<MK, MV, MR>;
190
- /**
191
- * (Protected) Create an empty instance of the same concrete class.
192
- * @remarks Time O(1), Space O(1)
193
- * @template TK
194
- * @template TV
195
- * @template TR
196
- * @param [options] - Optional constructor options for the like-kind instance.
197
- * @returns An empty like-kind instance.
198
- */
199
- protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeCounterOptions<TK, TV, TR>>): this;
200
- /**
201
- * (Protected) Create a like-kind instance and seed it from an iterable.
202
- * @remarks Time O(N log N), Space O(N)
203
- * @template TK
204
- * @template TV
205
- * @template TR
206
- * @param iter - Iterable used to seed the new tree.
207
- * @param [options] - Options merged with the current snapshot.
208
- * @returns A like-kind AVLTreeCounter built from the iterable.
209
- */
210
- protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeCounterNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeCounterOptions<TK, TV, TR>>): AVLTreeCounter<TK, TV, TR>;
211
- /**
212
- * (Protected) Normalize input into a node plus its effective value and count.
213
- * @remarks Time O(1), Space O(1)
214
- * @param keyNodeOrEntry - Key, node, or [key, value] entry.
215
- * @param [value] - Value used when a bare key is provided.
216
- * @param [count] - Count increment to apply (default 1).
217
- * @returns Tuple [node, value] where node may be undefined.
218
- */
219
- protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): [AVLTreeCounterNode<K, V> | undefined, V | undefined];
220
- /**
221
- * (Protected) Swap keys/values/counters between the source and destination nodes.
222
- * @remarks Time O(1), Space O(1)
223
- * @param srcNode - Source node (or key) whose properties will be moved.
224
- * @param destNode - Destination node (or key) to receive properties.
225
- * @returns Destination node after swap, or undefined.
226
- */
227
- protected _swapProperties(srcNode: BSTNOptKeyOrNode<K, AVLTreeCounterNode<K, V>>, destNode: BSTNOptKeyOrNode<K, AVLTreeCounterNode<K, V>>): AVLTreeCounterNode<K, V> | undefined;
228
- /**
229
- * (Protected) Replace one node by another and adjust counters accordingly.
230
- * @remarks Time O(1), Space O(1)
231
- * @param oldNode - Node being replaced.
232
- * @param newNode - Replacement node.
233
- * @returns The new node after replacement.
234
- */
235
- protected _replaceNode(oldNode: AVLTreeCounterNode<K, V>, newNode: AVLTreeCounterNode<K, V>): AVLTreeCounterNode<K, V>;
236
- }
@@ -1,197 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Pablo Zeng
5
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import type { AVLTreeMultiMapOptions, ElemOf, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
9
- import { AVLTree, AVLTreeNode } from './avl-tree';
10
- import { IBinaryTree } from '../../interfaces';
11
- /**
12
- * Node used by AVLTreeMultiMap; stores the key with a bucket of values (array).
13
- * @remarks Time O(1), Space O(1)
14
- * @template K
15
- * @template V
16
- */
17
- export declare class AVLTreeMultiMapNode<K = any, V = any> {
18
- key: K;
19
- value?: V[];
20
- parent?: AVLTreeMultiMapNode<K, V>;
21
- /**
22
- * Create an AVLTreeMultiMap node with a value bucket.
23
- * @remarks Time O(1), Space O(1)
24
- * @param key - Key of the node.
25
- * @param value - Initial array of values.
26
- * @returns New AVLTreeMultiMapNode instance.
27
- */
28
- constructor(key: K, value?: V[]);
29
- _left?: AVLTreeMultiMapNode<K, V> | null | undefined;
30
- /**
31
- * Get the left child pointer.
32
- * @remarks Time O(1), Space O(1)
33
- * @returns Left child node, or null/undefined.
34
- */
35
- get left(): AVLTreeMultiMapNode<K, V> | null | undefined;
36
- /**
37
- * Set the left child and update its parent pointer.
38
- * @remarks Time O(1), Space O(1)
39
- * @param v - New left child node, or null/undefined.
40
- * @returns void
41
- */
42
- set left(v: AVLTreeMultiMapNode<K, V> | null | undefined);
43
- _right?: AVLTreeMultiMapNode<K, V> | null | undefined;
44
- /**
45
- * Get the right child pointer.
46
- * @remarks Time O(1), Space O(1)
47
- * @returns Right child node, or null/undefined.
48
- */
49
- get right(): AVLTreeMultiMapNode<K, V> | null | undefined;
50
- /**
51
- * Set the right child and update its parent pointer.
52
- * @remarks Time O(1), Space O(1)
53
- * @param v - New right child node, or null/undefined.
54
- * @returns void
55
- */
56
- set right(v: AVLTreeMultiMapNode<K, V> | null | undefined);
57
- _height: number;
58
- /**
59
- * Gets the height of the node (used in self-balancing trees).
60
- * @remarks Time O(1), Space O(1)
61
- *
62
- * @returns The height.
63
- */
64
- get height(): number;
65
- /**
66
- * Sets the height of the node.
67
- * @remarks Time O(1), Space O(1)
68
- *
69
- * @param value - The new height.
70
- */
71
- set height(value: number);
72
- _color: RBTNColor;
73
- /**
74
- * Gets the color of the node (used in Red-Black trees).
75
- * @remarks Time O(1), Space O(1)
76
- *
77
- * @returns The node's color.
78
- */
79
- get color(): RBTNColor;
80
- /**
81
- * Sets the color of the node.
82
- * @remarks Time O(1), Space O(1)
83
- *
84
- * @param value - The new color.
85
- */
86
- set color(value: RBTNColor);
87
- _count: number;
88
- /**
89
- * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
90
- * @remarks Time O(1), Space O(1)
91
- *
92
- * @returns The subtree node count.
93
- */
94
- get count(): number;
95
- /**
96
- * Sets the count of nodes in the subtree.
97
- * @remarks Time O(1), Space O(1)
98
- *
99
- * @param value - The new count.
100
- */
101
- set count(value: number);
102
- /**
103
- * Gets the position of the node relative to its parent.
104
- * @remarks Time O(1), Space O(1)
105
- *
106
- * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
107
- */
108
- get familyPosition(): FamilyPosition;
109
- }
110
- /**
111
- * AVL-tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
112
- * @remarks Time O(1), Space O(1)
113
- * @template K
114
- * @template V
115
- * @template R
116
- */
117
- export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[], R> implements IBinaryTree<K, V[], R> {
118
- /**
119
- * Create an AVLTreeMultiMap and optionally bulk-insert items.
120
- * @remarks Time O(N log N), Space O(N)
121
- * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
122
- * @param [options] - Options for AVLTreeMultiMap (comparator, reverse, map mode).
123
- * @returns New AVLTreeMultiMap instance.
124
- */
125
- constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: AVLTreeMultiMapOptions<K, V[], R>);
126
- createNode(key: K, value?: V[]): AVLTreeMultiMapNode<K, V>;
127
- /**
128
- * Checks if the given item is a `AVLTreeMultiMapNode` instance.
129
- * @remarks Time O(1), Space O(1)
130
- *
131
- * @param keyNodeOrEntry - The item to check.
132
- * @returns True if it's a AVLTreeMultiMapNode, false otherwise.
133
- */
134
- isNode(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): keyNodeOrEntry is AVLTreeMultiMapNode<K, V>;
135
- set(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
136
- set(key: K, value: V): boolean;
137
- /**
138
- * Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
139
- * @remarks Time O(log N), Space O(1)
140
- * @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
141
- * @param value - Value to remove from the bucket.
142
- * @returns True if the value was removed; false if not found.
143
- */
144
- deleteValue(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, value: V): boolean;
145
- /**
146
- * Rebuild the tree into a perfectly balanced form using in-order nodes.
147
- * @remarks Time O(N), Space O(N)
148
- * @param [iterationType] - Traversal style to use when constructing the balanced tree.
149
- * @returns True if rebalancing succeeded (tree not empty).
150
- */
151
- perfectlyBalance(iterationType?: IterationType): boolean;
152
- /**
153
- * Create a new tree by mapping each [key, values] bucket.
154
- * @remarks Time O(N log N), Space O(N)
155
- * @template MK
156
- * @template MVArr
157
- * @template MR
158
- * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
159
- * @param [options] - Options for the output tree.
160
- * @param [thisArg] - Value for `this` inside the callback.
161
- * @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
162
- */
163
- map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<AVLTreeMultiMapOptions<MK, MVArr, MR>>, thisArg?: unknown): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
164
- /**
165
- * Create a new tree by mapping each [key, values] bucket.
166
- * @remarks Time O(N log N), Space O(N)
167
- * @template MK
168
- * @template MV
169
- * @template MR
170
- * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
171
- * @param [options] - Options for the output tree.
172
- * @param [thisArg] - Value for `this` inside the callback.
173
- * @returns A new AVLTree when mapping to non-array values; see overloads.
174
- */
175
- map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<AVLTreeMultiMapOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
176
- /**
177
- * (Protected) Create an empty instance of the same concrete class.
178
- * @remarks Time O(1), Space O(1)
179
- * @template TK
180
- * @template TV
181
- * @template TR
182
- * @param [options] - Optional constructor options for the like-kind instance.
183
- * @returns An empty like-kind instance.
184
- */
185
- protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>): this;
186
- /**
187
- * (Protected) Create a like-kind instance and seed it from an iterable.
188
- * @remarks Time O(N log N), Space O(N)
189
- * @template TK
190
- * @template TV
191
- * @template TR
192
- * @param iter - Iterable used to seed the new tree.
193
- * @param [options] - Options merged with the current snapshot.
194
- * @returns A like-kind AVLTree built from the iterable.
195
- */
196
- protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
197
- }
@@ -1,243 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Pablo Zeng
5
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor, TreeCounterOptions } from '../../types';
9
- import { BSTNode } from './bst';
10
- import { IBinaryTree } from '../../interfaces';
11
- import { RedBlackTree } from './red-black-tree';
12
- /**
13
- * RB-tree node with an extra 'count' field; keeps parent/child links.
14
- * @remarks Time O(1), Space O(1)
15
- * @template K
16
- * @template V
17
- */
18
- export declare class TreeCounterNode<K = any, V = any> {
19
- key: K;
20
- value?: V;
21
- parent?: TreeCounterNode<K, V>;
22
- /**
23
- * Create a tree counter node.
24
- * @remarks Time O(1), Space O(1)
25
- * @param key - Key of the node.
26
- * @param [value] - Value associated with the key (ignored in map mode).
27
- * @param [count] - Initial count for this node (default 1).
28
- * @param color - Initial color ('RED' or 'BLACK').
29
- * @returns New TreeCounterNode instance.
30
- */
31
- constructor(key: K, value?: V, count?: number, color?: RBTNColor);
32
- _left?: TreeCounterNode<K, V> | null | undefined;
33
- /**
34
- * Get the left child pointer.
35
- * @remarks Time O(1), Space O(1)
36
- * @returns Left child node, or null/undefined.
37
- */
38
- get left(): TreeCounterNode<K, V> | null | undefined;
39
- /**
40
- * Set the left child and update its parent pointer.
41
- * @remarks Time O(1), Space O(1)
42
- * @param v - New left child node, or null/undefined.
43
- * @returns void
44
- */
45
- set left(v: TreeCounterNode<K, V> | null | undefined);
46
- _right?: TreeCounterNode<K, V> | null | undefined;
47
- /**
48
- * Get the right child pointer.
49
- * @remarks Time O(1), Space O(1)
50
- * @returns Right child node, or null/undefined.
51
- */
52
- get right(): TreeCounterNode<K, V> | null | undefined;
53
- /**
54
- * Set the right child and update its parent pointer.
55
- * @remarks Time O(1), Space O(1)
56
- * @param v - New right child node, or null/undefined.
57
- * @returns void
58
- */
59
- set right(v: TreeCounterNode<K, V> | null | undefined);
60
- _height: number;
61
- /**
62
- * Gets the height of the node (used in self-balancing trees).
63
- * @remarks Time O(1), Space O(1)
64
- *
65
- * @returns The height.
66
- */
67
- get height(): number;
68
- /**
69
- * Sets the height of the node.
70
- * @remarks Time O(1), Space O(1)
71
- *
72
- * @param value - The new height.
73
- */
74
- set height(value: number);
75
- _color: RBTNColor;
76
- /**
77
- * Gets the color of the node (used in Red-Black trees).
78
- * @remarks Time O(1), Space O(1)
79
- *
80
- * @returns The node's color.
81
- */
82
- get color(): RBTNColor;
83
- /**
84
- * Sets the color of the node.
85
- * @remarks Time O(1), Space O(1)
86
- *
87
- * @param value - The new color.
88
- */
89
- set color(value: RBTNColor);
90
- _count: number;
91
- /**
92
- * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
93
- * @remarks Time O(1), Space O(1)
94
- *
95
- * @returns The subtree node count.
96
- */
97
- get count(): number;
98
- /**
99
- * Sets the count of nodes in the subtree.
100
- * @remarks Time O(1), Space O(1)
101
- *
102
- * @param value - The new count.
103
- */
104
- set count(value: number);
105
- /**
106
- * Gets the position of the node relative to its parent.
107
- * @remarks Time O(1), Space O(1)
108
- *
109
- * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
110
- */
111
- get familyPosition(): FamilyPosition;
112
- }
113
- /**
114
- * Red-Black Tree–based counter map (key → value with per-node count). Supports O(log N) updates and map-like mode.
115
- * @remarks Time O(1), Space O(1)
116
- * @template K
117
- * @template V
118
- * @template R
119
- */
120
- export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R> implements IBinaryTree<K, V, R> {
121
- /**
122
- * Create a TreeCounter and optionally bulk-insert items.
123
- * @remarks Time O(N log N), Space O(N)
124
- * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
125
- * @param [options] - Options for TreeCounter (comparator, reverse, map mode).
126
- * @returns New TreeCounter instance.
127
- */
128
- constructor(keysNodesEntriesOrRaws?: Iterable<K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: TreeCounterOptions<K, V, R>);
129
- protected _count: number;
130
- /**
131
- * Get the total aggregate count across all nodes.
132
- * @remarks Time O(1), Space O(1)
133
- * @returns Total count.
134
- */
135
- get count(): number;
136
- /**
137
- * Compute the total count by traversing the tree (sums node.count).
138
- * @remarks Time O(N), Space O(H)
139
- * @returns Total count recomputed from nodes.
140
- */
141
- getComputedCount(): number;
142
- createNode(key: K, value?: V, color?: RBTNColor, count?: number): TreeCounterNode<K, V>;
143
- /**
144
- * Type guard: check whether the input is a TreeCounterNode.
145
- * @remarks Time O(1), Space O(1)
146
- * @returns True if the value is a TreeCounterNode.
147
- */
148
- isNode(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is TreeCounterNode<K, V>;
149
- /**
150
- * Insert or increment a node and update aggregate count.
151
- * @remarks Time O(log N), Space O(1)
152
- * @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
153
- * @param [value] - Value when a bare key is provided (ignored in map mode).
154
- * @param [count] - How much to increase the node's count (default 1).
155
- * @returns True if inserted/updated; false if ignored.
156
- */
157
- set(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): boolean;
158
- /**
159
- * Delete a node (or decrement its count) and rebalance if needed.
160
- * @remarks Time O(log N), Space O(1)
161
- * @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node.
162
- * @param [ignoreCount] - If true, remove the node regardless of its count.
163
- * @returns Array of deletion results including deleted node and a rebalance hint when present.
164
- */
165
- delete(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, ignoreCount?: boolean): BinaryTreeDeleteResult<TreeCounterNode<K, V>>[];
166
- /**
167
- * Remove all nodes and reset aggregate counters.
168
- * @remarks Time O(N), Space O(1)
169
- * @returns void
170
- */
171
- clear(): void;
172
- /**
173
- * Rebuild the tree into a perfectly balanced form using in-order nodes.
174
- * @remarks Time O(N), Space O(N)
175
- * @param [iterationType] - Traversal style to use when constructing the balanced tree.
176
- * @returns True if rebalancing succeeded (tree not empty).
177
- */
178
- perfectlyBalance(iterationType?: IterationType): boolean;
179
- /**
180
- * Create a new TreeCounter by mapping each [key, value] entry.
181
- * @remarks Time O(N log N), Space O(N)
182
- * @template MK
183
- * @template MV
184
- * @template MR
185
- * @param callback - Function mapping (key, value, index, tree) → [newKey, newValue].
186
- * @param [options] - Options for the output tree.
187
- * @param [thisArg] - Value for `this` inside the callback.
188
- * @returns A new TreeCounter with mapped entries.
189
- */
190
- map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<TreeCounterOptions<MK, MV, MR>>, thisArg?: unknown): TreeCounter<MK, MV, MR>;
191
- /**
192
- * Deep copy this tree, preserving map mode and aggregate counts.
193
- * @remarks Time O(N), Space O(N)
194
- * @returns A deep copy of this tree.
195
- */
196
- clone(): this;
197
- /**
198
- * (Protected) Create an empty instance of the same concrete class.
199
- * @remarks Time O(1), Space O(1)
200
- * @template TK
201
- * @template TV
202
- * @template TR
203
- * @param [options] - Optional constructor options for the like-kind instance.
204
- * @returns An empty like-kind instance.
205
- */
206
- protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeCounterOptions<TK, TV, TR>>): this;
207
- /**
208
- * (Protected) Create a like-kind instance and seed it from an iterable.
209
- * @remarks Time O(N log N), Space O(N)
210
- * @template TK
211
- * @template TV
212
- * @template TR
213
- * @param iter - Iterable used to seed the new tree.
214
- * @param [options] - Options merged with the current snapshot.
215
- * @returns A like-kind TreeCounter built from the iterable.
216
- */
217
- protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<TreeCounterOptions<TK, TV, TR>>): TreeCounter<TK, TV, TR>;
218
- /**
219
- * (Protected) Normalize input into a node plus its effective value and count.
220
- * @remarks Time O(1), Space O(1)
221
- * @param keyNodeOrEntry - Key, node, or [key, value] entry.
222
- * @param [value] - Value used when a bare key is provided.
223
- * @param [count] - Count increment to apply (default 1).
224
- * @returns Tuple [node, value] where node may be undefined.
225
- */
226
- protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): [TreeCounterNode<K, V> | undefined, V | undefined];
227
- /**
228
- * (Protected) Swap keys/values/counters between the source and destination nodes.
229
- * @remarks Time O(1), Space O(1)
230
- * @param srcNode - Source node (or key) whose properties will be moved.
231
- * @param destNode - Destination node (or key) to receive properties.
232
- * @returns Destination node after swap, or undefined.
233
- */
234
- protected _swapProperties(srcNode: BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>, destNode: BSTNOptKeyOrNode<K, TreeCounterNode<K, V>>): TreeCounterNode<K, V> | undefined;
235
- /**
236
- * (Protected) Replace one node by another and adjust counters accordingly.
237
- * @remarks Time O(1), Space O(1)
238
- * @param oldNode - Node being replaced.
239
- * @param newNode - Replacement node.
240
- * @returns The new node after replacement.
241
- */
242
- protected _replaceNode(oldNode: TreeCounterNode<K, V>, newNode: TreeCounterNode<K, V>): TreeCounterNode<K, V>;
243
- }
@@ -1,2 +0,0 @@
1
- import { AVLTreeOptions } from './avl-tree';
2
- export type AVLTreeCounterOptions<K, V, R> = AVLTreeOptions<K, V, R> & {};
@@ -1,2 +0,0 @@
1
- import type { AVLTreeOptions } from './avl-tree';
2
- export type AVLTreeMultiMapOptions<K, V, R> = AVLTreeOptions<K, V, R> & {};
@@ -1,2 +0,0 @@
1
- import type { RedBlackTreeOptions } from './red-black-tree';
2
- export type TreeCounterOptions<K, V, R> = RedBlackTreeOptions<K, V, R> & {};