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

Hybrid top-down/bottom-up red-black tree with rank support. More...

#include <tpl_hRbTreeRk.H>

Inheritance diagram for Aleph::HtdRbTreeRk< Key, Compare >:
[legend]
Collaboration diagram for Aleph::HtdRbTreeRk< Key, Compare >:
[legend]

Classes

struct  Iterator
 In-order iterator. More...
 

Public Types

using Color = unsigned char
 
using Node = RbNodeRk< Key >
 
using key_type = Key
 

Public Member Functions

Compare & key_comp () noexcept
 
const Compare & key_comp () const noexcept
 
Compare & get_compare () noexcept
 
constexpr const Compare & get_compare () const noexcept
 
 HtdRbTreeRk (Compare __cmp=Compare()) noexcept
 
void swap (HtdRbTreeRk &tree) noexcept
 
 HtdRbTreeRk (HtdRbTreeRk &&tree) noexcept
 
HtdRbTreeRkoperator= (HtdRbTreeRk &&tree) noexcept
 
 HtdRbTreeRk (const HtdRbTreeRk &)=delete
 
HtdRbTreeRkoperator= (const HtdRbTreeRk &)=delete
 
virtual ~HtdRbTreeRk ()=default
 
constexpr bool is_empty () const noexcept
 
size_t size () const noexcept
 O(1) size using root count.
 
void reset () noexcept
 
Nodeinsert (Node *p) noexcept
 
Nodesearch_or_insert (Node *p) noexcept
 
Nodeinsert_dup (Node *p) noexcept
 
Nodesearch (const Key &key) const noexcept
 
Noderemove (const Key &key) noexcept
 
Node *& getRoot () noexcept
 
NodegetRoot () const noexcept
 
Nodeselect (size_t i) const
 Select the i-th smallest element (0-indexed).
 
std::pair< long, Node * > position (const Key &key) const noexcept
 Find position of a key.
 
std::pair< long, Node * > find_position (const Key &key) const noexcept
 Find position where key would be inserted.
 
Noderemove_pos (size_t i)
 Remove element at position i.
 
void split_pos (size_t pos, HtdRbTreeRk &t1, HtdRbTreeRk &t2)
 Split tree at position.
 
bool verify () const noexcept
 Verify red-black and rank invariants.
 

Private Member Functions

bool less (const Key &k1, const Key &k2) const noexcept
 
bool equals (const Key &k1, const Key &k2) const noexcept
 
void restoreRedCondition (Node *p, Node *&fp, Node *ffp, Node *fffp) noexcept
 
NodesearchFlipColorsAndInsert (Node *q) noexcept
 Insert with stack tracking for count updates.
 
NodesearchFlipColorsAndInsertDup (Node *q) noexcept
 
NodesearchAndBuildPath (const Key &key) noexcept
 
void findSuccAndSwap (Node *p, Node *&fp) noexcept
 
void balanceDownAndColor (Node *p, Node *&fp, Node *&sp) noexcept
 
void rotateNephewAndColor (Node *fp, Node *sp, Node *np) noexcept
 
void doubleRotateNephewAndColor (Node *fp, Node *sp, Node *snp) noexcept
 
void removeAndFixBlackCondition (Node *q) noexcept
 Remove node and fix red-black violations.
 
void init () noexcept
 

Static Private Member Functions

static NodegetSibling (Node *p, Node *fp) noexcept
 
static Noderotate_to_right_rk (Node *p, Node *fp) noexcept
 Right rotation with count update.
 
static Noderotate_to_left_rk (Node *p, Node *fp) noexcept
 Left rotation with count update.
 
static void flipColors (Node *p) noexcept
 
static void colorSiblingAsRed (Node *sp) noexcept
 
static void colorParentAndSibling (Node *fp, Node *sp) noexcept
 
static void updateCountRec (Node *p) noexcept
 Recursively update counts for entire subtree - O(n)
 
static bool verifyCountsRec (Node *p) noexcept
 

Private Attributes

Node headNode
 
Node headParent
 
Node headGrandParent
 
Nodehead
 
NodefHead
 
NodeffHead
 
Node *& root
 
FixedStack< Node * > path {Node::MaxHeight}
 
Compare cmp
 

Detailed Description

template<class Key, class Compare = Aleph::less<Key>>
class Aleph::HtdRbTreeRk< Key, Compare >

Hybrid top-down/bottom-up red-black tree with rank support.

This class implements a self-balancing binary search tree with O(log n) rank operations (select, position). It combines:

  • Insertion: Top-down with proactive color flipping and stack tracking
  • Deletion: Bottom-up using a stack to track ancestors
  • Rank operations: Subtree counts maintained during all operations
Complexity (ALL O(log n)):
  • search, insert, remove: O(log n)
  • select(i): O(log n) - get i-th smallest element
  • position(key): O(log n) - get rank of a key
  • find_position(key): O(log n) - get insertion position
  • size(): O(1)

This is the only implementation that achieves O(log n) for ALL operations including removal with rank support.

Template Parameters
KeyThe type of keys stored in the tree
CompareComparison functor (default: Aleph::less<Key>)
See also
HtdRbTree Hybrid tree without rank support
Gen_Rb_Tree_Rk Bottom-up tree with rank
GenTdRbTreeRk Top-down tree with rank (O(n) remove)
Author
Leandro Rabindranath León

Definition at line 92 of file tpl_hRbTreeRk.H.

Member Typedef Documentation

◆ Color

template<class Key , class Compare = Aleph::less<Key>>
using Aleph::HtdRbTreeRk< Key, Compare >::Color = unsigned char

Definition at line 95 of file tpl_hRbTreeRk.H.

◆ key_type

template<class Key , class Compare = Aleph::less<Key>>
using Aleph::HtdRbTreeRk< Key, Compare >::key_type = Key

Definition at line 714 of file tpl_hRbTreeRk.H.

◆ Node

template<class Key , class Compare = Aleph::less<Key>>
using Aleph::HtdRbTreeRk< Key, Compare >::Node = RbNodeRk<Key>

Definition at line 96 of file tpl_hRbTreeRk.H.

Constructor & Destructor Documentation

◆ HtdRbTreeRk() [1/3]

template<class Key , class Compare = Aleph::less<Key>>
Aleph::HtdRbTreeRk< Key, Compare >::HtdRbTreeRk ( Compare  __cmp = Compare())
inlinenoexcept

Definition at line 721 of file tpl_hRbTreeRk.H.

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

◆ HtdRbTreeRk() [2/3]

template<class Key , class Compare = Aleph::less<Key>>
Aleph::HtdRbTreeRk< Key, Compare >::HtdRbTreeRk ( HtdRbTreeRk< Key, Compare > &&  tree)
inlinenoexcept

◆ HtdRbTreeRk() [3/3]

template<class Key , class Compare = Aleph::less<Key>>
Aleph::HtdRbTreeRk< Key, Compare >::HtdRbTreeRk ( const HtdRbTreeRk< Key, Compare > &  )
delete

◆ ~HtdRbTreeRk()

template<class Key , class Compare = Aleph::less<Key>>
virtual Aleph::HtdRbTreeRk< Key, Compare >::~HtdRbTreeRk ( )
virtualdefault

Member Function Documentation

◆ balanceDownAndColor()

◆ colorParentAndSibling()

template<class Key , class Compare = Aleph::less<Key>>
static void Aleph::HtdRbTreeRk< Key, Compare >::colorParentAndSibling ( Node fp,
Node sp 
)
inlinestaticprivatenoexcept

◆ colorSiblingAsRed()

template<class Key , class Compare = Aleph::less<Key>>
static void Aleph::HtdRbTreeRk< Key, Compare >::colorSiblingAsRed ( Node sp)
inlinestaticprivatenoexcept

◆ doubleRotateNephewAndColor()

◆ equals()

◆ find_position()

template<class Key , class Compare = Aleph::less<Key>>
std::pair< long, Node * > Aleph::HtdRbTreeRk< Key, Compare >::find_position ( const Key &  key) const
inlinenoexcept

Find position where key would be inserted.

Works even if key doesn't exist in the tree.

Parameters
keyKey to find position for
Returns
Pair of (position, node). Node is nullptr if key not found.

Definition at line 886 of file tpl_hRbTreeRk.H.

References Aleph::HtdRbTreeRk< Key, Compare >::cmp, Aleph::HtdRbTreeRk< Key, Compare >::find_position(), Aleph::maps(), and Aleph::HtdRbTreeRk< Key, Compare >::root.

Referenced by Aleph::HtdRbTreeRk< Key, Compare >::find_position().

◆ findSuccAndSwap()

template<class Key , class Compare = Aleph::less<Key>>
void Aleph::HtdRbTreeRk< Key, Compare >::findSuccAndSwap ( Node p,
Node *&  fp 
)
inlineprivatenoexcept

◆ flipColors()

template<class Key , class Compare = Aleph::less<Key>>
static void Aleph::HtdRbTreeRk< Key, Compare >::flipColors ( Node p)
inlinestaticprivatenoexcept

◆ get_compare() [1/2]

template<class Key , class Compare = Aleph::less<Key>>
constexpr const Compare & Aleph::HtdRbTreeRk< Key, Compare >::get_compare ( ) const
inlineconstexprnoexcept

Definition at line 719 of file tpl_hRbTreeRk.H.

References Aleph::HtdRbTreeRk< Key, Compare >::cmp.

◆ get_compare() [2/2]

template<class Key , class Compare = Aleph::less<Key>>
Compare & Aleph::HtdRbTreeRk< Key, Compare >::get_compare ( )
inlinenoexcept

Definition at line 718 of file tpl_hRbTreeRk.H.

References Aleph::HtdRbTreeRk< Key, Compare >::cmp.

◆ getRoot() [1/2]

template<class Key , class Compare = Aleph::less<Key>>
Node * Aleph::HtdRbTreeRk< Key, Compare >::getRoot ( ) const
inlinenoexcept

Definition at line 851 of file tpl_hRbTreeRk.H.

References Aleph::HtdRbTreeRk< Key, Compare >::root.

◆ getRoot() [2/2]

template<class Key , class Compare = Aleph::less<Key>>
Node *& Aleph::HtdRbTreeRk< Key, Compare >::getRoot ( )
inlinenoexcept

Definition at line 850 of file tpl_hRbTreeRk.H.

References Aleph::HtdRbTreeRk< Key, Compare >::root.

◆ getSibling()

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

◆ init()

◆ insert()

◆ insert_dup()

template<class Key , class Compare = Aleph::less<Key>>
Node * Aleph::HtdRbTreeRk< Key, Compare >::insert_dup ( Node p)
inlinenoexcept

◆ is_empty()

template<class Key , class Compare = Aleph::less<Key>>
constexpr bool Aleph::HtdRbTreeRk< Key, Compare >::is_empty ( ) const
inlineconstexprnoexcept

◆ key_comp() [1/2]

template<class Key , class Compare = Aleph::less<Key>>
const Compare & Aleph::HtdRbTreeRk< Key, Compare >::key_comp ( ) const
inlinenoexcept

Definition at line 717 of file tpl_hRbTreeRk.H.

References Aleph::HtdRbTreeRk< Key, Compare >::cmp.

◆ key_comp() [2/2]

template<class Key , class Compare = Aleph::less<Key>>
Compare & Aleph::HtdRbTreeRk< Key, Compare >::key_comp ( )
inlinenoexcept

Definition at line 716 of file tpl_hRbTreeRk.H.

References Aleph::HtdRbTreeRk< Key, Compare >::cmp.

◆ less()

template<class Key , class Compare = Aleph::less<Key>>
bool Aleph::HtdRbTreeRk< Key, Compare >::less ( const Key &  k1,
const Key &  k2 
) const
inlineprivatenoexcept

Definition at line 112 of file tpl_hRbTreeRk.H.

References Aleph::HtdRbTreeRk< Key, Compare >::cmp, and Aleph::maps().

◆ operator=() [1/2]

template<class Key , class Compare = Aleph::less<Key>>
HtdRbTreeRk & Aleph::HtdRbTreeRk< Key, Compare >::operator= ( const HtdRbTreeRk< Key, Compare > &  )
delete

◆ operator=() [2/2]

template<class Key , class Compare = Aleph::less<Key>>
HtdRbTreeRk & Aleph::HtdRbTreeRk< Key, Compare >::operator= ( HtdRbTreeRk< Key, Compare > &&  tree)
inlinenoexcept

Definition at line 749 of file tpl_hRbTreeRk.H.

References Aleph::HtdRbTreeRk< Key, Compare >::swap().

◆ position()

template<class Key , class Compare = Aleph::less<Key>>
std::pair< long, Node * > Aleph::HtdRbTreeRk< Key, Compare >::position ( const Key &  key) const
inlinenoexcept

Find position of a key.

Parameters
keyKey to find
Returns
Pair of (position, node). Position is -1 if not found.

Definition at line 871 of file tpl_hRbTreeRk.H.

References Aleph::HtdRbTreeRk< Key, Compare >::cmp, Aleph::inorder_position(), Aleph::maps(), and Aleph::HtdRbTreeRk< Key, Compare >::root.

◆ remove()

◆ remove_pos()

template<class Key , class Compare = Aleph::less<Key>>
Node * Aleph::HtdRbTreeRk< Key, Compare >::remove_pos ( size_t  i)
inline

Remove element at position i.

Parameters
iPosition (0-indexed)
Returns
Removed node
Exceptions
std::out_of_rangeif i >= size()

Definition at line 900 of file tpl_hRbTreeRk.H.

References ah_out_of_range_error_if, KEY, Aleph::HtdRbTreeRk< Key, Compare >::remove(), Aleph::HtdRbTreeRk< Key, Compare >::select(), and Aleph::HtdRbTreeRk< Key, Compare >::size().

Referenced by Aleph::HtdRbTreeRk< Key, Compare >::split_pos().

◆ removeAndFixBlackCondition()

◆ reset()

template<class Key , class Compare = Aleph::less<Key>>
void Aleph::HtdRbTreeRk< Key, Compare >::reset ( )
inlinenoexcept

◆ restoreRedCondition()

◆ rotate_to_left_rk()

◆ rotate_to_right_rk()

template<class Key , class Compare = Aleph::less<Key>>
static Node * Aleph::HtdRbTreeRk< Key, Compare >::rotate_to_right_rk ( Node p,
Node fp 
)
inlinestaticprivatenoexcept

◆ rotateNephewAndColor()

◆ search()

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

◆ search_or_insert()

template<class Key , class Compare = Aleph::less<Key>>
Node * Aleph::HtdRbTreeRk< Key, Compare >::search_or_insert ( Node p)
inlinenoexcept

◆ searchAndBuildPath()

◆ searchFlipColorsAndInsert()

template<class Key , class Compare = Aleph::less<Key>>
Node * Aleph::HtdRbTreeRk< Key, Compare >::searchFlipColorsAndInsert ( Node q)
inlineprivatenoexcept

◆ searchFlipColorsAndInsertDup()

◆ select()

template<class Key , class Compare = Aleph::less<Key>>
Node * Aleph::HtdRbTreeRk< Key, Compare >::select ( size_t  i) const
inline

Select the i-th smallest element (0-indexed).

Parameters
iPosition (0 = smallest)
Returns
Pointer to node at position i
Exceptions
std::out_of_rangeif i >= size()

Definition at line 861 of file tpl_hRbTreeRk.H.

References Aleph::HtdRbTreeRk< Key, Compare >::root, and Aleph::select().

Referenced by Aleph::HtdRbTreeRk< Key, Compare >::remove_pos().

◆ size()

template<class Key , class Compare = Aleph::less<Key>>
size_t Aleph::HtdRbTreeRk< Key, Compare >::size ( ) const
inlinenoexcept

◆ split_pos()

template<class Key , class Compare = Aleph::less<Key>>
void Aleph::HtdRbTreeRk< Key, Compare >::split_pos ( size_t  pos,
HtdRbTreeRk< Key, Compare > &  t1,
HtdRbTreeRk< Key, Compare > &  t2 
)
inline

Split tree at position.

After split:

  • t1 contains elements at positions [0, pos)
  • t2 contains elements at positions [pos, size())
  • this tree becomes empty
Parameters
posSplit position
t1Tree for left part
t2Tree for right part

Definition at line 918 of file tpl_hRbTreeRk.H.

References ah_out_of_range_error_if, Aleph::DynList< T >::insert(), Aleph::maps(), Aleph::RbNodeRk< Key >::NullPtr, Aleph::HtdRbTreeRk< Key, Compare >::remove_pos(), Aleph::HTList::reset(), Aleph::HtdRbTreeRk< Key, Compare >::root, Aleph::HTList::size(), and Aleph::HtdRbTreeRk< Key, Compare >::size().

◆ swap()

template<class Key , class Compare = Aleph::less<Key>>
void Aleph::HtdRbTreeRk< Key, Compare >::swap ( HtdRbTreeRk< Key, Compare > &  tree)
inlinenoexcept

◆ updateCountRec()

template<class Key , class Compare = Aleph::less<Key>>
static void Aleph::HtdRbTreeRk< Key, Compare >::updateCountRec ( Node p)
inlinestaticprivatenoexcept

◆ verify()

template<class Key , class Compare = Aleph::less<Key>>
bool Aleph::HtdRbTreeRk< Key, Compare >::verify ( ) const
inlinenoexcept

◆ verifyCountsRec()

template<class Key , class Compare = Aleph::less<Key>>
static bool Aleph::HtdRbTreeRk< Key, Compare >::verifyCountsRec ( Node p)
inlinestaticprivatenoexcept

Member Data Documentation

◆ cmp

◆ ffHead

◆ fHead

◆ head

◆ headGrandParent

template<class Key , class Compare = Aleph::less<Key>>
Node Aleph::HtdRbTreeRk< Key, Compare >::headGrandParent
private

Definition at line 101 of file tpl_hRbTreeRk.H.

◆ headNode

template<class Key , class Compare = Aleph::less<Key>>
Node Aleph::HtdRbTreeRk< Key, Compare >::headNode
private

Definition at line 99 of file tpl_hRbTreeRk.H.

◆ headParent

template<class Key , class Compare = Aleph::less<Key>>
Node Aleph::HtdRbTreeRk< Key, Compare >::headParent
private

Definition at line 100 of file tpl_hRbTreeRk.H.

◆ path

◆ root


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