@@ -125,10 +125,7 @@ export class BinarySearchTree<T> implements Iterable<T> {
125
125
internals . findNode = < T > (
126
126
tree : BinarySearchTree < T > ,
127
127
value : T ,
128
- select ?: "higher" | "lower" ,
129
- returnIfFound ?: boolean ,
130
- ) : BinarySearchNode < T > | null =>
131
- tree . #findNode( value , select , returnIfFound ) ;
128
+ ) : BinarySearchNode < T > | null => tree . #findNode( value ) ;
132
129
internals . rotateNode = < T > (
133
130
tree : BinarySearchTree < T > ,
134
131
node : BinarySearchNode < T > ,
@@ -322,49 +319,15 @@ export class BinarySearchTree<T> implements Iterable<T> {
322
319
return this . #size;
323
320
}
324
321
325
- /**
326
- * Finds the node matching the given selection criteria.
327
- *
328
- * When searching for higher nodes, returns the lowest node that is higher than
329
- * the value. When searching for lower nodes, returns the highest node that is
330
- * lower than the value.
331
- *
332
- * By default, only accepts a node exactly matching the passed value and returns
333
- * it if found.
334
- *
335
- * @param value The value to search for
336
- * @param select Whether to accept nodes that are higher or lower than the value
337
- * @param returnIfFound Whether a node matching the value itself is accepted
338
- * @returns The node that matched, or null if none matched
339
- */
340
- #findNode(
341
- value : T ,
342
- select ?: "higher" | "lower" ,
343
- returnIfFound : boolean = true ,
344
- ) : BinarySearchNode < T > | null {
322
+ #findNode( value : T ) : BinarySearchNode < T > | null {
345
323
let node : BinarySearchNode < T > | null = this . #root;
346
- let result : BinarySearchNode < T > | null = null ;
347
324
while ( node ) {
348
- const order = this . #compare( value , node . value ) ;
349
- if ( order === 0 && returnIfFound ) return node ;
350
-
351
- let direction : Direction = order < 0 ? "left" : "right" ;
352
- if ( select === "higher" && order === 0 ) {
353
- direction = "right" ;
354
- } else if ( select === "lower" && order === 0 ) {
355
- direction = "left" ;
356
- }
357
-
358
- if (
359
- ( select === "higher" && direction === "left" ) ||
360
- ( select === "lower" && direction === "right" )
361
- ) {
362
- result = node ;
363
- }
364
-
325
+ const order : number = this . #compare( value as T , node . value ) ;
326
+ if ( order === 0 ) break ;
327
+ const direction : "left" | "right" = order < 0 ? "left" : "right" ;
365
328
node = node [ direction ] ;
366
329
}
367
- return result ;
330
+ return node ;
368
331
}
369
332
370
333
#rotateNode( node : BinarySearchNode < T > , direction : Direction ) {
@@ -525,114 +488,6 @@ export class BinarySearchTree<T> implements Iterable<T> {
525
488
return this . #findNode( value ) ?. value ?? null ;
526
489
}
527
490
528
- /**
529
- * Finds the lowest (leftmost) value in the binary search tree which is
530
- * greater than or equal to the given value, or null if the given value
531
- * is higher than all elements of the tree.
532
- *
533
- * The complexity of this operation depends on the underlying structure of the
534
- * tree. Refer to the documentation of the structure itself for more details.
535
- *
536
- * @example Finding values in the tree
537
- * ```ts
538
- * import { BinarySearchTree } from "@std/data-structures";
539
- * import { assertEquals } from "@std/assert";
540
- *
541
- * const tree = BinarySearchTree.from<number>([42]);
542
- *
543
- * assertEquals(tree.ceiling(41), 42);
544
- * assertEquals(tree.ceiling(42), 42);
545
- * assertEquals(tree.ceiling(43), null);
546
- * ```
547
- *
548
- * @param value The value to search for in the binary search tree.
549
- * @returns The ceiling if it was found, or null if not.
550
- */
551
- ceiling ( value : T ) : T | null {
552
- return this . #findNode( value , "higher" ) ?. value ?? null ;
553
- }
554
-
555
- /**
556
- * Finds the highest (rightmost) value in the binary search tree which is
557
- * less than or equal to the given value, or null if the given value
558
- * is lower than all elements of the tree.
559
- *
560
- * The complexity of this operation depends on the underlying structure of the
561
- * tree. Refer to the documentation of the structure itself for more details.
562
- *
563
- * @example Finding values in the tree
564
- * ```ts
565
- * import { BinarySearchTree } from "@std/data-structures";
566
- * import { assertEquals } from "@std/assert";
567
- *
568
- * const tree = BinarySearchTree.from<number>([42]);
569
- *
570
- * assertEquals(tree.floor(41), null);
571
- * assertEquals(tree.floor(42), 42);
572
- * assertEquals(tree.floor(43), 42);
573
- * ```
574
- *
575
- * @param value The value to search for in the binary search tree.
576
- * @returns The floor if it was found, or null if not.
577
- */
578
- floor ( value : T ) : T | null {
579
- return this . #findNode( value , "lower" ) ?. value ?? null ;
580
- }
581
-
582
- /**
583
- * Finds the lowest (leftmost) value in the binary search tree which is
584
- * strictly greater than the given value, or null if the given value
585
- * is higher than or equal to all elements of the tree
586
- *
587
- * The complexity of this operation depends on the underlying structure of the
588
- * tree. Refer to the documentation of the structure itself for more details.
589
- *
590
- * @example Finding values in the tree
591
- * ```ts
592
- * import { BinarySearchTree } from "@std/data-structures";
593
- * import { assertEquals } from "@std/assert";
594
- *
595
- * const tree = BinarySearchTree.from<number>([42]);
596
- *
597
- * assertEquals(tree.higher(41), 42);
598
- * assertEquals(tree.higher(42), null);
599
- * assertEquals(tree.higher(43), null);
600
- * ```
601
- *
602
- * @param value The value to search for in the binary search tree.
603
- * @returns The higher value if it was found, or null if not.
604
- */
605
- higher ( value : T ) : T | null {
606
- return this . #findNode( value , "higher" , false ) ?. value ?? null ;
607
- }
608
-
609
- /**
610
- * Finds the highest (rightmost) value in the binary search tree which is
611
- * strictly less than the given value, or null if the given value
612
- * is lower than or equal to all elements of the tree
613
- *
614
- * The complexity of this operation depends on the underlying structure of the
615
- * tree. Refer to the documentation of the structure itself for more details.
616
- *
617
- * @example Finding values in the tree
618
- * ```ts
619
- * import { BinarySearchTree } from "@std/data-structures";
620
- * import { assertEquals } from "@std/assert";
621
- *
622
- * const tree = BinarySearchTree.from<number>([42]);
623
- *
624
- * assertEquals(tree.lower(41), null);
625
- * assertEquals(tree.lower(42), null);
626
- * assertEquals(tree.lower(43), 42);
627
- * ```
628
- *
629
- * @param value The value to search for in the binary search tree.
630
- * @returns The lower value if it was found, or null if not.
631
- */
632
- lower ( value : T ) : T | null {
633
- return this . #findNode( value , "lower" , false ) ?. value ?? null ;
634
- }
635
-
636
491
/**
637
492
* Retrieve the lowest (left most) value in the binary search tree, or null if
638
493
* the tree is empty.
0 commit comments