min-priority-queue-typed 2.2.3 → 2.2.6
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/cjs/index.cjs +0 -36
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +0 -36
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +0 -36
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +0 -36
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +14 -57
- package/dist/types/data-structures/binary-tree/bst.d.ts +151 -117
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/max-priority-queue-typed.js +0 -33
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/common/index.ts +2 -4
- package/src/data-structures/base/iterable-entry-base.ts +9 -0
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -8
- package/src/data-structures/binary-tree/avl-tree.ts +4 -5
- package/src/data-structures/binary-tree/binary-tree.ts +67 -0
- package/src/data-structures/binary-tree/bst.ts +724 -108
- package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
|
@@ -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 {
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparator, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodePredicate, OptNode, RBTNColor } from '../../types';
|
|
9
9
|
import { BinaryTree } from './binary-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { Range } from '../../common';
|
|
@@ -257,7 +257,7 @@ export declare class BSTNode<K = any, V = any> {
|
|
|
257
257
|
* return findFirstCommon(path1, path2);
|
|
258
258
|
* };
|
|
259
259
|
*
|
|
260
|
-
* function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
|
|
260
|
+
* function findFirstCommon(arr1: (number | undefined)[], arr2: (number | undefined)[]): number | undefined {
|
|
261
261
|
* for (const num of arr1) {
|
|
262
262
|
* if (arr2.indexOf(num) !== -1) {
|
|
263
263
|
* return num;
|
|
@@ -279,7 +279,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
279
279
|
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
|
|
280
280
|
* @param [options] - Configuration options for the BST, including comparator.
|
|
281
281
|
*/
|
|
282
|
-
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode
|
|
282
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BSTOptions<K, V, R>);
|
|
283
283
|
protected _root?: BSTNode<K, V>;
|
|
284
284
|
/**
|
|
285
285
|
* Gets the root node of the tree.
|
|
@@ -288,17 +288,10 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
288
288
|
* @returns The root node.
|
|
289
289
|
*/
|
|
290
290
|
get root(): OptNode<BSTNode<K, V>>;
|
|
291
|
-
protected _isReverse: boolean;
|
|
292
291
|
/**
|
|
293
|
-
*
|
|
294
|
-
|
|
295
|
-
*
|
|
296
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
297
|
-
*/
|
|
298
|
-
get isReverse(): boolean;
|
|
299
|
-
/**
|
|
300
|
-
* The default comparator function.
|
|
301
|
-
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
292
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
293
|
+
|
|
294
|
+
* @remarks Time O(1) Space O(1)
|
|
302
295
|
*/
|
|
303
296
|
protected _comparator: Comparator<K>;
|
|
304
297
|
/**
|
|
@@ -308,14 +301,6 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
308
301
|
* @returns The comparator function.
|
|
309
302
|
*/
|
|
310
303
|
get comparator(): Comparator<K>;
|
|
311
|
-
protected _specifyComparable?: (key: K) => Comparable;
|
|
312
|
-
/**
|
|
313
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
314
|
-
* @remarks Time O(1)
|
|
315
|
-
*
|
|
316
|
-
* @returns The key-to-comparable conversion function.
|
|
317
|
-
*/
|
|
318
|
-
get specifyComparable(): ((key: K) => Comparable) | undefined;
|
|
319
304
|
/**
|
|
320
305
|
* (Protected) Creates a new BST node.
|
|
321
306
|
* @remarks Time O(1), Space O(1)
|
|
@@ -350,41 +335,12 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
350
335
|
* @returns True if the key is valid, false otherwise.
|
|
351
336
|
*/
|
|
352
337
|
isValidKey(key: any): key is K;
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
* @param [pattern='IN'] - The traversal order ('IN', 'PRE', 'POST').
|
|
360
|
-
* @param [onlyOne=false] - If true, stops after the first callback.
|
|
361
|
-
* @param [startNode=this._root] - The node to start from.
|
|
362
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
363
|
-
* @returns An array of callback results.
|
|
364
|
-
*/
|
|
365
|
-
dfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
366
|
-
/**
|
|
367
|
-
* Performs a Breadth-First Search (BFS) or Level-Order traversal.
|
|
368
|
-
* @remarks Time O(N), visits every node. Space O(N) in the worst case for the queue.
|
|
369
|
-
*
|
|
370
|
-
* @template C - The type of the callback function.
|
|
371
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
372
|
-
* @param [startNode=this._root] - The node to start from.
|
|
373
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
374
|
-
* @returns An array of callback results.
|
|
375
|
-
*/
|
|
376
|
-
bfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
377
|
-
/**
|
|
378
|
-
* Returns a 2D array of nodes, grouped by level.
|
|
379
|
-
* @remarks Time O(N), visits every node. Space O(N) for the result array and the queue/stack.
|
|
380
|
-
*
|
|
381
|
-
* @template C - The type of the callback function.
|
|
382
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
383
|
-
* @param [startNode=this._root] - The node to start from.
|
|
384
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
385
|
-
* @returns A 2D array of callback results.
|
|
386
|
-
*/
|
|
387
|
-
listLevels<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[][];
|
|
338
|
+
dfs(): (K | undefined)[];
|
|
339
|
+
dfs<C extends NodeCallback<BSTNode<K, V>>>(callback: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
340
|
+
bfs(): (K | undefined)[];
|
|
341
|
+
bfs<C extends NodeCallback<BSTNode<K, V>>>(callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
342
|
+
listLevels(): (K | undefined)[][];
|
|
343
|
+
listLevels<C extends NodeCallback<BSTNode<K, V>>>(callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[][];
|
|
388
344
|
/**
|
|
389
345
|
* Gets the first node matching a predicate.
|
|
390
346
|
* @remarks Time O(log N) if searching by key, O(N) if searching by predicate. Space O(log N) or O(N).
|
|
@@ -395,33 +351,10 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
395
351
|
* @returns The first matching node, or undefined if not found.
|
|
396
352
|
*/
|
|
397
353
|
getNode(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, startNode?: BSTNOptKeyOrNode<K, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>;
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
* Space O(log N) for the stack.
|
|
403
|
-
*
|
|
404
|
-
* @template C - The type of the callback function.
|
|
405
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, predicate, or range to search for.
|
|
406
|
-
* @param [onlyOne=false] - If true, stops after finding the first match.
|
|
407
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
408
|
-
* @param [startNode=this._root] - The node to start the search from.
|
|
409
|
-
* @param [iterationType=this.iterationType] - Whether to use 'RECURSIVE' or 'ITERATIVE' search.
|
|
410
|
-
* @returns An array of results from the callback function for each matching node.
|
|
411
|
-
*/
|
|
412
|
-
search<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
413
|
-
/**
|
|
414
|
-
* Performs an optimized search for nodes within a given key range.
|
|
415
|
-
* @remarks Time O(H + M), where H is tree height and M is the number of matches.
|
|
416
|
-
*
|
|
417
|
-
* @template C - The type of the callback function.
|
|
418
|
-
* @param range - A `Range` object or a `[low, high]` tuple.
|
|
419
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
420
|
-
* @param [startNode=this._root] - The node to start the search from.
|
|
421
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
422
|
-
* @returns An array of callback results.
|
|
423
|
-
*/
|
|
424
|
-
rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(range: Range<K> | [K, K], callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
354
|
+
search(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean): (K | undefined)[];
|
|
355
|
+
search<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne: boolean, callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
356
|
+
rangeSearch(range: Range<K> | [K, K]): (K | undefined)[];
|
|
357
|
+
rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(range: Range<K> | [K, K], callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
425
358
|
/**
|
|
426
359
|
* Adds a new node to the BST based on key comparison.
|
|
427
360
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
@@ -445,39 +378,59 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
445
378
|
*/
|
|
446
379
|
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
447
380
|
/**
|
|
448
|
-
* Returns the first
|
|
449
|
-
*
|
|
450
|
-
*
|
|
451
|
-
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
381
|
+
* Returns the first key with a value >= target.
|
|
382
|
+
* Equivalent to Java TreeMap.ceiling.
|
|
383
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
452
384
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
453
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
454
|
-
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
455
|
-
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
456
385
|
*/
|
|
457
|
-
|
|
386
|
+
ceiling(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
458
387
|
/**
|
|
459
|
-
* Returns the first node with a key
|
|
460
|
-
*
|
|
461
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
462
|
-
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
388
|
+
* Returns the first node with a key >= target and applies callback.
|
|
389
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
463
390
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
464
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
465
|
-
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
466
|
-
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
467
391
|
*/
|
|
468
|
-
|
|
392
|
+
ceiling<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, callback: C, iterationType?: IterationType): ReturnType<C>;
|
|
469
393
|
/**
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
*
|
|
473
|
-
*
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
*
|
|
478
|
-
*
|
|
394
|
+
* Returns the first key with a value > target.
|
|
395
|
+
* Equivalent to Java TreeMap.higher.
|
|
396
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
397
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
398
|
+
*/
|
|
399
|
+
higher(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
400
|
+
/**
|
|
401
|
+
* Returns the first node with a key > target and applies callback.
|
|
402
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
403
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
479
404
|
*/
|
|
480
|
-
|
|
405
|
+
higher<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, callback: C, iterationType?: IterationType): ReturnType<C>;
|
|
406
|
+
/**
|
|
407
|
+
* Returns the first key with a value <= target.
|
|
408
|
+
* Equivalent to Java TreeMap.floor.
|
|
409
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
410
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
411
|
+
*/
|
|
412
|
+
floor(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
413
|
+
/**
|
|
414
|
+
* Returns the first node with a key <= target and applies callback.
|
|
415
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
416
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
417
|
+
*/
|
|
418
|
+
floor<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, callback: C, iterationType?: IterationType): ReturnType<C>;
|
|
419
|
+
/**
|
|
420
|
+
* Returns the first key with a value < target.
|
|
421
|
+
* Equivalent to Java TreeMap.lower.
|
|
422
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
423
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
424
|
+
*/
|
|
425
|
+
lower(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
426
|
+
/**
|
|
427
|
+
* Returns the first node with a key < target and applies callback.
|
|
428
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
429
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
430
|
+
*/
|
|
431
|
+
lower<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, callback: C, iterationType?: IterationType): ReturnType<C>;
|
|
432
|
+
lesserOrGreaterTraverse(): (K | undefined)[];
|
|
433
|
+
lesserOrGreaterTraverse<C extends NodeCallback<BSTNode<K, V>>>(callback: C, lesserOrGreater?: number, targetNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
481
434
|
/**
|
|
482
435
|
* Rebuilds the tree to be perfectly balanced.
|
|
483
436
|
* @remarks Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
|
|
@@ -507,15 +460,96 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
507
460
|
* @param [thisArg] - `this` context for the callback.
|
|
508
461
|
* @returns A new, mapped BST.
|
|
509
462
|
*/
|
|
510
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
463
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BSTOptions<MK, MV, MR>>, thisArg?: unknown): BST<MK, MV, MR>;
|
|
464
|
+
/**
|
|
465
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
466
|
+
*
|
|
467
|
+
* @remarks
|
|
468
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
469
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
470
|
+
*
|
|
471
|
+
* @template K - The key type.
|
|
472
|
+
* @template V - The value type.
|
|
473
|
+
*
|
|
474
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
475
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
476
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
477
|
+
* - An entry tuple: searches for the key-value pair.
|
|
478
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
479
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
480
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
481
|
+
*
|
|
482
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
483
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
484
|
+
*
|
|
485
|
+
* @param startNode - The node to start the search from. Can be:
|
|
486
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
487
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
488
|
+
* - Default value: this._root (the tree's root).
|
|
489
|
+
*
|
|
490
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
491
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
492
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
493
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
494
|
+
*
|
|
495
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
496
|
+
* - Key: the matched node's key.
|
|
497
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
498
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
499
|
+
*/
|
|
500
|
+
deleteWhere(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeDeleteResult<BSTNode<K, V>>[];
|
|
501
|
+
/**
|
|
502
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
503
|
+
* @remarks Time O(1) Space O(1)
|
|
504
|
+
* @returns The default comparator function.
|
|
505
|
+
*/
|
|
506
|
+
protected _createDefaultComparator(): Comparator<K>;
|
|
507
|
+
/**
|
|
508
|
+
* (Protected) Binary search for floor by key with pruning optimization.
|
|
509
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
510
|
+
* Finds first node where key <= target.
|
|
511
|
+
* @remarks Time O(h) where h is tree height.
|
|
512
|
+
*
|
|
513
|
+
* @param key - The target key to search for.
|
|
514
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
515
|
+
* @returns The first node with key <= target, or undefined if none exists.
|
|
516
|
+
*/
|
|
517
|
+
protected _floorByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
518
|
+
/**
|
|
519
|
+
* (Protected) In-order traversal search for floor by predicate.
|
|
520
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
521
|
+
* Returns the last node that satisfies the predicate function.
|
|
522
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
523
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
524
|
+
*
|
|
525
|
+
* @param predicate - The predicate function to test nodes.
|
|
526
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
527
|
+
* @returns The last node satisfying predicate (highest key), or undefined if none found.
|
|
528
|
+
*/
|
|
529
|
+
protected _floorByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
530
|
+
/**
|
|
531
|
+
* (Protected) Binary search for lower by key with pruning optimization.
|
|
532
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
533
|
+
* Finds first node where key < target.
|
|
534
|
+
* @remarks Time O(h) where h is tree height.
|
|
535
|
+
*
|
|
536
|
+
* @param key - The target key to search for.
|
|
537
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
538
|
+
* @returns The first node with key < target, or undefined if none exists.
|
|
539
|
+
*/
|
|
540
|
+
protected _lowerByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
511
541
|
/**
|
|
512
|
-
*
|
|
513
|
-
*
|
|
542
|
+
* (Protected) In-order traversal search for lower by predicate.
|
|
543
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
544
|
+
* Returns the node that satisfies the predicate and appears last in in-order traversal.
|
|
545
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
546
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
514
547
|
*
|
|
515
|
-
* @param predicate -
|
|
516
|
-
* @
|
|
548
|
+
* @param predicate - The predicate function to test nodes.
|
|
549
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
550
|
+
* @returns The last node satisfying predicate (highest key < target), or undefined if none found.
|
|
517
551
|
*/
|
|
518
|
-
|
|
552
|
+
protected _lowerByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
519
553
|
/**
|
|
520
554
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
521
555
|
* Unified logic for both lowerBound and upperBound.
|
|
@@ -593,7 +627,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
593
627
|
protected _setRoot(v: OptNode<BSTNode<K, V>>): void;
|
|
594
628
|
/**
|
|
595
629
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
596
|
-
* @remarks Time O(1)
|
|
630
|
+
* @remarks Time O(1) Space O(1)
|
|
597
631
|
*
|
|
598
632
|
* @param a - The first key.
|
|
599
633
|
* @param b - The second key.
|
|
@@ -607,5 +641,5 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
607
641
|
* @param key - The key of the node to delete.
|
|
608
642
|
* @returns True if the node was found and deleted, false otherwise.
|
|
609
643
|
*/
|
|
610
|
-
|
|
644
|
+
protected _deleteByKey(key: K): boolean;
|
|
611
645
|
}
|
|
@@ -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,
|
|
8
|
+
import type { BinaryTreeDeleteResult, CRUD, EntryCallback, FamilyPosition, 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> {
|
|
@@ -261,7 +261,7 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
|
|
|
261
261
|
* @param [thisArg] - See parameter type for details.
|
|
262
262
|
* @returns A new RedBlackTree with mapped entries.
|
|
263
263
|
*/
|
|
264
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
264
|
+
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>;
|
|
265
265
|
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this;
|
|
266
266
|
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>;
|
|
267
267
|
protected _setRoot(v: RedBlackTreeNode<K, V> | undefined): void;
|
|
@@ -5,8 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult,
|
|
9
|
-
import { BSTOptions } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor, TreeCounterOptions } from '../../types';
|
|
10
9
|
import { BSTNode } from './bst';
|
|
11
10
|
import { IBinaryTree } from '../../interfaces';
|
|
12
11
|
import { RedBlackTree } from './red-black-tree';
|
|
@@ -188,7 +187,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
188
187
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
189
188
|
* @returns A new TreeCounter with mapped entries.
|
|
190
189
|
*/
|
|
191
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
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>;
|
|
192
191
|
/**
|
|
193
192
|
* Deep copy this tree, preserving map mode and aggregate counts.
|
|
194
193
|
* @remarks Time O(N), Space O(N)
|
|
@@ -204,7 +203,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
204
203
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
205
204
|
* @returns An empty like-kind instance.
|
|
206
205
|
*/
|
|
207
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
206
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeCounterOptions<TK, TV, TR>>): this;
|
|
208
207
|
/**
|
|
209
208
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
210
209
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -215,7 +214,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
215
214
|
* @param [options] - Options merged with the current snapshot.
|
|
216
215
|
* @returns A like-kind TreeCounter built from the iterable.
|
|
217
216
|
*/
|
|
218
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
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>;
|
|
219
218
|
/**
|
|
220
219
|
* (Protected) Normalize input into a node plus its effective value and count.
|
|
221
220
|
* @remarks Time O(1), Space O(1)
|
|
@@ -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 { ElemOf, EntryCallback, FamilyPosition, RBTNColor,
|
|
8
|
+
import type { ElemOf, EntryCallback, FamilyPosition, RBTNColor, TreeMultiMapOptions } from '../../types';
|
|
9
9
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
@@ -307,8 +307,8 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
307
307
|
* @returns True if the value was removed; false if not found.
|
|
308
308
|
*/
|
|
309
309
|
deleteValue(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, value: V): boolean;
|
|
310
|
-
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<
|
|
311
|
-
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<
|
|
310
|
+
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<TreeMultiMapOptions<MK, MVArr, MR>>, thisArg?: unknown): TreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
311
|
+
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<TreeMultiMapOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
312
312
|
/**
|
|
313
313
|
* (Protected) Create an empty instance of the same concrete class.
|
|
314
314
|
* @remarks Time O(1), Space O(1)
|
|
@@ -318,7 +318,7 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
318
318
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
319
319
|
* @returns An empty like-kind instance.
|
|
320
320
|
*/
|
|
321
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
321
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeMultiMapOptions<TK, TV, TR>>): this;
|
|
322
322
|
/**
|
|
323
323
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
324
324
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -329,5 +329,5 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
329
329
|
* @param [options] - Options merged with the current snapshot.
|
|
330
330
|
* @returns A like-kind RedBlackTree built from the iterable.
|
|
331
331
|
*/
|
|
332
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
332
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<TreeMultiMapOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
|
|
333
333
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { BinaryTreeOptions } from './binary-tree';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export type BSTOptions<K, V, R> =
|
|
5
|
-
|
|
6
|
-
isReverse?: boolean;
|
|
2
|
+
import type { Comparator, OptValue } from '../../common';
|
|
3
|
+
type BSTBaseOptions<K, V, R> = Omit<BinaryTreeOptions<K, V, R>, 'isDuplicate'>;
|
|
4
|
+
export type BSTOptions<K, V, R> = BSTBaseOptions<K, V, R> & {
|
|
5
|
+
comparator?: Comparator<K>;
|
|
7
6
|
};
|
|
8
7
|
export type BSTNOptKey<K> = K | undefined;
|
|
9
8
|
export type OptNode<NODE> = NODE | undefined;
|
|
10
9
|
export type BSTNEntry<K, V> = [BSTNOptKey<K>, OptValue<V>];
|
|
11
10
|
export type BSTNOptKeyOrNode<K, NODE> = BSTNOptKey<K> | NODE;
|
|
12
11
|
export type BSTNRep<K, V, NODE> = BSTNEntry<K, V> | BSTNOptKeyOrNode<K, NODE>;
|
|
12
|
+
export {};
|
|
@@ -929,37 +929,6 @@ var minPriorityQueueTyped = (() => {
|
|
|
929
929
|
}
|
|
930
930
|
};
|
|
931
931
|
|
|
932
|
-
// src/utils/utils.ts
|
|
933
|
-
function isPrimitiveComparable(value) {
|
|
934
|
-
const valueType = typeof value;
|
|
935
|
-
if (valueType === "number") return true;
|
|
936
|
-
return valueType === "bigint" || valueType === "string" || valueType === "boolean";
|
|
937
|
-
}
|
|
938
|
-
function tryObjectToPrimitive(obj) {
|
|
939
|
-
if (typeof obj.valueOf === "function") {
|
|
940
|
-
const valueOfResult = obj.valueOf();
|
|
941
|
-
if (valueOfResult !== obj) {
|
|
942
|
-
if (isPrimitiveComparable(valueOfResult)) return valueOfResult;
|
|
943
|
-
if (typeof valueOfResult === "object" && valueOfResult !== null) return tryObjectToPrimitive(valueOfResult);
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
if (typeof obj.toString === "function") {
|
|
947
|
-
const stringResult = obj.toString();
|
|
948
|
-
if (stringResult !== "[object Object]") return stringResult;
|
|
949
|
-
}
|
|
950
|
-
return null;
|
|
951
|
-
}
|
|
952
|
-
function isComparable(value, isForceObjectComparable = false) {
|
|
953
|
-
if (value === null || value === void 0) return false;
|
|
954
|
-
if (isPrimitiveComparable(value)) return true;
|
|
955
|
-
if (typeof value !== "object") return false;
|
|
956
|
-
if (value instanceof Date) return true;
|
|
957
|
-
if (isForceObjectComparable) return true;
|
|
958
|
-
const comparableValue = tryObjectToPrimitive(value);
|
|
959
|
-
if (comparableValue === null || comparableValue === void 0) return false;
|
|
960
|
-
return isPrimitiveComparable(comparableValue);
|
|
961
|
-
}
|
|
962
|
-
|
|
963
932
|
// src/common/index.ts
|
|
964
933
|
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
965
934
|
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
@@ -972,8 +941,6 @@ var minPriorityQueueTyped = (() => {
|
|
|
972
941
|
this.high = high;
|
|
973
942
|
this.includeLow = includeLow;
|
|
974
943
|
this.includeHigh = includeHigh;
|
|
975
|
-
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
976
|
-
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
977
944
|
}
|
|
978
945
|
// Determine whether a key is within the range
|
|
979
946
|
isInRange(key, comparator) {
|