RedBlackTree
ferum_std::red_black_tree
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:
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)
Quick Example
Struct TreePosition
TreePosition
Used for traversal the tree in various directions.
Constants
KEY_NOT_FOUND
Thrown when trying to perform an operation for a specific key but the key is not set.
VALUE_NOT_FOUND
Thrown when attempting to delete a value that doesn't exist.
EXPECTING_KEY_TO_BE_SET
Expecting the key to be set i.e. asserting leftChildNodeKeyIsSet, before accessing leftChildNodeKey.
INVALID_FIX_DOUBLE_RED_OPERATION
Thrown when trying fix a double red but the tree doesn't follow the correct structure.
INVALID_LEAF_NODE_HAS_LEFT_CHILD
Thrown when trying to perform an operation on a leaf node but that node has a left child.
INVALID_LEAF_NODE_HAS_RIGHT_CHILD
Thrown when trying to perform an operation on a leaf node but that node has a right child.
INVALID_LEAF_NODE_NO_PARENT
Thrown when trying to perform an operation on a leaf node but that node has no parent.
INVALID_NEXT_OPERATION
Thrown when trying to go to the next value or key in the iterator, when there isn't one!
INVALID_OUTGOING_SWAP_EDGE_DIRECTION
Thrown when the edges being swapped doesn't define a valid edge direction.
INVALID_ROTATION_NODES
Thrown when trying to perform an invalid rotation on the tree.
ITERATION_DIRECTION_MAX
When using an iterator, encodes the direction towards the next maximum value i.e. reverse inorder traversal.
ITERATION_DIRECTION_MIN
When using an iterator, encodes the direction towards the next minimum value i.e. inorder traversal.
ONLY_LEAF_NODES_CAN_BE_ADDED
Thrown when trying to add a non leaf node to the tree.
SUCCESSOR_FOR_LEAF_NODE
Thrown when trying to get the successor for a leaf node.
TREE_IS_EMPTY
Thrown when trying to perform an operation on the tree that requires the tree to be non empty.
Functions
Function new
new
Creates a new tree.
Function is_empty
is_empty
Returns if the tree is empty.
Function contains_key
contains_key
Returns true if the tree has at least one value with the given key.
Function key_count
key_count
Returns how many keys are in the tree.
Function value_count
value_count
Returns the total number of values in the tree.
Function key_value_count
key_value_count
Returns the total number of values for the given key.
Function first_value_at
first_value_at
Returns the first value with the givem key.
Function values_at
values_at
Returns all the values with the given key.
Function values_at_list
values_at_list
Returns all the values with the given key as a doubly linked list.
Function max_key
max_key
Returns the maximum key in the tree, if one exists.
Function min_key
min_key
Returns the minimum key in the tree, if one exists.
Function get_next_key
get_next_key
Returns the next key and updates position.
Last updated