Class RedBlackTree<T>
Type Parameters
Hierarchy
- BinarySearchTree<T>
- RedBlackTree (view full)
Constructors
constructor
- new
Red <T>(compare?): RedBlackTree<T>Black Tree Construct an empty red-black tree.
Type Parameters
Parameters
Returns RedBlackTree<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 red-black tree if it does not already exist in the tree.
The complexity of this operation is on average and at worst O(log n), where n is the number of values in the tree.
Parameters
- value: T
The value to insert into the 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 red-black tree if it exists in the tree.
The complexity of this operation is on average and at worst O(log n), where n is the number of values in the tree.
Parameters
- value: T
The value to remove from the 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?): RedBlackTree<T>
Create a new red-black tree from an array like, an iterable object, or an existing red-black 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 RedBlackTree is passed, in which case the comparison function is copied from the input tree.
Type Parameters
Parameters
Returns RedBlackTree<T>
A new red-black tree with the values from the passed collection.
Example: Creating a red-black tree from an array like
import { RedBlackTree } from "@std/data-structures";
const tree = RedBlackTree.from<number>([3, 10, 13, 4, 6, 7, 1, 14]);Example: Creating a red-black tree from an iterable object
import { RedBlackTree } from "@std/data-structures";
const tree = RedBlackTree.from<number>((function*() {
yield 3;
yield 10;
yield 13;
})());Example: Creating a red-black tree from an existing red-black tree
import { RedBlackTree } from "@std/data-structures";
const tree = RedBlackTree.from<number>([3, 10, 13, 4, 6, 7, 1, 14]);
const copy = RedBlackTree.from(tree);- from<T, U, V>(collection, options): RedBlackTree<U>
Create a new red-black tree from an array like, an iterable object, or an existing red-black 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 RedBlackTree 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> | RedBlackTree<T>
An array like, an iterable, or existing red-black 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 RedBlackTree<U>
A new red-black tree with the mapped values from the passed collection.
- collection: ArrayLike<T> | Iterable<T, any, any> | RedBlackTree<T>
A red-black tree. This is a kind of self-balancing binary search tree. The values are in ascending order by default, using JavaScript's built-in comparison operators to sort the values.
Red-Black Trees require fewer rotations than AVL Trees, so they can provide faster insertions and removal operations. If you need faster lookups, you should use an AVL Tree instead. AVL Trees are more strictly balanced than Red-Black Trees, so they can provide faster lookups.
Example: Usage
Typeparam
T The type of the values being stored in the tree.