Skip to content

Commit e7eaa61

Browse files
committed
Move new BST functions to unstable
1 parent cd8425c commit e7eaa61

File tree

6 files changed

+436
-197
lines changed

6 files changed

+436
-197
lines changed

data_structures/_binary_search_tree_internals.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ export const internals: {
2424
findNode<T>(
2525
tree: BinarySearchTree<T>,
2626
value: T,
27-
select?: "higher" | "lower",
28-
returnIfFound?: boolean,
2927
): BinarySearchNode<T> | null;
3028
rotateNode<T>(
3129
tree: BinarySearchTree<T>,

data_structures/binary_search_tree.ts

Lines changed: 6 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ export class BinarySearchTree<T> implements Iterable<T> {
125125
internals.findNode = <T>(
126126
tree: BinarySearchTree<T>,
127127
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);
132129
internals.rotateNode = <T>(
133130
tree: BinarySearchTree<T>,
134131
node: BinarySearchNode<T>,
@@ -322,49 +319,15 @@ export class BinarySearchTree<T> implements Iterable<T> {
322319
return this.#size;
323320
}
324321

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 {
345323
let node: BinarySearchNode<T> | null = this.#root;
346-
let result: BinarySearchNode<T> | null = null;
347324
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";
365328
node = node[direction];
366329
}
367-
return result;
330+
return node;
368331
}
369332

370333
#rotateNode(node: BinarySearchNode<T>, direction: Direction) {
@@ -525,114 +488,6 @@ export class BinarySearchTree<T> implements Iterable<T> {
525488
return this.#findNode(value)?.value ?? null;
526489
}
527490

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-
636491
/**
637492
* Retrieve the lowest (left most) value in the binary search tree, or null if
638493
* the tree is empty.

data_structures/binary_search_tree_test.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -556,47 +556,3 @@ Deno.test("BinarySearchTree.clear()", () => {
556556
tree.clear();
557557
assert(tree.isEmpty());
558558
});
559-
560-
Deno.test("BinarySearchTree.ceiling()", () => {
561-
const tree = BinarySearchTree.from([4, 2, 1, 3, 6, 5, 7]);
562-
assertEquals(tree.ceiling(0.5), 1);
563-
assertEquals(tree.ceiling(1), 1);
564-
assertEquals(tree.ceiling(1.5), 2);
565-
assertEquals(tree.ceiling(3.5), 4);
566-
assertEquals(tree.ceiling(6.5), 7);
567-
assertEquals(tree.ceiling(7), 7);
568-
assertEquals(tree.ceiling(7.5), null);
569-
});
570-
571-
Deno.test("BinarySearchTree.floor()", () => {
572-
const tree = BinarySearchTree.from([4, 2, 1, 3, 6, 5, 7]);
573-
assertEquals(tree.floor(0.5), null);
574-
assertEquals(tree.floor(1), 1);
575-
assertEquals(tree.floor(1.5), 1);
576-
assertEquals(tree.floor(4.5), 4);
577-
assertEquals(tree.floor(6.5), 6);
578-
assertEquals(tree.floor(7), 7);
579-
assertEquals(tree.floor(7.5), 7);
580-
});
581-
582-
Deno.test("BinarySearchTree.higher()", () => {
583-
const tree = BinarySearchTree.from([4, 2, 1, 3, 6, 5, 7]);
584-
assertEquals(tree.higher(0.5), 1);
585-
assertEquals(tree.higher(1), 2);
586-
assertEquals(tree.higher(1.5), 2);
587-
assertEquals(tree.higher(3.5), 4);
588-
assertEquals(tree.higher(6.5), 7);
589-
assertEquals(tree.higher(7), null);
590-
assertEquals(tree.higher(7.5), null);
591-
});
592-
593-
Deno.test("BinarySearchTree.lower()", () => {
594-
const tree = BinarySearchTree.from([4, 2, 1, 3, 6, 5, 7]);
595-
assertEquals(tree.lower(0.5), null);
596-
assertEquals(tree.lower(1), null);
597-
assertEquals(tree.lower(1.5), 1);
598-
assertEquals(tree.lower(4.5), 4);
599-
assertEquals(tree.lower(6.5), 6);
600-
assertEquals(tree.lower(7), 6);
601-
assertEquals(tree.lower(7.5), 7);
602-
});

data_structures/deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"./unstable-bidirectional-map": "./unstable_bidirectional_map.ts",
77
"./binary-heap": "./binary_heap.ts",
88
"./binary-search-tree": "./binary_search_tree.ts",
9+
"./unstable-binary-search-tree": "./unstable_binary_search_tree.ts",
910
"./comparators": "./comparators.ts",
1011
"./red-black-tree": "./red_black_tree.ts"
1112
}

0 commit comments

Comments
 (0)