data-structure-typed 2.2.5 → 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.
@@ -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
- * Searches the tree for nodes matching a predicate.
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
- * Gets the path from a given node up to the root.
603
- * @remarks Time O(H), where H is the depth of the `beginNode`. O(N) worst-case. Space O(H) for the result array.
604
- *
605
- * @template C - The type of the callback function.
606
- * @param beginNode - The node to start the path from.
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
- * Finds all leaf nodes in the tree.
656
- * @remarks Time O(N), visits every node. Space O(H) for recursive stack or O(N) for iterative queue.
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, CP, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodePredicate, OptNode, RBTNColor } from '../../types';
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
- * Performs a Depth-First Search (DFS) traversal.
340
- * @remarks Time O(N), visits every node. Space O(log N) for the call/explicit stack. O(N) worst-case.
341
- *
342
- * @template C - The type of the callback function.
343
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each node.
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
- * Searches the tree for nodes matching a predicate, key, or range.
385
- * @remarks This is an optimized search for a BST. If searching by key or range, it prunes branches.
386
- * Time O(H + M) for key/range search (H=height, M=matches). O(N) for predicate search.
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 node with a key greater than or equal to the given key.
434
- * This is equivalent to C++ std::lower_bound on a BST.
435
- * Supports RECURSIVE and ITERATIVE implementations.
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
- lowerBound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
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 strictly greater than the given key.
445
- * This is equivalent to C++ std::upper_bound on a BST.
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
- upperBound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
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 node with a key greater than or equal to the given key.
456
- * This is equivalent to Java TreeMap.ceilingEntry().
457
- * Supports RECURSIVE and ITERATIVE implementations.
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
- ceilingEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
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 strictly greater than the given key.
468
- * This is equivalent to Java TreeMap.higherEntry().
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
- higherEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
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 node with a key less than or equal to the given key.
480
- * This is equivalent to Java TreeMap.floorEntry().
481
- * Supports RECURSIVE and ITERATIVE implementations.
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
- floorEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
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 strictly less than the given key.
492
- * This is equivalent to Java TreeMap.lowerEntry().
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
- lowerEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
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
- * Traverses the tree and returns nodes that are lesser or greater than a target node.
504
- * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
505
- *
506
- * @template C - The type of the callback function.
507
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on matching nodes.
508
- * @param [lesserOrGreater=-1] - -1 for lesser, 1 for greater, 0 for equal.
509
- * @param [targetNode=this._root] - The node to compare against.
510
- * @param [iterationType=this.iterationType] - The traversal method.
511
- * @returns An array of callback results.
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
- lesserOrGreaterTraverse<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, lesserOrGreater?: CP, targetNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
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.
@@ -256,6 +256,14 @@ var dataStructureTyped = (() => {
256
256
  }
257
257
  return accumulator;
258
258
  }
259
+ /**
260
+ * Converts data structure to `[key, value]` pairs.
261
+ * @returns Array of entries.
262
+ * @remarks Time O(n), Space O(n)
263
+ */
264
+ toArray() {
265
+ return [...this];
266
+ }
259
267
  /**
260
268
  * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
261
269
  * @returns Array of entries (default) or a string.
@@ -269,7 +277,6 @@ var dataStructureTyped = (() => {
269
277
  * @remarks Time O(n), Space O(n)
270
278
  */
271
279
  print() {
272
- console.log(this.toVisual());
273
280
  }
274
281
  };
275
282
 
@@ -489,7 +496,6 @@ var dataStructureTyped = (() => {
489
496
  * Time O(n) due to materialization, Space O(n) for the intermediate representation.
490
497
  */
491
498
  print() {
492
- console.log(this.toVisual());
493
499
  }
494
500
  };
495
501
 
@@ -6834,8 +6840,6 @@ var dataStructureTyped = (() => {
6834
6840
  this.high = high;
6835
6841
  this.includeLow = includeLow;
6836
6842
  this.includeHigh = includeHigh;
6837
- if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
6838
- if (low > high) throw new RangeError("low must be less than or equal to high");
6839
6843
  }
6840
6844
  // Determine whether a key is within the range
6841
6845
  isInRange(key, comparator) {
@@ -8079,7 +8083,6 @@ var dataStructureTyped = (() => {
8079
8083
  * @param [startNode=this._root] - The node to start printing from.
8080
8084
  */
8081
8085
  print(options, startNode = this._root) {
8082
- console.log(this.toVisual(startNode, options));
8083
8086
  }
8084
8087
  /**
8085
8088
  * (Protected) Core DFS implementation.
@@ -8971,77 +8974,63 @@ var dataStructureTyped = (() => {
8971
8974
  else _iterate();
8972
8975
  return inserted;
8973
8976
  }
8974
- /**
8975
- * Returns the first node with a key greater than or equal to the given key.
8976
- * This is equivalent to C++ std::lower_bound on a BST.
8977
- * Supports RECURSIVE and ITERATIVE implementations.
8978
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
8979
- * Space Complexity: O(h) for recursion, O(1) for iteration.
8980
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
8981
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
8982
- * @returns The first node with key >= given key, or undefined if no such node exists.
8983
- */
8984
- lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
8985
- return this._bound(keyNodeEntryOrPredicate, true, iterationType);
8986
- }
8987
- /**
8988
- * Returns the first node with a key strictly greater than the given key.
8989
- * This is equivalent to C++ std::upper_bound on a BST.
8990
- * Supports RECURSIVE and ITERATIVE implementations.
8991
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
8992
- * Space Complexity: O(h) for recursion, O(1) for iteration.
8993
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
8994
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
8995
- * @returns The first node with key > given key, or undefined if no such node exists.
8996
- */
8997
- upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
8998
- return this._bound(keyNodeEntryOrPredicate, false, iterationType);
8999
- }
9000
- /**
9001
- * Returns the first node with a key greater than or equal to the given key.
9002
- * This is equivalent to Java TreeMap.ceilingEntry().
9003
- * Supports RECURSIVE and ITERATIVE implementations.
9004
- * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9005
- * Space Complexity: O(h) for recursion, O(1) for iteration.
9006
- *
9007
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9008
- * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9009
- * @returns The first node with key >= given key, or undefined if no such node exists.
9010
- */
9011
- ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9012
- return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
8977
+ ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
8978
+ let actualCallback = void 0;
8979
+ let actualIterationType = this.iterationType;
8980
+ if (typeof callback === "string") {
8981
+ actualIterationType = callback;
8982
+ } else if (callback) {
8983
+ actualCallback = callback;
8984
+ if (iterationType) {
8985
+ actualIterationType = iterationType;
8986
+ }
8987
+ }
8988
+ const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
8989
+ if (!actualCallback) {
8990
+ return node == null ? void 0 : node.key;
8991
+ }
8992
+ return node ? actualCallback(node) : void 0;
9013
8993
  }
9014
- /**
9015
- * Returns the first node with a key strictly greater than the given key.
9016
- * This is equivalent to Java TreeMap.higherEntry().
9017
- * Supports RECURSIVE and ITERATIVE implementations.
9018
- * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9019
- * Space Complexity: O(h) for recursion, O(1) for iteration.
9020
- *
9021
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9022
- * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9023
- * @returns The first node with key > given key, or undefined if no such node exists.
9024
- */
9025
- higherEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9026
- return this.upperBound(keyNodeEntryOrPredicate, iterationType);
8994
+ higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
8995
+ let actualCallback = void 0;
8996
+ let actualIterationType = this.iterationType;
8997
+ if (typeof callback === "string") {
8998
+ actualIterationType = callback;
8999
+ } else if (callback) {
9000
+ actualCallback = callback;
9001
+ if (iterationType) {
9002
+ actualIterationType = iterationType;
9003
+ }
9004
+ }
9005
+ const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
9006
+ if (!actualCallback) {
9007
+ return node == null ? void 0 : node.key;
9008
+ }
9009
+ return node ? actualCallback(node) : void 0;
9027
9010
  }
9028
- /**
9029
- * Returns the first node with a key less than or equal to the given key.
9030
- * This is equivalent to Java TreeMap.floorEntry().
9031
- * Supports RECURSIVE and ITERATIVE implementations.
9032
- * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9033
- * Space Complexity: O(h) for recursion, O(1) for iteration.
9034
- *
9035
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9036
- * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9037
- * @returns The first node with key <= given key, or undefined if no such node exists.
9038
- */
9039
- floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9011
+ floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
9040
9012
  if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9013
+ if (typeof callback === "string" || !callback) {
9014
+ return void 0;
9015
+ }
9041
9016
  return void 0;
9042
9017
  }
9018
+ let actualCallback = void 0;
9019
+ let actualIterationType = this.iterationType;
9020
+ if (typeof callback === "string") {
9021
+ actualIterationType = callback;
9022
+ } else if (callback) {
9023
+ actualCallback = callback;
9024
+ if (iterationType) {
9025
+ actualIterationType = iterationType;
9026
+ }
9027
+ }
9043
9028
  if (this._isPredicate(keyNodeEntryOrPredicate)) {
9044
- return this._floorByPredicate(keyNodeEntryOrPredicate, iterationType);
9029
+ const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
9030
+ if (!actualCallback) {
9031
+ return node == null ? void 0 : node.key;
9032
+ }
9033
+ return node ? actualCallback(node) : void 0;
9045
9034
  }
9046
9035
  let targetKey;
9047
9036
  if (this.isNode(keyNodeEntryOrPredicate)) {
@@ -9049,6 +9038,9 @@ var dataStructureTyped = (() => {
9049
9038
  } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9050
9039
  const key = keyNodeEntryOrPredicate[0];
9051
9040
  if (key === null || key === void 0) {
9041
+ if (typeof callback === "string" || !callback) {
9042
+ return void 0;
9043
+ }
9052
9044
  return void 0;
9053
9045
  }
9054
9046
  targetKey = key;
@@ -9056,27 +9048,40 @@ var dataStructureTyped = (() => {
9056
9048
  targetKey = keyNodeEntryOrPredicate;
9057
9049
  }
9058
9050
  if (targetKey !== void 0) {
9059
- return this._floorByKey(targetKey, iterationType);
9051
+ const node = this._floorByKey(targetKey, actualIterationType);
9052
+ if (!actualCallback) {
9053
+ return node == null ? void 0 : node.key;
9054
+ }
9055
+ return node ? actualCallback(node) : void 0;
9056
+ }
9057
+ if (typeof callback === "string" || !callback) {
9058
+ return void 0;
9060
9059
  }
9061
9060
  return void 0;
9062
9061
  }
9063
- /**
9064
- * Returns the first node with a key strictly less than the given key.
9065
- * This is equivalent to Java TreeMap.lowerEntry().
9066
- * Supports RECURSIVE and ITERATIVE implementations.
9067
- * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9068
- * Space Complexity: O(h) for recursion, O(1) for iteration.
9069
- *
9070
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9071
- * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9072
- * @returns The first node with key < given key, or undefined if no such node exists.
9073
- */
9074
- lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9062
+ lower(keyNodeEntryOrPredicate, callback, iterationType) {
9075
9063
  if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9064
+ if (typeof callback === "string" || !callback) {
9065
+ return void 0;
9066
+ }
9076
9067
  return void 0;
9077
9068
  }
9069
+ let actualCallback = void 0;
9070
+ let actualIterationType = this.iterationType;
9071
+ if (typeof callback === "string") {
9072
+ actualIterationType = callback;
9073
+ } else if (callback) {
9074
+ actualCallback = callback;
9075
+ if (iterationType) {
9076
+ actualIterationType = iterationType;
9077
+ }
9078
+ }
9078
9079
  if (this._isPredicate(keyNodeEntryOrPredicate)) {
9079
- return this._lowerByPredicate(keyNodeEntryOrPredicate, iterationType);
9080
+ const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
9081
+ if (!actualCallback) {
9082
+ return node == null ? void 0 : node.key;
9083
+ }
9084
+ return node ? actualCallback(node) : void 0;
9080
9085
  }
9081
9086
  let targetKey;
9082
9087
  if (this.isNode(keyNodeEntryOrPredicate)) {
@@ -9084,6 +9089,9 @@ var dataStructureTyped = (() => {
9084
9089
  } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9085
9090
  const key = keyNodeEntryOrPredicate[0];
9086
9091
  if (key === null || key === void 0) {
9092
+ if (typeof callback === "string" || !callback) {
9093
+ return void 0;
9094
+ }
9087
9095
  return void 0;
9088
9096
  }
9089
9097
  targetKey = key;
@@ -9091,7 +9099,14 @@ var dataStructureTyped = (() => {
9091
9099
  targetKey = keyNodeEntryOrPredicate;
9092
9100
  }
9093
9101
  if (targetKey !== void 0) {
9094
- return this._lowerByKey(targetKey, iterationType);
9102
+ const node = this._lowerByKey(targetKey, actualIterationType);
9103
+ if (!actualCallback) {
9104
+ return node == null ? void 0 : node.key;
9105
+ }
9106
+ return node ? actualCallback(node) : void 0;
9107
+ }
9108
+ if (typeof callback === "string" || !callback) {
9109
+ return void 0;
9095
9110
  }
9096
9111
  return void 0;
9097
9112
  }
@@ -9280,7 +9295,6 @@ var dataStructureTyped = (() => {
9280
9295
  */
9281
9296
  _createDefaultComparator() {
9282
9297
  return (a, b) => {
9283
- debugger;
9284
9298
  if (isComparable(a) && isComparable(b)) {
9285
9299
  if (a > b) return 1;
9286
9300
  if (a < b) return -1;