RedBlackTree
ferum_std::red_black_tree
Last updated
ferum_std::red_black_tree
Last updated
Ferum's implementation of a Red Black Tree. A red black tree is a self balancing binary tree which performs rotations on tree manipulations to maintain a tree height of log(k), where k is the number of keys in the tree. Values with duplicate keys can be inserted into the tree; each value will stored in a linked list on each tree node. When a node no longer has any values, the node is removed (this is referred to as key deletion). The tree only supports u128 keys because (as of writing) move has no way to define comparators for generic types.
The tree supports the following operations with the given time complexities:
Operation | Worst Case Time Complexity | Amortized Time Complexity |
---|---|---|
TreePosition
Used for traversal the tree in various directions.
Thrown when trying to perform an operation for a specific key but the key is not set.
Thrown when attempting to delete a value that doesn't exist.
Expecting the key to be set i.e. asserting leftChildNodeKeyIsSet, before accessing leftChildNodeKey.
Thrown when trying fix a double red but the tree doesn't follow the correct structure.
Thrown when trying to perform an operation on a leaf node but that node has a left child.
Thrown when trying to perform an operation on a leaf node but that node has a right child.
Thrown when trying to perform an operation on a leaf node but that node has no parent.
Thrown when trying to go to the next value or key in the iterator, when there isn't one!
Thrown when the edges being swapped doesn't define a valid edge direction.
Thrown when trying to perform an invalid rotation on the tree.
When using an iterator, encodes the direction towards the next maximum value i.e. reverse inorder traversal.
When using an iterator, encodes the direction towards the next minimum value i.e. inorder traversal.
Thrown when trying to add a non leaf node to the tree.
Thrown when trying to get the successor for a leaf node.
Thrown when trying to perform an operation on the tree that requires the tree to be non empty.
new
Creates a new tree.
is_empty
Returns if the tree is empty.
contains_key
Returns true if the tree has at least one value with the given key.
key_count
Returns how many keys are in the tree.
value_count
Returns the total number of values in the tree.
key_value_count
Returns the total number of values for the given key.
first_value_at
Returns the first value with the givem key.
values_at
Returns all the values with the given key.
values_at_list
Returns all the values with the given key as a doubly linked list.
max_key
Returns the maximum key in the tree, if one exists.
min_key
Returns the minimum key in the tree, if one exists.
get_next_key
Returns the next key and updates position.
Deletion of value
O(1)
O(1)
Deletion of key
O(log(k))
O(1)
Insertion of value with new key
O(log(k))
O(log(k))
Insertion of value with existing key
O(1)
O(1)
Retrieval of min/max key
O(1)
O(1)