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.
- package/dist/types/data-structures/base/linear-base.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +150 -20
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +188 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +238 -147
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +270 -0
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +181 -0
- package/dist/types/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/index.d.ts +3 -3
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +33 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +16 -0
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +33 -0
- package/package.json +2 -2
- package/src/data-structures/base/linear-base.ts +2 -12
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +45 -21
- package/src/data-structures/binary-tree/bst.ts +85 -10
- package/src/data-structures/binary-tree/index.ts +3 -3
- package/src/data-structures/binary-tree/red-black-tree.ts +568 -76
- package/src/data-structures/binary-tree/tree-map.ts +439 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +488 -325
- package/src/data-structures/binary-tree/tree-multi-set.ts +502 -0
- package/src/data-structures/binary-tree/tree-set.ts +407 -0
- package/src/data-structures/queue/deque.ts +10 -0
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/index.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-map.ts +45 -0
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +19 -0
- package/src/types/data-structures/binary-tree/tree-set.ts +39 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -236
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -197
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +0 -243
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +0 -2
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -2
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +0 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +0 -539
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +0 -438
- package/src/data-structures/binary-tree/tree-counter.ts +0 -575
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +0 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +0 -3
- package/src/types/data-structures/binary-tree/tree-counter.ts +0 -3
|
@@ -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).
|
|
@@ -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';
|
|
@@ -291,14 +291,14 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
291
291
|
* @returns True if duplicates are allowed, false otherwise.
|
|
292
292
|
*/
|
|
293
293
|
get isDuplicate(): boolean;
|
|
294
|
-
protected _store: Map<K, V
|
|
294
|
+
protected _store: Map<K, BinaryTreeNode<K, V>>;
|
|
295
295
|
/**
|
|
296
296
|
* Gets the external value store (used in Map mode).
|
|
297
297
|
* @remarks Time O(1)
|
|
298
298
|
*
|
|
299
299
|
* @returns The map storing key-value pairs.
|
|
300
300
|
*/
|
|
301
|
-
get store(): Map<K, V
|
|
301
|
+
get store(): Map<K, BinaryTreeNode<K, V>>;
|
|
302
302
|
protected _root?: BinaryTreeNode<K, V> | null | undefined;
|
|
303
303
|
/**
|
|
304
304
|
* Gets the root node of the tree.
|
|
@@ -482,10 +482,10 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
482
482
|
* Deletes a node from the tree.
|
|
483
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).
|
|
484
484
|
*
|
|
485
|
-
* @param
|
|
485
|
+
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
486
486
|
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
487
487
|
*/
|
|
488
|
-
delete(
|
|
488
|
+
delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
|
|
489
489
|
search(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne?: boolean): (K | undefined)[];
|
|
490
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>[];
|
|
491
491
|
/**
|
|
@@ -792,7 +792,7 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
792
792
|
* @param value - The value.
|
|
793
793
|
* @returns True if successful.
|
|
794
794
|
*/
|
|
795
|
-
protected _setValue(key: K | null | undefined, value: V | undefined):
|
|
795
|
+
protected _setValue(key: K | null | undefined, value: V | undefined): boolean;
|
|
796
796
|
/**
|
|
797
797
|
* (Protected) Clears all nodes from the tree.
|
|
798
798
|
* @remarks Time O(1)
|
|
@@ -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_____
|
|
@@ -4,7 +4,7 @@ export * from './binary-indexed-tree';
|
|
|
4
4
|
export * from './segment-tree';
|
|
5
5
|
export * from './avl-tree';
|
|
6
6
|
export * from './red-black-tree';
|
|
7
|
-
export * from './avl-tree-multi-map';
|
|
8
7
|
export * from './tree-multi-map';
|
|
9
|
-
export * from './tree-
|
|
10
|
-
export * from './
|
|
8
|
+
export * from './tree-set';
|
|
9
|
+
export * from './tree-map';
|
|
10
|
+
export * from './tree-multi-set';
|
|
@@ -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)
|
|
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
|
|
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
|
*/
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TreeMap (ordered map) — a restricted, native-like API backed by RedBlackTree.
|
|
3
|
+
*
|
|
4
|
+
* Design goals:
|
|
5
|
+
* - No node exposure (no node inputs/outputs)
|
|
6
|
+
* - Native Map-like surface + Java NavigableMap-like helpers
|
|
7
|
+
* - Strict default comparator (number/string/Date), otherwise require comparator
|
|
8
|
+
*/
|
|
9
|
+
import type { Comparator } from '../../types';
|
|
10
|
+
import type { TreeMapEntryCallback, TreeMapOptions, TreeMapRangeOptions, TreeMapReduceCallback } from '../../types';
|
|
11
|
+
/**
|
|
12
|
+
* An ordered Map backed by a red-black tree.
|
|
13
|
+
*
|
|
14
|
+
* - Iteration order is ascending by key.
|
|
15
|
+
* - No node exposure: all APIs use keys/values only.
|
|
16
|
+
*/
|
|
17
|
+
export declare class TreeMap<K = any, V = any, R = [K, V]> implements Iterable<[K, V | undefined]> {
|
|
18
|
+
#private;
|
|
19
|
+
/**
|
|
20
|
+
* Create a TreeMap from an iterable of `[key, value]` entries or raw elements.
|
|
21
|
+
*
|
|
22
|
+
* @param entries - Iterable of `[key, value]` tuples, or raw elements if `toEntryFn` is provided.
|
|
23
|
+
* @param options - Configuration options including optional `toEntryFn` to transform raw elements.
|
|
24
|
+
* @throws {TypeError} If any entry is not a 2-tuple-like value (when no toEntryFn), or when using
|
|
25
|
+
* the default comparator and encountering unsupported/invalid keys (e.g. `NaN`, invalid `Date`).
|
|
26
|
+
* @example
|
|
27
|
+
* // Standard usage with entries
|
|
28
|
+
* const map = new TreeMap([['a', 1], ['b', 2]]);
|
|
29
|
+
*
|
|
30
|
+
* // Using toEntryFn to transform raw objects
|
|
31
|
+
* const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
32
|
+
* const map = new TreeMap<number, User, User>(users, { toEntryFn: u => [u.id, u] });
|
|
33
|
+
*/
|
|
34
|
+
constructor(entries?: Iterable<R> | Iterable<[K, V | undefined]>, options?: TreeMapOptions<K, V, R>);
|
|
35
|
+
/**
|
|
36
|
+
* Create the strict default comparator.
|
|
37
|
+
*
|
|
38
|
+
* Supports:
|
|
39
|
+
* - `number` (rejects `NaN`; treats `-0` and `0` as equal)
|
|
40
|
+
* - `string`
|
|
41
|
+
* - `Date` (orders by `getTime()`, rejects invalid dates)
|
|
42
|
+
*
|
|
43
|
+
* For other key types, a custom comparator must be provided.
|
|
44
|
+
*/
|
|
45
|
+
static createDefaultComparator<K>(): Comparator<K>;
|
|
46
|
+
private _validateKey;
|
|
47
|
+
/**
|
|
48
|
+
* Number of entries in the map.
|
|
49
|
+
*/
|
|
50
|
+
get size(): number;
|
|
51
|
+
/**
|
|
52
|
+
* Whether the map is empty.
|
|
53
|
+
*/
|
|
54
|
+
isEmpty(): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Set or overwrite a value for a key.
|
|
57
|
+
* @remarks Expected time O(log n)
|
|
58
|
+
*/
|
|
59
|
+
set(key: K, value: V | undefined): this;
|
|
60
|
+
/**
|
|
61
|
+
* Get the value under a key.
|
|
62
|
+
* @remarks Expected time O(log n)
|
|
63
|
+
*/
|
|
64
|
+
get(key: K): V | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Test whether a key exists.
|
|
67
|
+
* @remarks Expected time O(log n)
|
|
68
|
+
*/
|
|
69
|
+
has(key: K): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Delete a key.
|
|
72
|
+
* @returns `true` if the key existed; otherwise `false`.
|
|
73
|
+
* @remarks Expected time O(log n)
|
|
74
|
+
*/
|
|
75
|
+
delete(key: K): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Remove all entries.
|
|
78
|
+
*/
|
|
79
|
+
clear(): void;
|
|
80
|
+
/**
|
|
81
|
+
* Iterate over keys in ascending order.
|
|
82
|
+
*/
|
|
83
|
+
keys(): IterableIterator<K>;
|
|
84
|
+
private _entryFromKey;
|
|
85
|
+
/**
|
|
86
|
+
* Iterate over values in ascending key order.
|
|
87
|
+
*
|
|
88
|
+
* Note: values may be `undefined` (TreeMap allows storing `undefined`, like native `Map`).
|
|
89
|
+
*/
|
|
90
|
+
values(): IterableIterator<V | undefined>;
|
|
91
|
+
/**
|
|
92
|
+
* Iterate over `[key, value]` entries in ascending key order.
|
|
93
|
+
*
|
|
94
|
+
* Note: values may be `undefined`.
|
|
95
|
+
*/
|
|
96
|
+
entries(): IterableIterator<[K, V | undefined]>;
|
|
97
|
+
[Symbol.iterator](): IterableIterator<[K, V | undefined]>;
|
|
98
|
+
/**
|
|
99
|
+
* Visit each entry in ascending key order.
|
|
100
|
+
*
|
|
101
|
+
* Note: callback value may be `undefined`.
|
|
102
|
+
*/
|
|
103
|
+
forEach(cb: (value: V | undefined, key: K, map: TreeMap<K, V>) => void, thisArg?: any): void;
|
|
104
|
+
/**
|
|
105
|
+
* Create a new TreeMap by mapping each entry to a new `[key, value]` entry.
|
|
106
|
+
*
|
|
107
|
+
* This mirrors `RedBlackTree.map`: mapping produces a new ordered container.
|
|
108
|
+
* @remarks Time O(n log n) expected, Space O(n)
|
|
109
|
+
*/
|
|
110
|
+
map<MK, MV>(callbackfn: TreeMapEntryCallback<K, V, [MK, MV], TreeMap<K, V>>, options?: Omit<TreeMapOptions<MK, MV>, 'toEntryFn'> & {
|
|
111
|
+
comparator?: (a: MK, b: MK) => number;
|
|
112
|
+
}, thisArg?: unknown): TreeMap<MK, MV>;
|
|
113
|
+
/**
|
|
114
|
+
* Create a new TreeMap containing only entries that satisfy the predicate.
|
|
115
|
+
* @remarks Time O(n log n) expected, Space O(n)
|
|
116
|
+
*/
|
|
117
|
+
filter(callbackfn: TreeMapEntryCallback<K, V, boolean, TreeMap<K, V>>, thisArg?: unknown): TreeMap<K, V>;
|
|
118
|
+
/**
|
|
119
|
+
* Reduce entries into a single accumulator.
|
|
120
|
+
* @remarks Time O(n), Space O(1)
|
|
121
|
+
*/
|
|
122
|
+
reduce<A>(callbackfn: TreeMapReduceCallback<K, V, A, TreeMap<K, V>>, initialValue: A): A;
|
|
123
|
+
/**
|
|
124
|
+
* Test whether all entries satisfy a predicate.
|
|
125
|
+
* @remarks Time O(n), Space O(1)
|
|
126
|
+
*/
|
|
127
|
+
every(callbackfn: TreeMapEntryCallback<K, V, boolean, TreeMap<K, V>>, thisArg?: unknown): boolean;
|
|
128
|
+
/**
|
|
129
|
+
* Test whether any entry satisfies a predicate.
|
|
130
|
+
* @remarks Time O(n), Space O(1)
|
|
131
|
+
*/
|
|
132
|
+
some(callbackfn: TreeMapEntryCallback<K, V, boolean, TreeMap<K, V>>, thisArg?: unknown): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Find the first entry that satisfies a predicate.
|
|
135
|
+
* @returns The first matching `[key, value]` tuple, or `undefined`.
|
|
136
|
+
* @remarks Time O(n), Space O(1)
|
|
137
|
+
*/
|
|
138
|
+
find(callbackfn: TreeMapEntryCallback<K, V, boolean, TreeMap<K, V>>, thisArg?: unknown): [K, V | undefined] | undefined;
|
|
139
|
+
/**
|
|
140
|
+
* Materialize the map into an array of `[key, value]` tuples.
|
|
141
|
+
* @remarks Time O(n), Space O(n)
|
|
142
|
+
*/
|
|
143
|
+
toArray(): Array<[K, V | undefined]>;
|
|
144
|
+
/**
|
|
145
|
+
* Print a human-friendly representation.
|
|
146
|
+
* @remarks Time O(n), Space O(n)
|
|
147
|
+
*/
|
|
148
|
+
print(): void;
|
|
149
|
+
/**
|
|
150
|
+
* Smallest entry by key.
|
|
151
|
+
*/
|
|
152
|
+
first(): [K, V | undefined] | undefined;
|
|
153
|
+
/**
|
|
154
|
+
* Largest entry by key.
|
|
155
|
+
*/
|
|
156
|
+
last(): [K, V | undefined] | undefined;
|
|
157
|
+
/**
|
|
158
|
+
* Remove and return the smallest entry.
|
|
159
|
+
*/
|
|
160
|
+
pollFirst(): [K, V | undefined] | undefined;
|
|
161
|
+
/**
|
|
162
|
+
* Remove and return the largest entry.
|
|
163
|
+
*/
|
|
164
|
+
pollLast(): [K, V | undefined] | undefined;
|
|
165
|
+
/**
|
|
166
|
+
* Smallest entry whose key is >= the given key.
|
|
167
|
+
*/
|
|
168
|
+
ceiling(key: K): [K, V | undefined] | undefined;
|
|
169
|
+
/**
|
|
170
|
+
* Largest entry whose key is <= the given key.
|
|
171
|
+
*/
|
|
172
|
+
floor(key: K): [K, V | undefined] | undefined;
|
|
173
|
+
/**
|
|
174
|
+
* Smallest entry whose key is > the given key.
|
|
175
|
+
*/
|
|
176
|
+
higher(key: K): [K, V | undefined] | undefined;
|
|
177
|
+
/**
|
|
178
|
+
* Largest entry whose key is < the given key.
|
|
179
|
+
*/
|
|
180
|
+
lower(key: K): [K, V | undefined] | undefined;
|
|
181
|
+
/**
|
|
182
|
+
* Return all entries in a given key range.
|
|
183
|
+
*
|
|
184
|
+
* @param range `[low, high]`
|
|
185
|
+
* @param options Inclusive/exclusive bounds (defaults to inclusive).
|
|
186
|
+
*/
|
|
187
|
+
rangeSearch(range: [K, K], options?: TreeMapRangeOptions): Array<[K, V | undefined]>;
|
|
188
|
+
}
|