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

Red-black binary search tree implementation (bottom-up). More...

#include <tpl_rb_tree.H>

Inheritance diagram for Aleph::Gen_Rb_Tree< NodeType, Key, Compare >:
[legend]
Collaboration diagram for Aleph::Gen_Rb_Tree< 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

Compare & key_comp () noexcept
 Returns a reference to the comparison criteria.
 
const Compare & key_comp () const noexcept
 
Compare & get_compare () noexcept
 
const Compare & get_compare () const noexcept
 
 Gen_Rb_Tree (Compare __cmp=Compare()) noexcept
 Default constructor with optional comparator.
 
void swap (Gen_Rb_Tree &tree) noexcept
 Swaps all elements with another tree in constant time.
 
 Gen_Rb_Tree (Gen_Rb_Tree &&tree) noexcept
 Move constructor.
 
Gen_Rb_Treeoperator= (Gen_Rb_Tree &&tree) noexcept
 Move assignment operator.
 
virtual ~Gen_Rb_Tree ()=default
 Destructor.
 
Nodesearch (const Key &key) const noexcept
 Search for a key in the tree.
 
Node *& getRoot () noexcept
 Get reference to root pointer.
 
NodegetRoot () const noexcept
 Get const root pointer.
 
bool is_empty () const noexcept
 Returns true if the tree is empty.
 
size_t size () const noexcept
 Returns the number of nodes in the tree (O(1))
 
void reset () noexcept
 Reset tree to empty state (does not free nodes)
 
Nodeinsert (Node *p) noexcept
 Insert a node into the tree.
 
Nodesearch_or_insert (Node *p) noexcept
 Search for key or insert if not found.
 
Nodeinsert_dup (Node *p) noexcept
 Insert a node allowing duplicates.
 
bool verify () const noexcept
 Verify that tree satisfies red-black properties.
 
Noderemove (const Key &key) noexcept
 Remove the node containing key.
 

Private Member Functions

Nodesearch_and_stack_rb (const Key &key, signed char &cmp_result) noexcept
 Search for key and stack ancestors (optimistic duplicate detection)
 
Nodesearch_dup_and_stack_rb (const Key &key, signed char &cmp_result) noexcept
 Search for insertion point (allows duplicates) and stack ancestors.
 
void fix_red_condition (Node *p) noexcept
 Fix red-red violation after insertion.
 
void find_succ_and_swap (Node *p, Node *&pp) noexcept
 Find successor and swap with node being deleted.
 
void fix_black_condition (Node *p) noexcept
 Fix black-height violation after deletion.
 
void init () noexcept
 Initialize tree state.
 

Private Attributes

Node head_node
 Sentinel header node.
 
Nodehead
 Pointer to sentinel.
 
Node *& root
 Reference to root (right child of head)
 
FixedStack< Node * > rb_stack
 Stack for ancestor path.
 
Compare cmp
 Comparison functor.
 
size_t num_nodes
 Number of nodes in tree.
 

Static Private Attributes

static constexpr signed char CmpLess = -1
 
static constexpr signed char CmpEqual = 0
 
static constexpr signed char CmpGreater = 1
 

Detailed Description

template<template< typename > class NodeType, typename Key, class Compare>
class Aleph::Gen_Rb_Tree< NodeType, Key, Compare >

Red-black binary search tree implementation (bottom-up).

A red-black tree is a self-balancing binary search tree where each node has a color (red or black). The tree maintains balance through five invariants that guarantee the height is at most 2*logâ‚‚(n+1), ensuring O(log n) worst-case time for all operations.

Red-Black Tree Properties:
  1. Every node is either red or black
  2. The root is black
  3. All leaves (null nodes) are black
  4. Red nodes have only black children
  5. Every path from root to leaf has the same number of black nodes

These properties ensure the tree remains approximately balanced, with the longest path at most twice the length of the shortest path.

This is the bottom-up implementation that uses a stack to track ancestors during descent and repairs violations by ascending back to the root.

Template Parameters
NodeTypeNode template (typically RbNode or RbNodeVtl).
KeyThe type of keys stored in the tree.
CompareComparison functor for ordering keys.
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)
  • Space: O(n) + O(log n) for ancestor stack during operations
Example:
tree.insert(new Rb_Tree<int>::Node(42));
tree.insert(new Rb_Tree<int>::Node(17));
tree.insert(new Rb_Tree<int>::Node(99));
auto node = tree.search(42);
if (node != nullptr)
std::cout << "Found: " << node->get_key() << '\n';
tree.remove(42);
Node * insert(Node *p) noexcept
Insert a node into the tree.
Node * search(const Key &key) const noexcept
Search for a key in the tree.
NodeType< Key > Node
Node * remove(const Key &key) noexcept
Remove the node containing key.
Red-black tree with nodes without virtual destructor.
Note
This is a low-level implementation managing raw nodes. For automatic memory management, use DynSetRbTree.
See also
GenTdRbTree Top-down (single-pass) alternative implementation.
DynSetRbTree High-level wrapper with automatic memory management.
Avl_Tree Alternative balanced tree (stricter balance, more rotations).

Definition at line 118 of file tpl_rb_tree.H.

Member Typedef Documentation

◆ compare_type

template<template< typename > class NodeType, typename Key , class Compare >
using Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::compare_type = Compare

Definition at line 123 of file tpl_rb_tree.H.

◆ key_type

template<template< typename > class NodeType, typename Key , class Compare >
using Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::key_type = Key

Definition at line 122 of file tpl_rb_tree.H.

◆ Node

template<template< typename > class NodeType, typename Key , class Compare >
using Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::Node = NodeType<Key>

Definition at line 121 of file tpl_rb_tree.H.

Constructor & Destructor Documentation

◆ Gen_Rb_Tree() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::Gen_Rb_Tree ( Compare  __cmp = Compare())
inlinenoexcept

Default constructor with optional comparator.

Definition at line 423 of file tpl_rb_tree.H.

◆ Gen_Rb_Tree() [2/2]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::Gen_Rb_Tree ( Gen_Rb_Tree< NodeType, Key, Compare > &&  tree)
inlinenoexcept

Move constructor.

Definition at line 441 of file tpl_rb_tree.H.

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

◆ ~Gen_Rb_Tree()

template<template< typename > class NodeType, typename Key , class Compare >
virtual Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::~Gen_Rb_Tree ( )
virtualdefault

Destructor.

Member Function Documentation

◆ find_succ_and_swap()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::find_succ_and_swap ( Node p,
Node *&  pp 
)
inlineprivatenoexcept

◆ fix_black_condition()

◆ fix_red_condition()

◆ get_compare() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
const Compare & Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::get_compare ( ) const
inlinenoexcept

Definition at line 420 of file tpl_rb_tree.H.

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

◆ get_compare() [2/2]

template<template< typename > class NodeType, typename Key , class Compare >
Compare & Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::get_compare ( )
inlinenoexcept

Definition at line 419 of file tpl_rb_tree.H.

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

◆ getRoot() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::getRoot ( ) const
inlinenoexcept

Get const root pointer.

Definition at line 491 of file tpl_rb_tree.H.

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

◆ getRoot() [2/2]

template<template< typename > class NodeType, typename Key , class Compare >
Node *& Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::getRoot ( )
inlinenoexcept

Get reference to root pointer.

Definition at line 488 of file tpl_rb_tree.H.

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

Referenced by write_rb().

◆ init()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::init ( )
inlineprivatenoexcept

Initialize tree state.

Definition at line 408 of file tpl_rb_tree.H.

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

◆ insert()

◆ insert_dup()

◆ is_empty()

template<template< typename > class NodeType, typename Key , class Compare >
bool Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::is_empty ( ) const
inlinenoexcept

Returns true if the tree is empty.

Definition at line 494 of file tpl_rb_tree.H.

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

◆ key_comp() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
const Compare & Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::key_comp ( ) const
inlinenoexcept

Definition at line 416 of file tpl_rb_tree.H.

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

◆ key_comp() [2/2]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::key_comp ( )
inlinenoexcept

Returns a reference to the comparison criteria.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 415 of file tpl_rb_tree.H.

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

◆ operator=()

template<template< typename > class NodeType, typename Key , class Compare >
Gen_Rb_Tree & Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::operator= ( Gen_Rb_Tree< NodeType, Key, Compare > &&  tree)
inlinenoexcept

◆ remove()

◆ reset()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::reset ( )
inlinenoexcept

Reset tree to empty state (does not free nodes)

Definition at line 500 of file tpl_rb_tree.H.

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

◆ search()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rb_Tree< 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 472 of file tpl_rb_tree.H.

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

Referenced by write_rb().

◆ search_and_stack_rb()

◆ search_dup_and_stack_rb()

◆ search_or_insert()

◆ size()

template<template< typename > class NodeType, typename Key , class Compare >
size_t Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::size ( ) const
inlinenoexcept

Returns the number of nodes in the tree (O(1))

Definition at line 497 of file tpl_rb_tree.H.

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

◆ swap()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::swap ( Gen_Rb_Tree< NodeType, Key, Compare > &  tree)
inlinenoexcept

Swaps all elements with another tree in constant time.

Parameters
[in]treethe red-black tree to swap with

Definition at line 433 of file tpl_rb_tree.H.

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

◆ verify()

template<template< typename > class NodeType, typename Key , class Compare >
bool Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::verify ( ) const
inlinenoexcept

Verify that tree satisfies red-black properties.

Definition at line 604 of file tpl_rb_tree.H.

References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp, is_red_black_bst(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.

Member Data Documentation

◆ cmp

◆ CmpEqual

◆ CmpGreater

◆ CmpLess

◆ head

template<template< typename > class NodeType, typename Key , class Compare >
Node* Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::head
private

◆ head_node

template<template< typename > class NodeType, typename Key , class Compare >
Node Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::head_node
private

Sentinel header node.

Definition at line 126 of file tpl_rb_tree.H.

◆ num_nodes

◆ rb_stack

◆ root


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