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... | |
Protected Types | |
using | color = typename RBNode::color |
Color type alias. | |
using | rebind_alloc_node = typename ALLOC::template rebind_alloc<RBNode> |
Rebound allocator type. | |
Protected 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. | |
~RBTree () | |
Destructor. | |
Protected 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 Protected 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:
|
explicitprotected |
Constructs RBTree with given comparison function.
compare | Comparison function to use |
|
protected |
Destructor.
Cleans up all tree nodes and allocated memory
|
protected |
Adjusts tree after deletion to maintain RB properties.
cur | Node where deletion occurred |
Handles color flips and rotations as needed
|
protected |
Adjusts tree after insertion to maintain RB properties.
cur | Newly inserted node |
Handles color flips and rotations as needed
|
protected |
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
|
protected |
Creates a new node by moving from another node.
other_node | Node to move from |
Performs move construction of node
|
protectednoexcept |
Destroys a node and deallocates memory.
node | Node to destroy |
Uses allocator to destroy and deallocate node
|
protectednoexcept |
Destroys entire tree and deallocates all nodes.
Uses breadth-first traversal to destroy all nodes
|
protected |
Erases node with given key.
key | Key to erase |
Automatically balances tree after deletion
|
protected |
Finds node with given key.
key | Key to search for |
Uses binary search tree traversal
|
protected |
Gets the maximum node in the tree.
Traverses right children until reaching the rightmost node
|
protected |
Gets the minimum node in the tree.
Traverses left children until reaching the leftmost node
|
protected |
Gets precursor node (in-order predecessor)
cur | Current node |
|
protected |
Gets successor node (in-order successor)
cur | Current node |
|
protected |
Compares priority between a key and a node.
key | Key to compare |
other | Node to compare |
Uses the tree's comparison function
|
protected |
Compares priority between two nodes.
cur | First node to compare |
other | Second node to compare |
Uses the tree's comparison function
|
protected |
Inserts new key-value pair.
key | Key to insert |
value | Value to insert |
Automatically balances tree after insertion
|
protected |
Modifies value for existing key.
key | Key to modify |
value | New value to set |
|
protected |
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
|
protected |
Performs left rotation around a node.
cur | Node to rotate around |
Maintains tree properties during rotation
|
protected |
Performs right rotation around a node.
cur | Node to rotate around |
Maintains tree properties during rotation
|
protected |
Creates a deep copy of the tree.