The sort order. Optional only for numbers, strings and
bigints. In generic code where TypeScript cannot tell that T is one of
them (T extends number), pass a comparator or create the tree with
BSTree.from<T>([]).
The number of edges on the longest path from the root to a leaf: -1 for
an empty tree and 0 for a tree with a single value. O(1).
The number of values in the tree. O(1).
Same as SortedTree.values.
Removes all values.
Calls callback for every value in ascending order.
OptionalthisArg: unknownInserts value unless an equal value is already stored (that value is
kept as is).
true if the value was inserted.
Returns the largest value, or undefined if the tree is empty.
Returns the smallest value, or undefined if the tree is empty.
Removes and returns the largest value.
Removes and returns the smallest value.
Iterates over the values within the bounds, in ascending order unless
reverse is set. Modifications during the iteration follow the same rules
as for SortedTree.values.
Optionaloptions: RangeOptions<T>Returns the values in ascending order.
Iterates over the values in ascending order.
The tree may be modified during the iteration: removed values are
skipped, values inserted ahead of the iterator are visited and values
inserted behind it are not. Unlike with a Set, a value is never visited
twice, even if it is deleted and inserted again.
After the tree changes, the iterator finds its place again by comparing with the last value it returned. While you use the iterator, do not change the fields the comparator reads of that value, even after removing it from the tree.
Iterates over the values in descending order. Modifications during the iteration follow the same rules as for SortedTree.values.
StaticfromCreates a tree from numbers, strings or bigints.
Creates a tree from numbers, strings or bigints.
Creates a tree from numbers, strings or bigints.
Creates a tree from any values sorted by compare.
Creates a tree from bigints of an explicit type, also in generic code
(BSTree.from<T>(values) with T extends bigint).
Optionalcompare: Comparator<T>Creates a tree from numbers of an explicit type, such as
BSTree.from<1 | 2>([1, 2]), also in generic code
(BSTree.from<T>(values) with T extends number).
Optionalcompare: Comparator<T>Creates a tree from strings of an explicit type, also in generic code
(BSTree.from<T>(values) with T extends string).
Optionalcompare: Comparator<T>
An unbalanced binary search tree.
Operations take O(h) time, where
his the height of the tree: O(log n) on random input, but O(n) when values are inserted in sorted order. Many random removals mixed with insertions also make it deeper over time. Prefer AVLTree unless the input order is known to be random.Like
Array.from,from()called on a subclass creates an instance of that subclass. TypeScript requires the subclass constructor to accept the comparator as its only argument and the values to match the subclass type.Example