binary-tree-typed 2.2.5 → 2.2.7
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 +46 -126
- 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/binary-tree.ts +67 -0
- package/src/data-structures/binary-tree/bst.ts +295 -89
|
@@ -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;
|
|
@@ -335,41 +335,12 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
335
335
|
* @returns True if the key is valid, false otherwise.
|
|
336
336
|
*/
|
|
337
337
|
isValidKey(key: any): key is K;
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
* @param [pattern='IN'] - The traversal order ('IN', 'PRE', 'POST').
|
|
345
|
-
* @param [onlyOne=false] - If true, stops after the first callback.
|
|
346
|
-
* @param [startNode=this._root] - The node to start from.
|
|
347
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
348
|
-
* @returns An array of callback results.
|
|
349
|
-
*/
|
|
350
|
-
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>[];
|
|
351
|
-
/**
|
|
352
|
-
* Performs a Breadth-First Search (BFS) or Level-Order traversal.
|
|
353
|
-
* @remarks Time O(N), visits every node. Space O(N) in the worst case for the queue.
|
|
354
|
-
*
|
|
355
|
-
* @template C - The type of the callback function.
|
|
356
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
357
|
-
* @param [startNode=this._root] - The node to start from.
|
|
358
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
359
|
-
* @returns An array of callback results.
|
|
360
|
-
*/
|
|
361
|
-
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>[];
|
|
362
|
-
/**
|
|
363
|
-
* Returns a 2D array of nodes, grouped by level.
|
|
364
|
-
* @remarks Time O(N), visits every node. Space O(N) for the result array and the queue/stack.
|
|
365
|
-
*
|
|
366
|
-
* @template C - The type of the callback function.
|
|
367
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
|
|
368
|
-
* @param [startNode=this._root] - The node to start from.
|
|
369
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
370
|
-
* @returns A 2D array of callback results.
|
|
371
|
-
*/
|
|
372
|
-
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>[][];
|
|
373
344
|
/**
|
|
374
345
|
* Gets the first node matching a predicate.
|
|
375
346
|
* @remarks Time O(log N) if searching by key, O(N) if searching by predicate. Space O(log N) or O(N).
|
|
@@ -380,33 +351,10 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
380
351
|
* @returns The first matching node, or undefined if not found.
|
|
381
352
|
*/
|
|
382
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>>;
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
* Space O(log N) for the stack.
|
|
388
|
-
*
|
|
389
|
-
* @template C - The type of the callback function.
|
|
390
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, predicate, or range to search for.
|
|
391
|
-
* @param [onlyOne=false] - If true, stops after finding the first match.
|
|
392
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
393
|
-
* @param [startNode=this._root] - The node to start the search from.
|
|
394
|
-
* @param [iterationType=this.iterationType] - Whether to use 'RECURSIVE' or 'ITERATIVE' search.
|
|
395
|
-
* @returns An array of results from the callback function for each matching node.
|
|
396
|
-
*/
|
|
397
|
-
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>[];
|
|
398
|
-
/**
|
|
399
|
-
* Performs an optimized search for nodes within a given key range.
|
|
400
|
-
* @remarks Time O(H + M), where H is tree height and M is the number of matches.
|
|
401
|
-
*
|
|
402
|
-
* @template C - The type of the callback function.
|
|
403
|
-
* @param range - A `Range` object or a `[low, high]` tuple.
|
|
404
|
-
* @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
|
|
405
|
-
* @param [startNode=this._root] - The node to start the search from.
|
|
406
|
-
* @param [iterationType=this.iterationType] - The traversal method.
|
|
407
|
-
* @returns An array of callback results.
|
|
408
|
-
*/
|
|
409
|
-
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>[];
|
|
410
358
|
/**
|
|
411
359
|
* Adds a new node to the BST based on key comparison.
|
|
412
360
|
* @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
|
|
@@ -430,87 +378,59 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
430
378
|
*/
|
|
431
379
|
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
432
380
|
/**
|
|
433
|
-
* Returns the first
|
|
434
|
-
*
|
|
435
|
-
*
|
|
436
|
-
* 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.
|
|
437
384
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
438
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
439
|
-
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
440
|
-
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
441
385
|
*/
|
|
442
|
-
|
|
386
|
+
ceiling(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
443
387
|
/**
|
|
444
|
-
* Returns the first node with a key
|
|
445
|
-
*
|
|
446
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
447
|
-
* 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.
|
|
448
390
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
449
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
450
|
-
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
451
|
-
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
452
391
|
*/
|
|
453
|
-
|
|
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>;
|
|
454
393
|
/**
|
|
455
|
-
* Returns the first
|
|
456
|
-
*
|
|
457
|
-
*
|
|
458
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
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.
|
|
459
397
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
460
|
-
*
|
|
461
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
462
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
463
|
-
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
464
398
|
*/
|
|
465
|
-
|
|
399
|
+
higher(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
466
400
|
/**
|
|
467
|
-
* Returns the first node with a key
|
|
468
|
-
*
|
|
469
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
470
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
401
|
+
* Returns the first node with a key > target and applies callback.
|
|
402
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
471
403
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
472
|
-
*
|
|
473
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
474
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
475
|
-
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
476
404
|
*/
|
|
477
|
-
|
|
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>;
|
|
478
406
|
/**
|
|
479
|
-
* Returns the first
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
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.
|
|
483
410
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
484
|
-
*
|
|
485
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
486
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
487
|
-
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
488
411
|
*/
|
|
489
|
-
|
|
412
|
+
floor(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): K | undefined;
|
|
490
413
|
/**
|
|
491
|
-
* Returns the first node with a key
|
|
492
|
-
*
|
|
493
|
-
* Supports RECURSIVE and ITERATIVE implementations.
|
|
494
|
-
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
414
|
+
* Returns the first node with a key <= target and applies callback.
|
|
415
|
+
* Time Complexity: O(log n) average, O(h) worst case.
|
|
495
416
|
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
496
|
-
*
|
|
497
|
-
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
498
|
-
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
499
|
-
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
500
417
|
*/
|
|
501
|
-
|
|
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>;
|
|
502
419
|
/**
|
|
503
|
-
*
|
|
504
|
-
*
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
*
|
|
511
|
-
*
|
|
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.
|
|
512
430
|
*/
|
|
513
|
-
|
|
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>[];
|
|
514
434
|
/**
|
|
515
435
|
* Rebuilds the tree to be perfectly balanced.
|
|
516
436
|
* @remarks Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
|
|
@@ -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) {
|