Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
rbNode.H File Reference

Red-Black tree node definition and validation utilities. More...

#include <tpl_binNode.H>
Include dependency graph for rbNode.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  RbNode_Data
 Data portion of a Red-Black tree node. More...
 
class  RbNode< Key >
 Declare RbNode type with 128-byte pool allocation. More...
 
class  RbNodeVtl< Key >
 

Macros

#define COLOR(p)   ((p)->getColor())
 Access the color of node p.
 
#define RED   (0)
 Red color constant (newly inserted nodes are red)
 
#define BLACK   (1)
 Black color constant.
 

Typedefs

typedef unsigned char Color
 Color type for RB nodes.
 

Functions

template<class Node >
bool test_black_condition (Node *p, int &max, int bh=0)
 Test the black-height property of an RB tree.
 
template<class Node >
int red_black_height (Node *p) noexcept
 Black height of a red-black subtree, checking all its nodes.
 
template<class Node >
bool is_red_black (Node *node)
 Test all Red-Black tree properties for a node.
 
template<class Node >
bool is_red_black_tree (Node *node)
 Test RB properties for entire subtree.
 
template<class Node , class Compare >
bool is_red_black_bst (Node *node, Compare &cmp)
 Verify that tree is both a valid RB tree and valid BST.
 

Detailed Description

Red-Black tree node definition and validation utilities.

This file defines the RbNode structure for Red-Black trees, including the color attribute (RED or BLACK) and functions to validate RB tree properties.

Red-Black Tree Properties

  1. Every node is either red or black
  2. All leaves (NIL) are black
  3. Red nodes have only black children
  4. All paths from a node to leaves have same black height

The usual definition adds that the root is black. The red-black trees of the library do not force it: the root can be red, as the only node of a tree is. A red root has black children, so the height is still at most 2 log2(n + 1), and the validation functions accept it.

Validation Functions

See also
tpl_rb_tree.H Bottom-up Red-Black tree
tpl_tdRbTree.H Top-down Red-Black tree
Author
Leandro Rabindranath León

Definition in file rbNode.H.

Macro Definition Documentation

◆ BLACK

#define BLACK   (1)

Black color constant.

Definition at line 80 of file rbNode.H.

◆ COLOR

#define COLOR (   p)    ((p)->getColor())

Access the color of node p.

Definition at line 74 of file rbNode.H.

◆ RED

#define RED   (0)

Red color constant (newly inserted nodes are red)

Definition at line 77 of file rbNode.H.

Typedef Documentation

◆ Color

typedef unsigned char Color

Color type for RB nodes.

Definition at line 71 of file rbNode.H.

Function Documentation

◆ is_red_black()

template<class Node >
bool is_red_black ( Node *  node)

Test all Red-Black tree properties for a node.

Validates that:

  • Node color is valid (RED or BLACK)
  • Red nodes don't have red children
  • Black-height is consistent across all paths
Template Parameters
NodeRB node type
Parameters
nodeNode to test
Returns
true if all RB properties are satisfied

Definition at line 203 of file rbNode.H.

References BLACK, COLOR, LLINK, max(), RED, RLINK, and test_black_condition().

Referenced by main().

◆ is_red_black_bst()

template<class Node , class Compare >
bool is_red_black_bst ( Node *  node,
Compare &  cmp 
)

Verify that tree is both a valid RB tree and valid BST.

Combines RB property validation with BST ordering validation.

Template Parameters
NodeRB node type
CompareComparison functor for keys
Parameters
nodeRoot of tree to validate
cmpComparison function object
Returns
true if tree is a valid RB-BST

Definition at line 251 of file rbNode.H.

References cmp(), and is_red_black_tree().

Referenced by Aleph::HtdRbTree< Key, Compare >::verify(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::verify().

◆ is_red_black_tree()

template<class Node >
bool is_red_black_tree ( Node *  node)

Test RB properties for entire subtree.

Validates the RB properties of node and of all its descendants: valid colors, no red node with a red child, and the same number of black nodes on every path down to Node::NullPtr. node itself can be red or black.

Template Parameters
NodeRB node type
Parameters
nodeRoot of subtree to test
Returns
true if entire subtree is a valid RB tree
Note
O(n).

Definition at line 234 of file rbNode.H.

References red_black_height().

Referenced by is_red_black_bst(), Aleph::HtdRbTree< Key, Compare >::verifyRedBlack(), and Aleph::GenTdRbTree< NodeType, Key, Compare >::verifyRedBlack().

◆ red_black_height()

template<class Node >
int red_black_height ( Node *  p)
noexcept

Black height of a red-black subtree, checking all its nodes.

In one traversal, checks every node of the subtree rooted at p: its color is RED or BLACK, a red node has no red child, and every path from p down to Node::NullPtr crosses the same number of black nodes.

Template Parameters
NodeRB node type
Parameters
pRoot of subtree to test
Returns
the number of black nodes of each path, or -1 if a property is violated
Note
O(n).

Definition at line 164 of file rbNode.H.

References BLACK, COLOR, LLINK, RED, red_black_height(), and RLINK.

Referenced by is_red_black_tree(), and red_black_height().

◆ test_black_condition()

template<class Node >
bool test_black_condition ( Node *  p,
int &  max,
int  bh = 0 
)

Test the black-height property of an RB tree.

Verifies that all paths from node p down to Node::NullPtr have the same number of black nodes (black-height).

Template Parameters
NodeRB node type
Parameters
pRoot of subtree to test
maxReference to store/compare black heights (-1 initially)
bhCurrent black height along this path (default 0)
Returns
true if all the paths have equal black height

Definition at line 131 of file rbNode.H.

References BLACK, COLOR, LLINK, max(), RLINK, and test_black_condition().

Referenced by is_red_black(), and test_black_condition().