binary-tree-typed 2.2.4 → 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 +8 -2
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +8 -2
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +8 -2
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +8 -2
- 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/binary-tree.d.ts +14 -57
- package/dist/types/data-structures/binary-tree/bst.d.ts +110 -96
- package/dist/umd/binary-tree-typed.js +8 -2
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +3 -3
- package/dist/umd/binary-tree-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-multi-map.ts +3 -1
- package/src/data-structures/binary-tree/binary-tree.ts +67 -0
- package/src/data-structures/binary-tree/bst.ts +658 -71
|
@@ -96,6 +96,12 @@ export declare abstract class IterableEntryBase<K = any, V = any> {
|
|
|
96
96
|
* @remarks Time O(n), Space O(1)
|
|
97
97
|
*/
|
|
98
98
|
reduce<U>(callbackfn: ReduceEntryCallback<K, V, U>, initialValue: U): U;
|
|
99
|
+
/**
|
|
100
|
+
* Converts data structure to `[key, value]` pairs.
|
|
101
|
+
* @returns Array of entries.
|
|
102
|
+
* @remarks Time O(n), Space O(n)
|
|
103
|
+
*/
|
|
104
|
+
toArray(): [K, V][];
|
|
99
105
|
/**
|
|
100
106
|
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
101
107
|
* @returns Array of entries (default) or a string.
|
|
@@ -488,19 +488,8 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
488
488
|
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
489
489
|
*/
|
|
490
490
|
delete(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
* @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Performs a full DFS (pre-order) scan of the tree. Time O(N), as it may visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
|
|
494
|
-
*
|
|
495
|
-
* @template C - The type of the callback function.
|
|
496
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
497
|
-
* @param [onlyOne=false] - If true, stops after finding the first match.
|
|
498
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
499
|
-
* @param [startNode=this._root] - The node to start the search from.
|
|
500
|
-
* @param [iterationType=this.iterationType] - Whether to use 'RECURSIVE' or 'ITERATIVE' search.
|
|
501
|
-
* @returns An array of results from the callback function for each matching node.
|
|
502
|
-
*/
|
|
503
|
-
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
|
+
search(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne?: boolean): (K | undefined)[];
|
|
492
|
+
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>[];
|
|
504
493
|
/**
|
|
505
494
|
* Gets all nodes matching a predicate.
|
|
506
495
|
* @remarks Time O(N) (via `search`). Space O(H) or O(N) (via `search`).
|
|
@@ -598,39 +587,12 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
598
587
|
* @returns The minimum height (-1 for empty, 0 for single node).
|
|
599
588
|
*/
|
|
600
589
|
getMinHeight(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): number;
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on each node in the path.
|
|
608
|
-
* @param [isReverse=false] - If true, returns the path from root-to-node.
|
|
609
|
-
* @returns An array of callback results.
|
|
610
|
-
*/
|
|
611
|
-
getPathToRoot<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(beginNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, callback?: C, isReverse?: boolean): ReturnType<C>[];
|
|
612
|
-
/**
|
|
613
|
-
* Finds the leftmost node in a subtree (the node with the smallest key in a BST).
|
|
614
|
-
* @remarks Time O(H), where H is the height of the left spine. O(N) worst-case. Space O(H) for recursive/trampoline stack.
|
|
615
|
-
*
|
|
616
|
-
* @template C - The type of the callback function.
|
|
617
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on the leftmost node.
|
|
618
|
-
* @param [startNode=this._root] - The subtree root to search from.
|
|
619
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
620
|
-
* @returns The callback result for the leftmost node.
|
|
621
|
-
*/
|
|
622
|
-
getLeftMost<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>;
|
|
623
|
-
/**
|
|
624
|
-
* Finds the rightmost node in a subtree (the node with the largest key in a BST).
|
|
625
|
-
* @remarks Time O(H), where H is the height of the right spine. O(N) worst-case. Space O(H) for recursive/trampoline stack.
|
|
626
|
-
*
|
|
627
|
-
* @template C - The type of the callback function.
|
|
628
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on the rightmost node.
|
|
629
|
-
* @param [startNode=this._root] - The subtree root to search from.
|
|
630
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
631
|
-
* @returns The callback result for the rightmost node.
|
|
632
|
-
*/
|
|
633
|
-
getRightMost<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>;
|
|
590
|
+
getPathToRoot(beginNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): (K | undefined)[];
|
|
591
|
+
getPathToRoot<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(beginNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, callback: C, isReverse?: boolean): ReturnType<C>[];
|
|
592
|
+
getLeftMost(): K | undefined;
|
|
593
|
+
getLeftMost<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(callback: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>;
|
|
594
|
+
getRightMost(): K | undefined;
|
|
595
|
+
getRightMost<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(callback: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>;
|
|
634
596
|
/**
|
|
635
597
|
* Gets the Morris traversal predecessor (rightmost node in the left subtree, or node itself).
|
|
636
598
|
* @remarks This is primarily a helper for Morris traversal. Time O(H), where H is the height of the left subtree. O(N) worst-case. Space O(1).
|
|
@@ -647,23 +609,18 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
647
609
|
* @returns The successor node, or null/undefined if none exists.
|
|
648
610
|
*/
|
|
649
611
|
getSuccessor(x?: K | BinaryTreeNode<K, V> | null): BinaryTreeNode<K, V> | null | undefined;
|
|
612
|
+
dfs(): (K | undefined)[];
|
|
650
613
|
dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
651
614
|
dfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: boolean): ReturnType<C>[];
|
|
615
|
+
bfs(): (K | undefined)[];
|
|
652
616
|
bfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
653
617
|
bfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
*
|
|
658
|
-
* @template C - The type of the callback function.
|
|
659
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each leaf node.
|
|
660
|
-
* @param [startNode=this._root] - The node to start from.
|
|
661
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
662
|
-
* @returns An array of callback results.
|
|
663
|
-
*/
|
|
664
|
-
leaves<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
618
|
+
leaves(): (K | undefined)[];
|
|
619
|
+
leaves<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
620
|
+
listLevels(): (K | undefined)[][];
|
|
665
621
|
listLevels<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][];
|
|
666
622
|
listLevels<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
|
|
623
|
+
morris(): (K | undefined)[];
|
|
667
624
|
morris<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): ReturnType<C>[];
|
|
668
625
|
/**
|
|
669
626
|
* Clones the tree.
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparator,
|
|
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;
|
|
@@ -288,12 +288,6 @@ 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
|
-
/**
|
|
292
|
-
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
293
|
-
* @remarks Time O(1) Space O(1)
|
|
294
|
-
* @returns The default comparator function.
|
|
295
|
-
*/
|
|
296
|
-
protected _createDefaultComparator(): Comparator<K>;
|
|
297
291
|
/**
|
|
298
292
|
* The comparator function used to determine the order of keys in the tree.
|
|
299
293
|
|
|
@@ -341,41 +335,12 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
341
335
|
* @returns True if the key is valid, false otherwise.
|
|
342
336
|
*/
|
|
343
337
|
isValidKey(key: any): key is K;
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
* @param [pattern='IN'] - The traversal order ('IN', 'PRE', 'POST').
|
|
351
|
-
* @param [onlyOne=false] - If true, stops after the first callback.
|
|
352
|
-
* @param [startNode=this._root] - The node to start from.
|
|
353
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
354
|
-
* @returns An array of callback results.
|
|
355
|
-
*/
|
|
356
|
-
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>[];
|
|
357
|
-
/**
|
|
358
|
-
* Performs a Breadth-First Search (BFS) or Level-Order traversal.
|
|
359
|
-
* @remarks Time O(N), visits every node. Space O(N) in the worst case for the queue.
|
|
360
|
-
*
|
|
361
|
-
* @template C - The type of the callback function.
|
|
362
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
363
|
-
* @param [startNode=this._root] - The node to start from.
|
|
364
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
365
|
-
* @returns An array of callback results.
|
|
366
|
-
*/
|
|
367
|
-
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>[];
|
|
368
|
-
/**
|
|
369
|
-
* Returns a 2D array of nodes, grouped by level.
|
|
370
|
-
* @remarks Time O(N), visits every node. Space O(N) for the result array and the queue/stack.
|
|
371
|
-
*
|
|
372
|
-
* @template C - The type of the callback function.
|
|
373
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
374
|
-
* @param [startNode=this._root] - The node to start from.
|
|
375
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
376
|
-
* @returns A 2D array of callback results.
|
|
377
|
-
*/
|
|
378
|
-
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>[][];
|
|
379
344
|
/**
|
|
380
345
|
* Gets the first node matching a predicate.
|
|
381
346
|
* @remarks Time O(log N) if searching by key, O(N) if searching by predicate. Space O(log N) or O(N).
|
|
@@ -386,33 +351,10 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
386
351
|
* @returns The first matching node, or undefined if not found.
|
|
387
352
|
*/
|
|
388
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>>;
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
* Space O(log N) for the stack.
|
|
394
|
-
*
|
|
395
|
-
* @template C - The type of the callback function.
|
|
396
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, predicate, or range to search for.
|
|
397
|
-
* @param [onlyOne=false] - If true, stops after finding the first match.
|
|
398
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
399
|
-
* @param [startNode=this._root] - The node to start the search from.
|
|
400
|
-
* @param [iterationType=this.iterationType] - Whether to use 'RECURSIVE' or 'ITERATIVE' search.
|
|
401
|
-
* @returns An array of results from the callback function for each matching node.
|
|
402
|
-
*/
|
|
403
|
-
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>[];
|
|
404
|
-
/**
|
|
405
|
-
* Performs an optimized search for nodes within a given key range.
|
|
406
|
-
* @remarks Time O(H + M), where H is tree height and M is the number of matches.
|
|
407
|
-
*
|
|
408
|
-
* @template C - The type of the callback function.
|
|
409
|
-
* @param range - A `Range` object or a `[low, high]` tuple.
|
|
410
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
411
|
-
* @param [startNode=this._root] - The node to start the search from.
|
|
412
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
413
|
-
* @returns An array of callback results.
|
|
414
|
-
*/
|
|
415
|
-
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>[];
|
|
416
358
|
/**
|
|
417
359
|
* Adds a new node to the BST based on key comparison.
|
|
418
360
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
@@ -436,39 +378,59 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
436
378
|
*/
|
|
437
379
|
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
438
380
|
/**
|
|
439
|
-
* Returns the first
|
|
440
|
-
*
|
|
441
|
-
*
|
|
442
|
-
* 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.
|
|
443
384
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
444
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
445
|
-
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
446
|
-
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
447
385
|
*/
|
|
448
|
-
|
|
386
|
+
ceiling(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
449
387
|
/**
|
|
450
|
-
* Returns the first node with a key
|
|
451
|
-
*
|
|
452
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
453
|
-
* 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.
|
|
454
390
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
455
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
456
|
-
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
457
|
-
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
458
391
|
*/
|
|
459
|
-
|
|
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>;
|
|
460
393
|
/**
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
*
|
|
469
|
-
*
|
|
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.
|
|
470
404
|
*/
|
|
471
|
-
|
|
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>[];
|
|
472
434
|
/**
|
|
473
435
|
* Rebuilds the tree to be perfectly balanced.
|
|
474
436
|
* @remarks Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
|
|
@@ -536,6 +498,58 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
536
498
|
* - If no nodes match the search criteria, the returned map is empty.
|
|
537
499
|
*/
|
|
538
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;
|
|
541
|
+
/**
|
|
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.
|
|
547
|
+
*
|
|
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.
|
|
551
|
+
*/
|
|
552
|
+
protected _lowerByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
539
553
|
/**
|
|
540
554
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
541
555
|
* Unified logic for both lowerBound and upperBound.
|
|
@@ -1011,6 +1011,14 @@ var binaryTreeTyped = (() => {
|
|
|
1011
1011
|
}
|
|
1012
1012
|
return accumulator;
|
|
1013
1013
|
}
|
|
1014
|
+
/**
|
|
1015
|
+
* Converts data structure to `[key, value]` pairs.
|
|
1016
|
+
* @returns Array of entries.
|
|
1017
|
+
* @remarks Time O(n), Space O(n)
|
|
1018
|
+
*/
|
|
1019
|
+
toArray() {
|
|
1020
|
+
return [...this];
|
|
1021
|
+
}
|
|
1014
1022
|
/**
|
|
1015
1023
|
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
1016
1024
|
* @returns Array of entries (default) or a string.
|
|
@@ -1040,8 +1048,6 @@ var binaryTreeTyped = (() => {
|
|
|
1040
1048
|
this.high = high;
|
|
1041
1049
|
this.includeLow = includeLow;
|
|
1042
1050
|
this.includeHigh = includeHigh;
|
|
1043
|
-
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
1044
|
-
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
1045
1051
|
}
|
|
1046
1052
|
// Determine whether a key is within the range
|
|
1047
1053
|
isInRange(key, comparator) {
|