Red Black Tree¶
| Spice | |
|---|---|
RedBlackTree<K, V> struct¶
Implements: IIterable<Pair<K, V>>
A Red-Black Tree is a self-balancing search tree, which is used e.g. in the implementation of maps.
Time complexity:
Insert: O(log n)
Delete: O(log n)
Lookup: O(log n)
Methods¶
insert¶
| Spice | |
|---|---|
Insert a new key-value pair into the tree.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key of the new element |
value |
const V& |
The value of the new element |
upsert¶
| Spice | |
|---|---|
Upsert a key-value pair into the tree. If the key already exists, update its value. Otherwise, insert a new key-value pair.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key of the element |
value |
const V& |
The value of the element |
remove¶
| Spice | |
|---|---|
Remove an element from the tree. If the key is not found, this method is a noop.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key of the element to remove |
find¶
| Spice | |
|---|---|
Find the value for a given key. Note: If the key is not found in the tree, this function will panic. To avoid this, use findSafe instead.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to search for |
Returns: V& — The value for the given key
findSafe¶
| Spice | |
|---|---|
Find the value for a given key.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to search for |
Returns: Result<V> — The value for the given key, or an error if the key was not found
contains¶
| Spice | |
|---|---|
Check if the tree contains a given key.
Parameters
| Name | Type | Description |
|---|---|---|
key |
const K& |
The key to search for |
Returns: bool — True if the key was found, false otherwise
getSize¶
| Spice | |
|---|---|
Get the number of elements in the tree.
Returns: unsigned long — The number of elements in the tree
isEmpty¶
| Spice | |
|---|---|
Check if the tree is empty.
Returns: bool — True if the tree is empty, false otherwise
clear¶
| Spice | |
|---|---|
Clear all elements from the tree.
getIterator¶
| Spice | |
|---|---|
Retrieve a forward iterator for the red black tree
Returns: RedBlackTreeIterator<K, V>
RedBlackTreeIterator<K, V> struct¶
Implements: IIterator<Pair<const K&, V&>>
Iterator to iterate over a red black tree data structure
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a red-black tree iterator over the given tree, starting at its minimum key
Parameters
| Name | Type | Description |
|---|---|---|
tree |
RedBlackTree<K, V>& |
Red-black tree to iterate over |
Methods¶
get¶
| Spice | |
|---|---|
Returns the current key-value pair of the red black tree
Returns: Pair<const K&, V&>& — Current key/value pair
getIdx¶
| Spice | |
|---|---|
Returns the current index and the current item of the red black tree
Returns: Pair<unsigned long, Pair<const K&, V&>&> — Pair of current index and current key/value pair
isValid¶
| Spice | |
|---|---|
Check if the iterator is valid
Returns: bool — true or false
next¶
| Spice | |
|---|---|
Moves the cursor to the next key/value pair