Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::GenTdRbTree< NodeType, Key, Compare > Class Template Reference

Top-down red-black binary search tree implementation. More...

#include <tpl_tdRbTree.H>

Inheritance diagram for Aleph::GenTdRbTree< NodeType, Key, Compare >:
[legend]

Classes

struct  Iterator
 Iterator over tree nodes in sorted order. More...
 

Public Types

using Node = NodeType< Key >
 
using key_type = Key
 
using compare_type = Compare
 

Public Member Functions

 GenTdRbTree () noexcept
 Default constructor.
 
 GenTdRbTree (const Compare &__cmp) noexcept
 Constructor with comparator.
 
 GenTdRbTree (GenTdRbTree &&other) noexcept
 Move constructor.
 
GenTdRbTreeoperator= (GenTdRbTree &&other) noexcept
 Move assignment operator.
 
 GenTdRbTree (const GenTdRbTree &)=delete
 Deleted copy constructor (use explicit copy if needed)
 
GenTdRbTreeoperator= (const GenTdRbTree &)=delete
 Deleted copy assignment.
 
void reset () noexcept
 Reset tree to empty state (does not free nodes)
 
void swap (GenTdRbTree &other) noexcept
 Swap contents with another tree.
 
virtual ~GenTdRbTree ()=default
 
size_t size () const noexcept
 Get number of nodes in tree (O(1))
 
bool is_empty () const noexcept
 Check if tree is empty.
 
Compare & get_compare () noexcept
 Get reference to comparator.
 
const Compare & get_compare () const noexcept
 
Nodeinsert (Node *p) noexcept
 Insert a node into the tree.
 
Nodeinsert_dup (Node *p) noexcept
 Insert a node, allowing duplicates.
 
Nodesearch_or_insert (Node *p) noexcept
 Search for key or insert if not found.
 
Nodesearch (const Key &key) const noexcept
 Search for a key in the tree.
 
Noderemove (const Key &key) noexcept
 Remove and return node with given key.
 
Node *& getRoot () noexcept
 Get reference to root pointer.
 
NodegetRoot () const noexcept
 Get const root pointer

 
bool verifyRedBlack () const noexcept
 Verify that tree satisfies all red-black properties.
 
bool verify () const noexcept
 Alias for verify.
 

Private Member Functions

bool less (const Key &a, const Key &b) const noexcept
 Returns true if a < b according to comparator.
 
bool equals (const Key &a, const Key &b) const noexcept
 Returns true if a == b (neither a < b nor b < a)
 
void restoreRedCondition (Node *p, Node *&fp, Node *ffp, Node *fffp) noexcept
 Restore red-black property after color flip creates two consecutive reds.
 
NodesearchFlipColorsAndInsert (Node *q) noexcept
 Search for insertion point, flipping colors proactively to maintain balance.
 
NodesearchFlipColorsAndInsertDup (Node *q) noexcept
 Search for insertion point allowing duplicates.
 
void colorRootAsRed () noexcept
 Color root red if safe (both children black)
 
NodesearchAndColorRed (const Key &key, Node *&fp) noexcept
 Search for key while coloring nodes red for deletion.
 
void init () noexcept
 Initialize header nodes.
 

Static Private Member Functions

static NodegetSibling (Node *p, Node *fp) noexcept
 Get sibling of node p whose parent is fp.
 
static void flipColors (Node *p) noexcept
 Flip colors: black parent with two red children becomes red with two black children.
 
static NodegotoLeftAndColorRed (Node *fp, Node *&ffp) noexcept
 Descend to left child, ensuring current node is red for deletion.
 
static NodegotoRightAndColorRed (Node *fp, Node *&ffp) noexcept
 Descend to right child, ensuring current node is red for deletion.
 
static void findSuccAndSwap (Node *p, Node *&fp) noexcept
 Find successor and swap with node p for deletion.
 
static void findPredAndSwap (Node *p, Node *&fp) noexcept
 Find predecessor and swap with node p for deletion.
 
static void removeAndRendLeafRed (Node *p, Node *fp) noexcept
 Remove node p and ensure it becomes a red leaf.
 
static bool testBlackHeight (Node *p, int &max, int bh=0) noexcept
 Test black height property recursively.
 
static bool testNode (Node *p) noexcept
 Test red-black properties for a single node.
 
static bool verify (Node *p) noexcept
 Recursively verify red-black properties.
 

Private Attributes

Node headNode
 Sentinel header node (parent of root)
 
Node headParent
 Sentinel grandparent node.
 
Nodehead
 Pointer to header.
 
NodefHead
 Pointer to header's parent.
 
Node *& root
 Reference to root (right child of head)
 
Compare cmp
 Comparison functor.
 
size_t num_nodes
 Number of nodes in tree.
 

Detailed Description

template<template< class > class NodeType, typename Key, class Compare = std::less<Key>>
class Aleph::GenTdRbTree< NodeType, Key, Compare >

Top-down red-black binary search tree implementation.

This class implements the top-down (single-pass) red-black tree algorithm as described by Guibas and Sedgewick. Unlike the bottom-up approach that requires backtracking after insertion/deletion, this algorithm performs all necessary rotations and color changes while descending the tree.

Advantages over bottom-up:
  • No stack needed for ancestor path (saves O(log n) space)
  • Better cache locality (single pass through tree)
  • Potentially faster due to reduced memory traffic
Disadvantages:
  • More complex code
  • May perform more color flips than strictly necessary
Complexity:
  • Search: O(log n) worst case
  • Insert: O(log n) worst case, at most 2 rotations
  • Delete: O(log n) worst case, at most 3 rotations
  • Size: O(1)
Template Parameters
NodeTypeNode template (typically RbNode or RbNodeVtl).
KeyThe type of keys stored in the tree.
CompareComparison functor for ordering keys (default: std::less<Key>).
See also
Gen_Rb_Tree Bottom-up red-black tree implementation.
Avl_Tree Alternative balanced tree with stricter balance.

Definition at line 96 of file tpl_tdRbTree.H.

Member Typedef Documentation

◆ compare_type

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
using Aleph::GenTdRbTree< NodeType, Key, Compare >::compare_type = Compare

Definition at line 101 of file tpl_tdRbTree.H.

◆ key_type

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
using Aleph::GenTdRbTree< NodeType, Key, Compare >::key_type = Key

Definition at line 100 of file tpl_tdRbTree.H.

◆ Node

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
using Aleph::GenTdRbTree< NodeType, Key, Compare >::Node = NodeType<Key>

Definition at line 99 of file tpl_tdRbTree.H.

Constructor & Destructor Documentation

◆ GenTdRbTree() [1/4]

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Aleph::GenTdRbTree< NodeType, Key, Compare >::GenTdRbTree ( )
inlinenoexcept

Default constructor.

Definition at line 622 of file tpl_tdRbTree.H.

References Aleph::GenTdRbTree< NodeType, Key, Compare >::init().

◆ GenTdRbTree() [2/4]

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Aleph::GenTdRbTree< NodeType, Key, Compare >::GenTdRbTree ( const Compare &  __cmp)
inlineexplicitnoexcept

Constructor with comparator.

Definition at line 629 of file tpl_tdRbTree.H.

References Aleph::GenTdRbTree< NodeType, Key, Compare >::init().

◆ GenTdRbTree() [3/4]

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Aleph::GenTdRbTree< NodeType, Key, Compare >::GenTdRbTree ( GenTdRbTree< NodeType, Key, Compare > &&  other)
inlinenoexcept

◆ GenTdRbTree() [4/4]

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Aleph::GenTdRbTree< NodeType, Key, Compare >::GenTdRbTree ( const GenTdRbTree< NodeType, Key, Compare > &  )
delete

Deleted copy constructor (use explicit copy if needed)

◆ ~GenTdRbTree()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
virtual Aleph::GenTdRbTree< NodeType, Key, Compare >::~GenTdRbTree ( )
virtualdefault

Member Function Documentation

◆ colorRootAsRed()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
void Aleph::GenTdRbTree< NodeType, Key, Compare >::colorRootAsRed ( )
inlineprivatenoexcept

◆ equals()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
bool Aleph::GenTdRbTree< NodeType, Key, Compare >::equals ( const Key &  a,
const Key &  b 
) const
inlineprivatenoexcept

◆ findPredAndSwap()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
static void Aleph::GenTdRbTree< NodeType, Key, Compare >::findPredAndSwap ( Node p,
Node *&  fp 
)
inlinestaticprivatenoexcept

◆ findSuccAndSwap()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
static void Aleph::GenTdRbTree< NodeType, Key, Compare >::findSuccAndSwap ( Node p,
Node *&  fp 
)
inlinestaticprivatenoexcept

◆ flipColors()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
static void Aleph::GenTdRbTree< NodeType, Key, Compare >::flipColors ( Node p)
inlinestaticprivatenoexcept

Flip colors: black parent with two red children becomes red with two black children.

Definition at line 178 of file tpl_tdRbTree.H.

References BLACK, COLOR, Aleph::LLINK(), Aleph::maps(), RED, and Aleph::RLINK().

Referenced by Aleph::GenTdRbTree< NodeType, Key, Compare >::searchFlipColorsAndInsert(), and Aleph::GenTdRbTree< NodeType, Key, Compare >::searchFlipColorsAndInsertDup().

◆ get_compare() [1/2]

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
const Compare & Aleph::GenTdRbTree< NodeType, Key, Compare >::get_compare ( ) const
inlinenoexcept

Definition at line 693 of file tpl_tdRbTree.H.

References Aleph::GenTdRbTree< NodeType, Key, Compare >::cmp.

◆ get_compare() [2/2]

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Compare & Aleph::GenTdRbTree< NodeType, Key, Compare >::get_compare ( )
inlinenoexcept

Get reference to comparator.

Definition at line 692 of file tpl_tdRbTree.H.

References Aleph::GenTdRbTree< NodeType, Key, Compare >::cmp.

◆ getRoot() [1/2]

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Node * Aleph::GenTdRbTree< NodeType, Key, Compare >::getRoot ( ) const
inlinenoexcept

Get const root pointer

Definition at line 807 of file tpl_tdRbTree.H.

References Aleph::GenTdRbTree< NodeType, Key, Compare >::root.

◆ getRoot() [2/2]

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Node *& Aleph::GenTdRbTree< NodeType, Key, Compare >::getRoot ( )
inlinenoexcept

Get reference to root pointer.

Definition at line 804 of file tpl_tdRbTree.H.

References Aleph::GenTdRbTree< NodeType, Key, Compare >::root.

◆ getSibling()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
static Node * Aleph::GenTdRbTree< NodeType, Key, Compare >::getSibling ( Node p,
Node fp 
)
inlinestaticprivatenoexcept

Get sibling of node p whose parent is fp.

Definition at line 125 of file tpl_tdRbTree.H.

References Aleph::LLINK(), Aleph::maps(), and Aleph::RLINK().

◆ gotoLeftAndColorRed()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
static Node * Aleph::GenTdRbTree< NodeType, Key, Compare >::gotoLeftAndColorRed ( Node fp,
Node *&  ffp 
)
inlinestaticprivatenoexcept

◆ gotoRightAndColorRed()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
static Node * Aleph::GenTdRbTree< NodeType, Key, Compare >::gotoRightAndColorRed ( Node fp,
Node *&  ffp 
)
inlinestaticprivatenoexcept

◆ init()

◆ insert()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Node * Aleph::GenTdRbTree< NodeType, Key, Compare >::insert ( Node p)
inlinenoexcept

Insert a node into the tree.

Parameters
pNode to insert (must be red with null children).
Returns
Pointer to inserted node, or nullptr if key already exists.

Definition at line 700 of file tpl_tdRbTree.H.

References COLOR, Aleph::LLINK(), Aleph::maps(), Aleph::GenTdRbTree< NodeType, Key, Compare >::num_nodes, RED, Aleph::RLINK(), Aleph::GenTdRbTree< NodeType, Key, Compare >::root, and Aleph::GenTdRbTree< NodeType, Key, Compare >::searchFlipColorsAndInsert().

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ insert_dup()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Node * Aleph::GenTdRbTree< NodeType, Key, Compare >::insert_dup ( Node p)
inlinenoexcept

Insert a node, allowing duplicates.

Parameters
pNode to insert.
Returns
Pointer to inserted node (always succeeds).

Definition at line 721 of file tpl_tdRbTree.H.

References COLOR, Aleph::LLINK(), Aleph::maps(), Aleph::GenTdRbTree< NodeType, Key, Compare >::num_nodes, RED, Aleph::RLINK(), Aleph::GenTdRbTree< NodeType, Key, Compare >::root, and Aleph::GenTdRbTree< NodeType, Key, Compare >::searchFlipColorsAndInsertDup().

Referenced by TEST(), and TEST().

◆ is_empty()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
bool Aleph::GenTdRbTree< NodeType, Key, Compare >::is_empty ( ) const
inlinenoexcept

Check if tree is empty.

Definition at line 689 of file tpl_tdRbTree.H.

References Aleph::GenTdRbTree< NodeType, Key, Compare >::root.

◆ less()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
bool Aleph::GenTdRbTree< NodeType, Key, Compare >::less ( const Key &  a,
const Key &  b 
) const
inlineprivatenoexcept

Returns true if a < b according to comparator.

Definition at line 113 of file tpl_tdRbTree.H.

References Aleph::GenTdRbTree< NodeType, Key, Compare >::cmp.

◆ operator=() [1/2]

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
GenTdRbTree & Aleph::GenTdRbTree< NodeType, Key, Compare >::operator= ( const GenTdRbTree< NodeType, Key, Compare > &  )
delete

Deleted copy assignment.

◆ operator=() [2/2]

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
GenTdRbTree & Aleph::GenTdRbTree< NodeType, Key, Compare >::operator= ( GenTdRbTree< NodeType, Key, Compare > &&  other)
inlinenoexcept

◆ remove()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Node * Aleph::GenTdRbTree< NodeType, Key, Compare >::remove ( const Key &  key)
inlinenoexcept

◆ removeAndRendLeafRed()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
static void Aleph::GenTdRbTree< NodeType, Key, Compare >::removeAndRendLeafRed ( Node p,
Node fp 
)
inlinestaticprivatenoexcept

◆ reset()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
void Aleph::GenTdRbTree< NodeType, Key, Compare >::reset ( )
inlinenoexcept

Reset tree to empty state (does not free nodes)

Definition at line 669 of file tpl_tdRbTree.H.

References Aleph::GenTdRbTree< NodeType, Key, Compare >::num_nodes, and Aleph::GenTdRbTree< NodeType, Key, Compare >::root.

◆ restoreRedCondition()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
void Aleph::GenTdRbTree< NodeType, Key, Compare >::restoreRedCondition ( Node p,
Node *&  fp,
Node ffp,
Node fffp 
)
inlineprivatenoexcept

◆ search()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Node * Aleph::GenTdRbTree< NodeType, Key, Compare >::search ( const Key &  key) const
inlinenoexcept

Search for a key in the tree.

Parameters
keyKey to search for.
Returns
Pointer to node containing key, or nullptr if not found.

Definition at line 769 of file tpl_tdRbTree.H.

References Aleph::GenTdRbTree< NodeType, Key, Compare >::equals(), KEY, Aleph::LLINK(), Aleph::RLINK(), and Aleph::GenTdRbTree< NodeType, Key, Compare >::root.

Referenced by Aleph::GenTdRbTree< NodeType, Key, Compare >::search_or_insert(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ search_or_insert()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Node * Aleph::GenTdRbTree< NodeType, Key, Compare >::search_or_insert ( Node p)
inlinenoexcept

Search for key or insert if not found.

Parameters
pNode to search or insert.
Returns
Pointer to p if inserted, or pointer to existing node with same key.

Definition at line 742 of file tpl_tdRbTree.H.

References COLOR, KEY, Aleph::LLINK(), Aleph::maps(), Aleph::GenTdRbTree< NodeType, Key, Compare >::num_nodes, RED, Aleph::RLINK(), Aleph::GenTdRbTree< NodeType, Key, Compare >::root, Aleph::GenTdRbTree< NodeType, Key, Compare >::search(), and Aleph::GenTdRbTree< NodeType, Key, Compare >::searchFlipColorsAndInsert().

Referenced by TEST(), and TEST().

◆ searchAndColorRed()

◆ searchFlipColorsAndInsert()

◆ searchFlipColorsAndInsertDup()

◆ size()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
size_t Aleph::GenTdRbTree< NodeType, Key, Compare >::size ( ) const
inlinenoexcept

Get number of nodes in tree (O(1))

Definition at line 686 of file tpl_tdRbTree.H.

References Aleph::GenTdRbTree< NodeType, Key, Compare >::num_nodes.

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ swap()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
void Aleph::GenTdRbTree< NodeType, Key, Compare >::swap ( GenTdRbTree< NodeType, Key, Compare > &  other)
inlinenoexcept

◆ testBlackHeight()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
static bool Aleph::GenTdRbTree< NodeType, Key, Compare >::testBlackHeight ( Node p,
int max,
int  bh = 0 
)
inlinestaticprivatenoexcept

◆ testNode()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
static bool Aleph::GenTdRbTree< NodeType, Key, Compare >::testNode ( Node p)
inlinestaticprivatenoexcept

◆ verify() [1/2]

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
bool Aleph::GenTdRbTree< NodeType, Key, Compare >::verify ( ) const
inlinenoexcept

◆ verify() [2/2]

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
static bool Aleph::GenTdRbTree< NodeType, Key, Compare >::verify ( Node p)
inlinestaticprivatenoexcept

◆ verifyRedBlack()

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
bool Aleph::GenTdRbTree< NodeType, Key, Compare >::verifyRedBlack ( ) const
inlinenoexcept

Verify that tree satisfies all red-black properties.

Returns
true if tree is valid, false otherwise.

Definition at line 862 of file tpl_tdRbTree.H.

References Aleph::GenTdRbTree< NodeType, Key, Compare >::root, and Aleph::GenTdRbTree< NodeType, Key, Compare >::verify().

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Aleph::GenTdRbTree< NodeType, Key, Compare >::verify().

Member Data Documentation

◆ cmp

◆ fHead

◆ head

◆ headNode

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Node Aleph::GenTdRbTree< NodeType, Key, Compare >::headNode
private

Sentinel header node (parent of root)

Definition at line 104 of file tpl_tdRbTree.H.

◆ headParent

template<template< class > class NodeType, typename Key , class Compare = std::less<Key>>
Node Aleph::GenTdRbTree< NodeType, Key, Compare >::headParent
private

Sentinel grandparent node.

Definition at line 105 of file tpl_tdRbTree.H.

◆ num_nodes

◆ root


The documentation for this class was generated from the following file: