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

Red-black tree with rank support (select/position operations). More...

#include <tpl_rbRk.H>

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

Classes

class  Iterator
 Iterator on nodes of the tree. More...
 

Public Types

typedef NodeType< Key > Node
 
typedef Key key_type
 

Public Member Functions

Compare & key_comp () noexcept
 
Compare & get_compare () noexcept
 
 Gen_Rb_Tree_Rk (Compare cmp_arg=Compare())
 
void swap (Gen_Rb_Tree_Rk &tree) noexcept
 
 Gen_Rb_Tree_Rk (Gen_Rb_Tree_Rk &&tree) noexcept
 
Gen_Rb_Tree_Rkoperator= (Gen_Rb_Tree_Rk &&tree) noexcept
 
virtual ~Gen_Rb_Tree_Rk ()=default
 
Nodesearch (const Key &key)
 
Node *& getRoot () noexcept
 
bool is_empty () const noexcept
 
size_t size () const noexcept
 
Nodeinsert (Node *p)
 
Nodesearch_or_insert (Node *p)
 
Nodeinsert_dup (Node *p)
 
bool verify () const
 
Noderemove (const Key &key)
 
Nodeselect (const size_t i) const
 Return the i-th node in order sense.
 
std::pair< long, Node * > position (const Key &key) const noexcept
 Compute the inorder position of a key.
 
std::pair< long, Node * > find_position (const Key &key) const noexcept
 Find the inorder position of a key in the tree.
 
void join_exclusive (Gen_Rb_Tree_Rk &t) noexcept
 Join this tree exclusively with another tree.
 
Nodesplit_key (const Key &key, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
 Split tree by key.
 
void split_key_dup (const Key &key, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
 Split tree by key including duplicates.
 
void split_pos (size_t pos, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
 Split tree by inorder position.
 

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)
 
Noderotate_to_right_rk (Node *p, Node *pp) noexcept
 
Noderotate_to_left_rk (Node *p, Node *pp) noexcept
 
void fix_red_condition (Node *p)
 
void find_succ_and_swap (Node *p, Node *&pp)
 
void fix_black_condition (Node *p)
 
void update_counters_after_insertion () noexcept
 
void update_counters_after_deletion () noexcept
 
Nodesplit_key_rec_rb (Node *p, const Key &key, Node *&t1, Node *&t2) noexcept
 
void split_key_dup_rec_rb (Node *p, const Key &key, Node *&t1, Node *&t2) noexcept
 
void split_pos_rec_rb (Node *p, size_t pos, Node *&t1, Node *&t2) noexcept
 

Static Private Member Functions

static size_t black_height (Node *p) noexcept
 
static Noderotate_right_simple (Node *p) noexcept
 
static Noderotate_left_simple (Node *p) noexcept
 
static void fix_red_violation (Node *&subtree_root) noexcept
 
static Nodeextract_min_rb (Node *&p) noexcept
 
static Nodeextract_max_rb (Node *&p) noexcept
 
static Nodejoin_with_pivot_rb (Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
 
static Nodejoin_right_rb (Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
 
static Nodejoin_left_rb (Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
 
static Nodejoin_exclusive_rec_rb (Node *t1, size_t bh1, Node *t2, size_t bh2) noexcept
 

Private Attributes

Node head_node
 
Nodehead
 
Node *& root
 
FixedStack< Node * > rb_stack
 
Compare cmp
 

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_Rk< NodeType, Key, Compare >

Red-black tree with rank support (select/position operations).

This class extends the standard red-black tree with subtree size counts, enabling efficient O(log n) rank-based operations:

  • select(i): Get the i-th smallest element (0-indexed)
  • position(key): Get the rank/position of a key
  • find_position(key): Get insertion position even if key doesn't exist
  • split_pos(pos, t1, t2): Split tree at a position
  • remove_pos(i): Remove element at position i
How it works:
Each node stores a count field representing the number of nodes in its subtree (including itself). Rotations update these counts to maintain the invariant. This adds O(1) overhead per rotation but enables O(log n) ranked operations.
Complexity:
  • All basic operations (search, insert, remove): O(log n)
  • Rank operations (select, position): O(log n)
  • Space: O(n) + O(log n) for stack during operations
Example:
for (int i = 0; i < 100; ++i)
// Get 50th smallest element
auto node = tree.select(49); // 0-indexed, returns node with key 49
// Get position of key 42
auto [pos, found] = tree.position(42); // pos = 42, found points to node
Node * select(const size_t i) const
Return the i-th node in order sense.
Definition tpl_rbRk.H:640
Node * insert(Node *p)
Definition tpl_rbRk.H:508
std::pair< long, Node * > position(const Key &key) const noexcept
Compute the inorder position of a key.
Definition tpl_rbRk.H:651
NodeType< Key > Node
Definition tpl_rbRk.H:111
DynList< T > maps(const C &c, Op op)
Classic map operation.
Red-Black binary search tree with nodes without virtual destructor and with subtree counters for sele...
Definition tpl_rbRk.H:1544
Template Parameters
NodeTypeNode template (RbNodeRk or RbNodeRkVtl).
KeyThe type of keys stored in the tree.
CompareComparison functor for ordering keys.
See also
Gen_Rb_Tree Basic red-black tree without rank support.
GenTdRbTreeRk Top-down red-black with rank.

Definition at line 107 of file tpl_rbRk.H.

Member Typedef Documentation

◆ key_type

template<template< typename > class NodeType, typename Key , class Compare >
typedef Key Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::key_type

Definition at line 458 of file tpl_rbRk.H.

◆ Node

template<template< typename > class NodeType, typename Key , class Compare >
typedef NodeType<Key> Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::Node

Definition at line 111 of file tpl_rbRk.H.

Constructor & Destructor Documentation

◆ Gen_Rb_Tree_Rk() [1/2]

template<template< typename > class NodeType, typename Key , class Compare >
Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::Gen_Rb_Tree_Rk ( Compare  cmp_arg = Compare())
inline

Definition at line 464 of file tpl_rbRk.H.

◆ Gen_Rb_Tree_Rk() [2/2]

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

Definition at line 475 of file tpl_rbRk.H.

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

◆ ~Gen_Rb_Tree_Rk()

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

Member Function Documentation

◆ black_height()

◆ extract_max_rb()

template<template< typename > class NodeType, typename Key , class Compare >
static Node * Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::extract_max_rb ( Node *&  p)
inlinestaticprivatenoexcept

◆ extract_min_rb()

template<template< typename > class NodeType, typename Key , class Compare >
static Node * Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::extract_min_rb ( Node *&  p)
inlinestaticprivatenoexcept

◆ find_position()

template<template< typename > class NodeType, typename Key , class Compare >
std::pair< long, Node * > Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::find_position ( const Key &  key) const
inlinenoexcept

Find the inorder position of a key in the tree.

Parameters
[in]keyto be searched
Returns
a pair with the position and the node pointer

Definition at line 666 of file tpl_rbRk.H.

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

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

◆ find_succ_and_swap()

◆ fix_black_condition()

◆ fix_red_condition()

◆ fix_red_violation()

template<template< typename > class NodeType, typename Key , class Compare >
static void Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::fix_red_violation ( Node *&  subtree_root)
inlinestaticprivatenoexcept

◆ get_compare()

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

◆ getRoot()

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

Definition at line 502 of file tpl_rbRk.H.

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

Referenced by TEST().

◆ insert()

◆ insert_dup()

◆ is_empty()

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

◆ join_exclusive()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::join_exclusive ( Gen_Rb_Tree_Rk< NodeType, Key, Compare > &  t)
inlinenoexcept

Join this tree exclusively with another tree.

All keys in this tree must be less than all keys in t. After the operation, t becomes empty and this tree contains all nodes.

Parameters
[in,out]ttree to join with (will be empty after)
Note
O(m log(n+m)) time complexity where m is size of t

Definition at line 1327 of file tpl_rbRk.H.

References Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::insert(), Aleph::FixedStack< T >::is_empty(), Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::pop(), Aleph::FixedStack< T >::push(), Aleph::RLINK(), and Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::root.

◆ join_exclusive_rec_rb()

◆ join_left_rb()

◆ join_right_rb()

◆ join_with_pivot_rb()

◆ key_comp()

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

◆ operator=()

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

◆ position()

template<template< typename > class NodeType, typename Key , class Compare >
std::pair< long, Node * > Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::position ( const Key &  key) const
inlinenoexcept

Compute the inorder position of a key.

Parameters
[in]keyto be searched
Returns
a pair with the inorder position and the node pointer. If key is not found, position is -1.

Definition at line 651 of file tpl_rbRk.H.

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

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

◆ remove()

◆ rotate_left_simple()

◆ rotate_right_simple()

◆ rotate_to_left_rk()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::rotate_to_left_rk ( Node p,
Node pp 
)
inlineprivatenoexcept

◆ rotate_to_right_rk()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::rotate_to_right_rk ( Node p,
Node pp 
)
inlineprivatenoexcept

◆ search()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::search ( const Key &  key)
inline

◆ search_and_stack_rb()

◆ search_dup_and_stack_rb()

◆ search_or_insert()

◆ select()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::select ( const size_t  i) const
inline

Return the i-th node in order sense.

Parameters
[in]iinorder position of node to be selected
Returns
a pointer to the i-th node inorder sense
Exceptions
out_of_rangeif i is greater or equal than the number of nodes of tree

Definition at line 640 of file tpl_rbRk.H.

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

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

◆ size()

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

◆ split_key()

template<template< typename > class NodeType, typename Key , class Compare >
Node * Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::split_key ( const Key &  key,
Gen_Rb_Tree_Rk< NodeType, Key, Compare > &  t1,
Gen_Rb_Tree_Rk< NodeType, Key, Compare > &  t2 
)
inlinenoexcept

Split tree by key.

After the operation:

  • t1 contains all keys < key
  • t2 contains all keys > key
  • This tree becomes empty
Parameters
[in]keythe splitting key
[out]t1tree to receive keys < key
[out]t2tree to receive keys > key
Returns
pointer to node containing key, or nullptr if not found
Note
O(n) time complexity (simple iterative implementation)

Definition at line 1375 of file tpl_rbRk.H.

References Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::cmp, Aleph::DynList< T >::insert(), Aleph::FixedStack< T >::is_empty(), KEY, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::pop(), Aleph::FixedStack< T >::push(), Aleph::RLINK(), and Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::root.

Referenced by TEST(), and TEST().

◆ split_key_dup()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::split_key_dup ( const Key &  key,
Gen_Rb_Tree_Rk< NodeType, Key, Compare > &  t1,
Gen_Rb_Tree_Rk< NodeType, Key, Compare > &  t2 
)
inlinenoexcept

Split tree by key including duplicates.

After the operation:

  • t1 contains all keys <= key
  • t2 contains all keys > key
  • This tree becomes empty
Parameters
[in]keythe splitting key
[out]t1tree to receive keys <= key
[out]t2tree to receive keys > key
Note
O(n) time complexity (simple iterative implementation)

Definition at line 1423 of file tpl_rbRk.H.

References Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::cmp, Aleph::DynList< T >::insert(), Aleph::FixedStack< T >::is_empty(), KEY, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::pop(), Aleph::FixedStack< T >::push(), Aleph::RLINK(), and Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::root.

Referenced by TEST().

◆ split_key_dup_rec_rb()

◆ split_key_rec_rb()

◆ split_pos()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::split_pos ( size_t  pos,
Gen_Rb_Tree_Rk< NodeType, Key, Compare > &  t1,
Gen_Rb_Tree_Rk< NodeType, Key, Compare > &  t2 
)
inlinenoexcept

Split tree by inorder position.

After the operation:

  • t1 contains nodes at positions [0, pos)
  • t2 contains nodes at positions [pos, size)
  • This tree becomes empty
Parameters
[in]posthe splitting position (0-based)
[out]t1tree to receive positions [0, pos)
[out]t2tree to receive positions [pos, size)
Note
O(n) time complexity (simple iterative implementation)

Definition at line 1465 of file tpl_rbRk.H.

References Aleph::count(), Aleph::DynList< T >::insert(), Aleph::FixedStack< T >::is_empty(), Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::pop(), Aleph::FixedStack< T >::push(), Aleph::RLINK(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::root, and Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::size().

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

◆ split_pos_rec_rb()

◆ swap()

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

◆ update_counters_after_deletion()

template<template< typename > class NodeType, typename Key , class Compare >
void Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::update_counters_after_deletion ( )
inlineprivatenoexcept

◆ update_counters_after_insertion()

◆ verify()

Member Data Documentation

◆ cmp

◆ CmpEqual

◆ CmpGreater

◆ CmpLess

◆ head

◆ head_node

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

Definition at line 115 of file tpl_rbRk.H.

◆ rb_stack

◆ root

template<template< typename > class NodeType, typename Key , class Compare >
Node*& Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::root
private

Definition at line 117 of file tpl_rbRk.H.

Referenced by Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::Gen_Rb_Tree_Rk(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::find_position(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::fix_black_condition(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::fix_red_condition(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::getRoot(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::insert(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::is_empty(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::join_exclusive(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::operator=(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::position(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::remove(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::search(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::search_and_stack_rb(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::search_dup_and_stack_rb(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::search_or_insert(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::select(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::size(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::split_key(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::split_key_dup(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::split_pos(), Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::swap(), and Aleph::Gen_Rb_Tree_Rk< NodeType, Key, Compare >::verify().


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