ORIGINAL
|
Red-Black Tree container implementation. More...
#include <RBTree.h>
Classes | |
class | Iterator |
Bidirectional iterator for RBTree. More... | |
class | RBNode |
Internal node class for Red-Black Tree. More... | |
Public Types | |
using | color = RBNode::color |
Color type alias. | |
using | rebind_alloc_node = ALLOC::template rebind_alloc< RBNode > |
Rebound allocator type. | |
Public Member Functions | |
RBNode * | treeCopy () const |
Creates a deep copy of the tree. | |
RBNode * | getPrecursorNode (RBNode *cur) const |
Gets precursor node (in-order predecessor) | |
RBNode * | getSuccessorNode (RBNode *cur) const |
Gets successor node (in-order successor) | |
RBNode * | getMinNode () const |
Gets the minimum node in the tree. | |
RBNode * | getMaxNode () const |
Gets the maximum node in the tree. | |
RBNode * | replaceNode (RBNode *src, RBNode *tar) |
Replaces one node with another while maintaining tree structure. | |
RBNode * | createNode (const K_TYPE &key=K_TYPE{}, const V_TYPE &value=V_TYPE{}, color color=RED, RBNode *parent=nullptr, RBNode *left=nullptr, RBNode *right=nullptr) const |
Creates a new node with given parameters. | |
RBNode * | createNode (RBNode &&other_node) const |
Creates a new node by moving from another node. | |
void | destroyNode (RBNode *node) noexcept |
Destroys a node and deallocates memory. | |
bool | highPriority (RBNode *cur, RBNode *other) const |
Compares priority between two nodes. | |
bool | highPriority (const K_TYPE &key, RBNode *other) const |
Compares priority between a key and a node. | |
RBNode * | rotateLeft (RBNode *cur) |
Performs left rotation around a node. | |
RBNode * | rotateRight (RBNode *cur) |
Performs right rotation around a node. | |
void | adjustInsert (RBNode *cur) |
Adjusts tree after insertion to maintain RB properties. | |
void | adjustErase (RBNode *cur) |
Adjusts tree after deletion to maintain RB properties. | |
void | destroyTree () noexcept |
Destroys entire tree and deallocates all nodes. | |
RBTree (Compare compare=Compare{}) | |
Constructs RBTree with given comparison function. | |
RBNode * | find (const K_TYPE &key) const |
Finds node with given key. | |
bool | modify (const K_TYPE &key, const V_TYPE &value) |
Modifies value for existing key. | |
bool | insert (const K_TYPE &key, const V_TYPE &value) |
Inserts new key-value pair. | |
bool | erase (const K_TYPE &key) |
Erases node with given key. | |
virtual | ~RBTree () |
Destructor. | |
Public Attributes | |
RBNode * | root_ |
Root node pointer. | |
u_integer | size_ |
Number of elements. | |
Compare | compare_ |
Comparison function. | |
rebind_alloc_node | rebind_alloc {} |
Node allocator. | |
friend | Iterator |
Static Public Attributes | |
static constexpr color | BLACK = color::BLACK |
Black color constant. | |
static constexpr color | RED = color::RED |
Red color constant. | |
Red-Black Tree container implementation.
K_TYPE | Key type (must be comparable) |
V_TYPE | Value type |
ALLOC | Allocator type (default: allocator<K_TYPE>) |
Compare | Comparison function type (default: increaseComparator<K_TYPE>) |
This class provides a balanced binary search tree implementation with the following properties:
|
explicit |
Constructs RBTree with given comparison function.
compare | Comparison function to use |
|
virtual |
Destructor.
Cleans up all tree nodes and allocated memory
Adjusts tree after deletion to maintain RB properties.
cur | Node where deletion occurred |
Handles color flips and rotations as needed
Adjusts tree after insertion to maintain RB properties.
cur | Newly inserted node |
Handles color flips and rotations as needed
original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::RBNode * original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::createNode | ( | const K_TYPE & | key = K_TYPE{} , |
const V_TYPE & | value = V_TYPE{} , |
||
color | color = RED , |
||
RBNode * | parent = nullptr , |
||
RBNode * | left = nullptr , |
||
RBNode * | right = nullptr |
||
) | const |
Creates a new node with given parameters.
key | Key for new node |
value | Value for new node |
color | Initial color (default: RED) |
parent | Parent node pointer (default: nullptr) |
left | Left child pointer (default: nullptr) |
right | Right child pointer (default: nullptr) |
Uses allocator to construct node in place
original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::RBNode * original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::createNode | ( | RBNode && | other_node | ) | const |
Creates a new node by moving from another node.
other_node | Node to move from |
Performs move construction of node
Destroys a node and deallocates memory.
node | Node to destroy |
Uses allocator to destroy and deallocate node
|
noexcept |
Destroys entire tree and deallocates all nodes.
Uses breadth-first traversal to destroy all nodes
Erases node with given key.
key | Key to erase |
Automatically balances tree after deletion
original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::RBNode * original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::find | ( | const K_TYPE & | key | ) | const |
Finds node with given key.
key | Key to search for |
Uses binary search tree traversal
original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::RBNode * original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::getMaxNode | ( | ) | const |
Gets the maximum node in the tree.
Traverses right children until reaching the rightmost node
original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::RBNode * original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::getMinNode | ( | ) | const |
Gets the minimum node in the tree.
Traverses left children until reaching the leftmost node
original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::RBNode * original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::getPrecursorNode | ( | RBNode * | cur | ) | const |
Gets precursor node (in-order predecessor)
cur | Current node |
original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::RBNode * original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::getSuccessorNode | ( | RBNode * | cur | ) | const |
Gets successor node (in-order successor)
cur | Current node |
bool original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::highPriority | ( | const K_TYPE & | key, |
RBNode * | other | ||
) | const |
Compares priority between a key and a node.
key | Key to compare |
other | Node to compare |
Uses the tree's comparison function
bool original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::highPriority | ( | RBNode * | cur, |
RBNode * | other | ||
) | const |
Compares priority between two nodes.
cur | First node to compare |
other | Second node to compare |
Uses the tree's comparison function
bool original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::insert | ( | const K_TYPE & | key, |
const V_TYPE & | value | ||
) |
Inserts new key-value pair.
key | Key to insert |
value | Value to insert |
Automatically balances tree after insertion
bool original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::modify | ( | const K_TYPE & | key, |
const V_TYPE & | value | ||
) |
Modifies value for existing key.
key | Key to modify |
value | New value to set |
original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::RBNode * original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::replaceNode | ( | RBNode * | src, |
RBNode * | tar | ||
) |
Replaces one node with another while maintaining tree structure.
src | Source node to replace with |
tar | Target node to be replaced |
Preserves all connections and colors during replacement
original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::RBNode * original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::rotateLeft | ( | RBNode * | cur | ) |
Performs left rotation around a node.
cur | Node to rotate around |
Maintains tree properties during rotation
original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::RBNode * original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::rotateRight | ( | RBNode * | cur | ) |
Performs right rotation around a node.
cur | Node to rotate around |
Maintains tree properties during rotation
original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::RBNode * original::RBTree< K_TYPE, V_TYPE, ALLOC, Compare >::treeCopy | ( | ) | const |
Creates a deep copy of the tree.