Class BinarySearchTree<T>
Type Parameters
Implements
- Iterable<T>
Hierarchy
- BinarySearchTree (view full)
Constructors
constructor
- new
Binary <T>(compare?): BinarySearchTree<T>Search Tree Construct an empty binary search tree.
To create a binary search tree from an array like, an iterable object, or an existing binary search tree, use the BinarySearchTree.from method.
Type Parameters
Parameters
Returns BinarySearchTree<T>
Accessors
size
- get size(): number
The count of values stored in the binary search tree.
The complexity of this operation is O(1).
Returns number
The count of values stored in the binary search tree.
Methods
[iterator]
clear
find
- find(value): null | T
Check if a value exists in the binary search tree.
The complexity of this operation depends on the underlying structure of the tree. Refer to the documentation of the structure itself for more details.
Parameters
- value: T
The value to search for in the binary search tree.
Returns null | T
The value if it was found, or null if not found.
- value: T
insert
- insert(value): boolean
Add a value to the binary search tree if it does not already exist in the tree.
The complexity of this operation is on average O(log n), where n is the number of values in the tree. In the worst case, the complexity is O(n).
Parameters
- value: T
The value to insert into the binary search tree.
Returns boolean
trueif the value was inserted,falseif the value already exists in the tree.- value: T
isEmpty
lnrValues
lrnValues
lvlValues
max
- max(): null | T
Retrieve the highest (right most) value in the binary search tree, or null if the tree is empty.
The complexity of this operation depends on the underlying structure of the tree. Refer to the documentation of the structure itself for more details.
Returns null | T
The maximum value in the binary search tree, or null if the tree is empty.
min
- min(): null | T
Retrieve the lowest (left most) value in the binary search tree, or null if the tree is empty.
The complexity of this operation depends on the underlying structure of the tree. Refer to the documentation of the structure itself for more details.
Returns null | T
The minimum value in the binary search tree, or null if the tree is empty.
nlrValues
remove
- remove(value): boolean
Remove a value from the binary search tree if it exists in the tree.
The complexity of this operation is on average O(log n), where n is the number of values in the tree. In the worst case, the complexity is O(n).
Parameters
- value: T
The value to remove from the binary search tree.
Returns boolean
trueif the value was found and removed,falseif the value was not found in the tree.- value: T
rnlValues
Staticfrom
- from<T>(collection, options?): BinarySearchTree<T>
Creates a new binary search tree from an array like, an iterable object, or an existing binary search tree.
A custom comparison function can be provided to sort the values in a specific order. By default, the values are sorted in ascending order, unless a BinarySearchTree is passed, in which case the comparison function is copied from the input tree.
Type Parameters
Parameters
Returns BinarySearchTree<T>
A new binary search tree created from the passed collection.
Example: Creating a binary search tree from an array like
import { BinarySearchTree } from "@std/data-structures";
const tree = BinarySearchTree.from<number>([42, 43, 41]);Example: Creating a binary search tree from an iterable object
import { BinarySearchTree } from "@std/data-structures";
const tree = BinarySearchTree.from<number>((function*() {
yield 42;
yield 43;
yield 41;
})());Example: Creating a binary search tree from an existing binary search tree
import { BinarySearchTree } from "@std/data-structures";
const tree = BinarySearchTree.from<number>([42, 43, 41]);
const copy = BinarySearchTree.from(tree);- from<T, U, V>(collection, options): BinarySearchTree<U>
Create a new binary search tree from an array like, an iterable object, or an existing binary search tree.
A custom mapping function can be provided to transform the values before inserting them into the tree.
A custom comparison function can be provided to sort the values in a specific order. A custom mapping function can be provided to transform the values before inserting them into the tree. By default, the values are sorted in ascending order, unless a BinarySearchTree is passed, in which case the comparison function is copied from the input tree. The comparison operator is used to sort the values in the tree after mapping the values.
Type Parameters
Parameters
- collection: ArrayLike<T> | Iterable<T, any, any> | BinarySearchTree<T>
An array like, an iterable, or existing binary search tree.
- options: {
compare?: ((a: U, b: U) => number);
map: ((value: T, index: number) => U);
thisArg?: V;
}The options object to customize the mapping and comparison functions. The
thisArgproperty can be used to set thethisvalue when calling the mapping function.
Returns BinarySearchTree<U>
A new binary search tree containing the mapped values from the passed collection.
- collection: ArrayLike<T> | Iterable<T, any, any> | BinarySearchTree<T>
An unbalanced binary search tree. The values are in ascending order by default, using JavaScript's built-in comparison operators to sort the values.
For performance, it's recommended that you use a self-balancing binary search tree instead of this one unless you are extending this to create a self-balancing tree. See RedBlackTree for an example of how BinarySearchTree can be extended to create a self-balancing binary search tree.
Example: Usage
Typeparam
T The type of the values stored in the binary search tree.