LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
swoc::detail::RBNode Struct Reference

#include <RBTree.h>

Inheritance diagram for swoc::detail::RBNode:
Inheritance graph
Collaboration diagram for swoc::detail::RBNode:
Collaboration graph

Classes

struct  Linkage
 Support for IntrusiveDList. More...
 

Public Types

enum class  Color { RED , BLACK }
 Node colors. More...
 
enum class  Direction { NONE , LEFT , RIGHT }
 Directional constants. More...
 
using self_type = RBNode
 self reference type
 

Public Member Functions

 RBNode ()=default
 Default constructor.
 
virtual ~RBNode ()
 Force virtual destructor.
 
self_typechild_at (Direction d) const
 
Direction direction_of (self_type *const &n) const
 
Color color () const
 
self_typeleft_most_descendant () const
 
Direction flip (Direction d)
 
int validate ()
 
self_typerotate (Direction dir)
 
self_typeset_child (self_type *child, Direction dir)
 
self_typeremove ()
 
void clear_child (Direction dir)
 

Subclass hook methods

Color _color {Color::RED}
 node color
 
self_type_parent {nullptr}
 parent node (needed for rotations)
 
self_type_left {nullptr}
 left child
 
self_type_right {nullptr}
 right child
 
self_type_next {nullptr}
 Next node.
 
self_type_prev {nullptr}
 Previous node.
 
virtual void structure_fixup ()
 
virtual bool structure_validate ()
 
void replace_with (self_type *n)
 
self_typerebalance_after_insert ()
 Rebalance the tree starting at this node.
 
self_typerebalance_after_remove (Color c, Direction dir)
 
self_typeripple_structure_fixup ()
 
static self_typebuildTree (self_type *&head, int n)
 Recursively build a balanced RB tree from a sorted linked list.
 
static self_typebuildTree (self_type *&head, int n, bool isBlack)
 Called by buildTree above.
 
static void printTree (self_type *root, std::string indent="", bool last=true)
 Recursively print the tree in a human-readable format.
 

Detailed Description

A node in a red/black tree.

This class provides only the basic tree operations. The container must provide the search and decision logic. This enables this class to be a base class for templated nodes with much less code duplication.

Definition at line 26 of file RBTree.h.

Member Typedef Documentation

◆ self_type

self reference type

Definition at line 27 of file RBTree.h.

Member Enumeration Documentation

◆ Color

enum class swoc::detail::RBNode::Color
strong

Node colors.

Definition at line 30 of file RBTree.h.

◆ Direction

Directional constants.

Definition at line 36 of file RBTree.h.

Constructor & Destructor Documentation

◆ ~RBNode()

virtual swoc::detail::RBNode::~RBNode ( )
inlinevirtual

Force virtual destructor.

Definition at line 46 of file RBTree.h.

Member Function Documentation

◆ buildTree() [1/2]

RBNode * swoc::detail::RBNode::buildTree ( self_type *& head,
int n )
static

Recursively build a balanced RB tree from a sorted linked list.

Constraints:

  • The list is sorted in ascending order.
  • A copy (not reference) of the head pointer is passed in.
Parameters
headCopy of the head pointer of the list.
nNumber of nodes being processed at the current level.
Returns
self_type* The root node of the tree.

Definition at line 299 of file RBTree.cc.

◆ buildTree() [2/2]

RBNode * swoc::detail::RBNode::buildTree ( self_type *& head,
int n,
bool isBlack )
static

Called by buildTree above.

Definition at line 314 of file RBTree.cc.

◆ child_at()

RBNode * swoc::detail::RBNode::child_at ( Direction d) const

Get the child in direction dir.

Parameters
dChild direction.
Returns
A pointer to the child node, or nullptr if no child in that direction.

Definition at line 32 of file RBTree.cc.

◆ clear_child()

void swoc::detail::RBNode::clear_child ( RBNode::Direction dir)
inline

Remove the dir child.

Parameters
dirDirection of child to remove.

The pointers in this and the child are updated as needed. It is not an error if there is no child in the direction dir.

Definition at line 245 of file RBTree.h.

◆ color()

RBNode::Color swoc::detail::RBNode::color ( ) const
inline
Returns
The color of the node.

Definition at line 235 of file RBTree.h.

◆ direction_of()

auto swoc::detail::RBNode::direction_of ( self_type *const & n) const
inline

Get the direction of a child node n.

Parameters
nChild node.
Returns
Direction of the child if n is a child, Direction::NONE if not.

Definition at line 230 of file RBTree.h.

◆ flip()

RBNode::Direction swoc::detail::RBNode::flip ( RBNode::Direction d)
inline

Reverse a direction

Returns
LEFT if d is RIGHT, RIGHT if d is LEFT, NONE otherwise.

Definition at line 240 of file RBTree.h.

◆ left_most_descendant()

auto swoc::detail::RBNode::left_most_descendant ( ) const
Returns
The left most descendant of this, or null ptr if no left child.

Definition at line 439 of file RBTree.cc.

◆ printTree()

void swoc::detail::RBNode::printTree ( self_type * root,
std::string indent = "",
bool last = true )
static

Recursively print the tree in a human-readable format.

Parameters
rootRoot node of the tree.
indentIndentation string.
lastFlag to indicate if the node is the last node.

Definition at line 371 of file RBTree.cc.

◆ rebalance_after_insert()

RBNode * swoc::detail::RBNode::rebalance_after_insert ( )

Rebalance the tree starting at this node.

The tree is rebalanced so that all of the invariants are true. The (potentially new) root of the tree is returned.

Returns
The root node of the tree after the rebalance.

Definition at line 110 of file RBTree.cc.

◆ rebalance_after_remove()

RBNode * swoc::detail::RBNode::rebalance_after_remove ( Color c,
Direction d )

Rebalance the tree after a deletion. Called on the lowest modified node.

Returns
The new root of the tree. Rebalance the tree root at this node after a removal.
Parameters
cThe color of the removed node.
dThe direction of the removed node from this.
Returns
The new root node.

Invariant: this is the parent of the removed node.

Rebalance tree after a deletion Called on the spliced in node or its parent, whichever is not NIL. This modifies the tree structure only if c is BLACK.

Parameters
cThe color of the removed node
dDirection of removed node from its parent

Definition at line 231 of file RBTree.cc.

◆ remove()

RBNode * swoc::detail::RBNode::remove ( )

Remove this node from the tree. The tree is rebalanced after removal.

Returns
The new root node.

Definition at line 152 of file RBTree.cc.

◆ replace_with()

void swoc::detail::RBNode::replace_with ( self_type * n)

Replace this node with n.

Parameters
nReplacement node.

Invariant: this is a non-order modifying replacement.

Definition at line 87 of file RBTree.cc.

◆ ripple_structure_fixup()

RBNode * swoc::detail::RBNode::ripple_structure_fixup ( )

Invoke structure_fixup on this node and its parents to the root.

Returns
The root node.
Note
This is used by the base class to inform subclasses of structure changes, starting from a leaf node up to the root node.

Definition at line 75 of file RBTree.cc.

◆ rotate()

RBNode * swoc::detail::RBNode::rotate ( Direction dir)

Rotate the subtree rooted at this node in the direction dir.

Parameters
dirDirection of rotatoin.
Returns
The root node after rotation.

this node is rotated in to the position of its dir child and the child in the other direction becomes the new root node for the subtree. This operation is a no-op if there is no other child.

If this node has a parent then that parent's child pointer is updated as needed.

Definition at line 37 of file RBTree.cc.

◆ set_child()

RBNode * swoc::detail::RBNode::set_child ( self_type * child,
Direction dir )

Set the dir child of this node to child.

Parameters
childNode to set as the child.
dirDirection of the child location.
Returns
child

The internal pointers in both this and child are updated as needed. It is an error to call this if the specified child is already set.

Definition at line 62 of file RBTree.cc.

◆ structure_fixup()

virtual void swoc::detail::RBNode::structure_fixup ( )
inlinevirtual

Structural change notification.

This method is called if the structure of the subtree rooted at this node was changed. This makes it possible to keep subtree information nodes synchronized with the state of the tree efficiently.

This is intended a hook. The base method is empty so that subclasses are not required to override.

Reimplemented in swoc::DiscreteSpace< METRIC, PAYLOAD >::Node.

Definition at line 132 of file RBTree.h.

◆ structure_validate()

bool swoc::detail::RBNode::structure_validate ( )
inlinevirtual

Called from validate to perform any additional validation checks. Clients should chain this if they wish to perform additional checks.

Returns
true if the validation is successful, false otherwise.

Definition at line 253 of file RBTree.h.

◆ validate()

int swoc::detail::RBNode::validate ( )

Perform internal validation checks.

Returns
0 on failure, black height of the tree on success.

Ensure that the local information associated with each node is correct globally This should only be called on debug builds as it breaks any efficiencies we have gained from our tree structure.

Definition at line 398 of file RBTree.cc.

Member Data Documentation

◆ _color

Color swoc::detail::RBNode::_color {Color::RED}

node color

Definition at line 205 of file RBTree.h.

◆ _left

self_type* swoc::detail::RBNode::_left {nullptr}

left child

Definition at line 207 of file RBTree.h.

◆ _next

self_type* swoc::detail::RBNode::_next {nullptr}

Next node.

Definition at line 209 of file RBTree.h.

◆ _parent

self_type* swoc::detail::RBNode::_parent {nullptr}

parent node (needed for rotations)

Definition at line 206 of file RBTree.h.

◆ _prev

self_type* swoc::detail::RBNode::_prev {nullptr}

Previous node.

Definition at line 210 of file RBTree.h.

◆ _right

self_type* swoc::detail::RBNode::_right {nullptr}

right child

Definition at line 208 of file RBTree.h.


The documentation for this struct was generated from the following files: