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

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

#include <tpl_hRbTree.H>

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

Classes

struct  Iterator
 In-order iterator. More...
 

Public Types

using Color = unsigned char
 Color type for red-black nodes.
 
using Node = RbNode< Key >
 The node type used by this tree.
 
using key_type = Key
 The key type stored in nodes.
 

Public Member Functions

Compare & key_comp () noexcept
 Returns a reference to the comparison functor.
 
const Compare & key_comp () const noexcept
 
Compare & get_compare () noexcept
 Alias for key_comp()
 
constexpr const Compare & get_compare () const noexcept
 
 HtdRbTree () noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare > and std::is_nothrow_constructible_v< Compare, Compare & >)
 Builds an empty tree with a comparison criteria built by default.
 
 HtdRbTree (Compare __cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >)
 Builds an empty tree with the comparison criteria __cmp.
 
void swap (HtdRbTree &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
 Swaps in constant time the nodes and the comparison criteria of this and tree.
 
 HtdRbTree (HtdRbTree &&tree) noexcept(bin_node_detail::nothrow_header_from_v< Node > and bin_node_detail::nothrow_compare_taken_v< Compare >)
 Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.
 
HtdRbTree & operator= (HtdRbTree &&tree) noexcept(std::is_nothrow_swappable_v< Compare >)
 Swaps the nodes and the comparison criteria with tree.
 
 HtdRbTree (const HtdRbTree &)=delete
 Copy constructor deleted.
 
HtdRbTree & operator= (const HtdRbTree &)=delete
 Copy assignment deleted.
 
virtual ~HtdRbTree ()=default
 Virtual destructor.
 
constexpr bool is_empty () const noexcept
 Check if tree is empty.
 
constexpr size_t size () const noexcept
 Get number of nodes in tree.
 
void reset () noexcept
 Reset tree (does not free nodes)
 
Node * insert (Node *p) noexcept(nothrow_compare_v< Compare, Key >)
 Insert a node into the tree.
 
Node * search_or_insert (Node *p) noexcept(nothrow_compare_v< Compare, Key >)
 Search for key or insert if not found.
 
Node * insert_dup (Node *p) noexcept(nothrow_compare_v< Compare, Key >)
 Insert allowing duplicate keys.
 
Node * search (const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
 Search for a key.
 
Node * remove (const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
 Remove node with given key.
 
Node *& getRoot () noexcept
 Get reference to root pointer.
 
Node * getRoot () const noexcept
 Get root pointer (const)
 
bool verifyRedBlack () const noexcept
 Verify the red-black properties of every node.
 
bool verify () const
 Verify tree is a valid red-black BST.
 

Private Types

using Path = InlineStack< Node *, Node::MaxHeight+1 >
 

Private Member Functions

void restoreRedCondition (Node *p, Node *&fp, Node *ffp, Node *fffp)
 Restore red-black condition after insertion.
 
Node * searchFlipColorsAndInsert (Node *q) noexcept(nothrow_compare_v< Compare, Key >)
 Search for insertion point with proactive color flipping.
 
Node * searchFlipColorsAndInsertDup (Node *q) noexcept(nothrow_compare_v< Compare, Key >)
 Like searchFlipColorsAndInsert but allows duplicate keys; as it, a comparison that throws leaves a valid tree without q.
 
Node * searchAndBuildPath (const Key &key, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
 Search for key while building path stack.
 
void findSuccAndSwap (Node *p, Node *&fp, Path &path) noexcept
 Find successor and swap with node to delete.
 
void balanceDownAndColor (Node *p, Node *&fp, Node *&sp, Path &path) noexcept
 Balance down a violating black node.
 
void rotateNephewAndColor (Node *fp, Node *sp, Node *np, Path &path) noexcept
 Rotate nephew up and recolor to fix violation.
 
void doubleRotateNephewAndColor (Node *fp, Node *sp, Node *snp, Path &path) noexcept
 Double rotation and recolor to fix violation.
 
void removeAndFixBlackCondition (Node *q, Path &path) noexcept
 Remove node and fix any black-height violations.
 

Static Private Member Functions

static Node * getSibling (Node *p, Node *fp) noexcept
 Returns sibling of p given its parent fp.
 
static void flipColors (Node *p) noexcept
 Flip colors of a black node with two red children.
 
static void colorSiblingAsRed (Node *sp) noexcept
 Color sibling red to propagate violation upward.
 
static void colorParentAndSibling (Node *fp, Node *sp) noexcept
 Recolor parent and sibling to fix violation.
 

Private Attributes

Node headNode
 Auxiliary node, parent of root.
 
Node headParent
 Auxiliary node, grandparent of root.
 
Node headGrandParent
 Auxiliary node, great-grandparent of root.
 
Node * head
 Pointer to head node.
 
Node * fHead
 Pointer to head's parent.
 
Node * ffHead
 Pointer to head's grandparent.
 
Node *& root
 Reference to root (right child of head)
 
size_t node_count = 0
 Number of nodes in tree.
 
Compare cmp
 Comparison functor.
 

Detailed Description

template<class Key, class Compare = Aleph::less<Key>>
requires StrictWeakOrder<Compare, Key>
class Aleph::HtdRbTree< Key, Compare >

Hybrid top-down/bottom-up red-black tree.

This class implements a self-balancing binary search tree using the red-black tree algorithm with a hybrid approach:

  • Insertion: Top-down with proactive color flipping. As we descend to find the insertion point, we preemptively flip colors of nodes with two red children. This ensures the tree remains balanced with at most one rotation after insertion.
  • Deletion: Bottom-up using a stack to track ancestors. After removing a node, we fix any violations by walking back up the path.
Complexity:
  • All operations (insert, remove, search): O(log n)
  • Space: O(n) + O(log n) stack for deletion
Template Parameters
KeyThe type of keys stored in the tree
CompareComparison functor (default: Aleph::less<Key>)
Author
Leandro Rabindranath León

Definition at line 85 of file tpl_hRbTree.H.

Member Typedef Documentation

◆ Color

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

Color type for red-black nodes.

Definition at line 89 of file tpl_hRbTree.H.

◆ key_type

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

The key type stored in nodes.

Definition at line 779 of file tpl_hRbTree.H.

◆ Node

template<class Key , class Compare = Aleph::less<Key>>
using Aleph::HtdRbTree< Key, Compare >::Node = RbNode<Key>

The node type used by this tree.

Definition at line 92 of file tpl_hRbTree.H.

◆ Path

template<class Key , class Compare = Aleph::less<Key>>
using Aleph::HtdRbTree< Key, Compare >::Path = InlineStack<Node *, Node::MaxHeight + 1>
private

Definition at line 113 of file tpl_hRbTree.H.

Constructor & Destructor Documentation

◆ HtdRbTree() [1/4]

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

Builds an empty tree with a comparison criteria built by default.

It is a constructor apart from the one that takes the comparison, and not a default argument of that one: GCC and Clang do not count a default argument in the noexcept of the implicit default constructor of a tree that inherits these, and what the construction of the comparison threw would end the program there.

Exceptions
whatthe default construction of the key of a header node or of the comparison, or the copy of the comparison, throws; nothing when they are noexcept.

Definition at line 806 of file tpl_hRbTree.H.

◆ HtdRbTree() [2/4]

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

Builds an empty tree with the comparison criteria __cmp.

Parameters
[in]__cmpthe comparison criteria, which the tree copies.
Exceptions
whatthe default construction of the key of a header node or the copy of __cmp throws; nothing when both are noexcept. The copy is from a Compare that is not const, which a comparison may have apart from the one from a const one.

Definition at line 823 of file tpl_hRbTree.H.

References BLACK, COLOR, Aleph::HtdRbTree< Key, Compare >::ffHead, Aleph::HtdRbTree< Key, Compare >::fHead, Aleph::HtdRbTree< Key, Compare >::head, RbNode< Key >::NullPtr, and RLINK.

◆ HtdRbTree() [3/4]

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

Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.

It builds no key, if it can: the key of each of the three header nodes is built by default when that cannot throw; else it is that of the same header node of tree, moved, when the move of a key cannot throw (see bin_node_detail::header_from()).

Parameters
[in,out]treethe tree to move from. It keeps its comparison criteria, which are copied: from a const one, or from one that is not const, if they have only that copy. Criteria that cannot be copied are moved, and tree keeps criteria that were moved from.
Exceptions
whatthe copy of the comparison criteria throws, or their move if they cannot be copied; and what the default construction of the key of a header node throws, with a key whose move may throw too. Then tree keeps its nodes. Nothing when they are noexcept.

Definition at line 873 of file tpl_hRbTree.H.

References BLACK, COLOR, Aleph::HtdRbTree< Key, Compare >::ffHead, Aleph::HtdRbTree< Key, Compare >::fHead, Aleph::HtdRbTree< Key, Compare >::head, RbNode< Key >::NullPtr, RLINK, and Aleph::HtdRbTree< Key, Compare >::root.

◆ HtdRbTree() [4/4]

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

Copy constructor deleted.

◆ ~HtdRbTree()

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

Virtual destructor.

Member Function Documentation

◆ balanceDownAndColor()

template<class Key , class Compare = Aleph::less<Key>>
void Aleph::HtdRbTree< Key, Compare >::balanceDownAndColor ( Node *  p,
Node *&  fp,
Node *&  sp,
Path &  path 
)
inlineprivatenoexcept

Balance down a violating black node.

When sibling is red, rotate to make it black. This ensures the sibling-black cases can be applied.

Precondition
p is black, fp is black, sp is red
Parameters
pViolating node (black, missing a black node)
fpParent of p (updated)
spSibling of p (updated to new sibling, which will be black)
pathThe path from the head node to fp; its top is updated to the new parent of fp.

Definition at line 511 of file tpl_hRbTree.H.

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

Referenced by Aleph::HtdRbTree< Key, Compare >::removeAndFixBlackCondition().

◆ colorParentAndSibling()

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

Recolor parent and sibling to fix violation.

Case: parent is red. Swapping colors fixes the tree.

Parameters
fpParent (red)
spSibling (black)

Definition at line 642 of file tpl_hRbTree.H.

References BLACK, COLOR, LLINK, RED, and RLINK.

Referenced by Aleph::HtdRbTree< Key, Compare >::removeAndFixBlackCondition().

◆ colorSiblingAsRed()

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

Color sibling red to propagate violation upward.

After this, fp becomes the new violating node.

Parameters
spSibling (black, both children black)

Definition at line 629 of file tpl_hRbTree.H.

References BLACK, COLOR, and RED.

Referenced by Aleph::HtdRbTree< Key, Compare >::removeAndFixBlackCondition().

◆ doubleRotateNephewAndColor()

template<class Key , class Compare = Aleph::less<Key>>
void Aleph::HtdRbTree< Key, Compare >::doubleRotateNephewAndColor ( Node *  fp,
Node *  sp,
Node *  snp,
Path &  path 
)
inlineprivatenoexcept

Double rotation and recolor to fix violation.

Case: nephew snp is red and on the "inside" (opposite side from sibling). Double rotation (rotate sibling, then parent) fixes the tree.

Precondition
sp is black, snp is red
Parameters
fpParent of violating node
spSibling (black)
snpSibling's nephew (red, opposite side from sp)
pathThe path from the head node to fp; its top is the parent of fp.

Definition at line 593 of file tpl_hRbTree.H.

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

Referenced by Aleph::HtdRbTree< Key, Compare >::removeAndFixBlackCondition().

◆ findSuccAndSwap()

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

Find successor and swap with node to delete.

Used during deletion when the node has two children. Finds the in-order successor and swaps pointers (not contents).

Parameters
pNode to delete
fpParent of p (updated to point to new position)
pathThe path from the head node to p; the successor takes the place of p in it, and the nodes down to the successor are pushed.

Definition at line 426 of file tpl_hRbTree.H.

References COLOR, LLINK, log(), Aleph::HtdRbTree< Key, Compare >::node_count, RbNode< Key >::NullPtr, and RLINK.

Referenced by Aleph::HtdRbTree< Key, Compare >::removeAndFixBlackCondition().

◆ flipColors()

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

Flip colors of a black node with two red children.

The black node becomes red and its children become black. This is the key operation for top-down insertion.

Parameters
pNode to flip (must be black with two red children)

Definition at line 194 of file tpl_hRbTree.H.

References BLACK, COLOR, LLINK, RbNode< Key >::NullPtr, RED, and RLINK.

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

◆ get_compare() [1/2]

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

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 791 of file tpl_hRbTree.H.

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

◆ get_compare() [2/2]

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

Alias for key_comp()

Definition at line 788 of file tpl_hRbTree.H.

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

◆ getRoot() [1/2]

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

Get root pointer (const)

Definition at line 1069 of file tpl_hRbTree.H.

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

◆ getRoot() [2/2]

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

Get reference to root pointer.

Definition at line 1066 of file tpl_hRbTree.H.

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

Referenced by TEST().

◆ getSibling()

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

Returns sibling of p given its parent fp.

Definition at line 120 of file tpl_hRbTree.H.

References LLINK, and RLINK.

Referenced by Aleph::HtdRbTree< Key, Compare >::removeAndFixBlackCondition().

◆ insert()

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

Insert a node into the tree.

Parameters
pNode to insert (must have COLOR=RED and null children)
Returns
Pointer to inserted node, or nullptr if duplicate key
Exceptions
anythingthat the comparison throws; the color flips and rotations done on the way down leave a valid red-black tree, and p is linked after the last comparison, so the tree keeps its keys and size, without p.
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 943 of file tpl_hRbTree.H.

References COLOR, LLINK, Aleph::HtdRbTree< Key, Compare >::node_count, RbNode< Key >::NullPtr, RED, RLINK, Aleph::HtdRbTree< Key, Compare >::root, and Aleph::HtdRbTree< Key, Compare >::searchFlipColorsAndInsert().

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

◆ insert_dup()

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

Insert allowing duplicate keys.

Parameters
pNode to insert
Returns
Pointer to inserted node
Exceptions
anythingthat the comparison throws; then the tree is a valid red-black tree with its keys and size, without p, as in insert().
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 1000 of file tpl_hRbTree.H.

References COLOR, LLINK, Aleph::HtdRbTree< Key, Compare >::node_count, RbNode< Key >::NullPtr, RED, RLINK, Aleph::HtdRbTree< Key, Compare >::root, and Aleph::HtdRbTree< Key, Compare >::searchFlipColorsAndInsertDup().

◆ is_empty()

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

Check if tree is empty.

Definition at line 920 of file tpl_hRbTree.H.

References RbNode< Key >::NullPtr, and Aleph::HtdRbTree< Key, Compare >::root.

◆ key_comp() [1/2]

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

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 785 of file tpl_hRbTree.H.

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

◆ key_comp() [2/2]

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

Returns a reference to the comparison functor.

Definition at line 782 of file tpl_hRbTree.H.

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

◆ operator=() [1/2]

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

Copy assignment deleted.

◆ operator=() [2/2]

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

Swaps the nodes and the comparison criteria with tree.

Parameters
[in,out]treethe tree to swap with.
Returns
*this.
Exceptions
whatthe swap of the comparison criteria throws, which goes first: then the nodes stay where they were.

Definition at line 904 of file tpl_hRbTree.H.

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

◆ remove()

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

Remove node with given key.

Parameters
keyKey to remove
Returns
Pointer to removed node, or nullptr if not found
Exceptions
anythingthat the comparison throws; the tree changes only after the last comparison, so it is left as it was.
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 1040 of file tpl_hRbTree.H.

References Aleph::HtdRbTree< Key, Compare >::cmp, KEY, Aleph::HtdRbTree< Key, Compare >::node_count, RbNode< Key >::NullPtr, Aleph::HtdRbTree< Key, Compare >::removeAndFixBlackCondition(), RbNode< Key >::reset(), Aleph::HtdRbTree< Key, Compare >::root, and Aleph::HtdRbTree< Key, Compare >::searchAndBuildPath().

◆ removeAndFixBlackCondition()

template<class Key , class Compare = Aleph::less<Key>>
void Aleph::HtdRbTree< Key, Compare >::removeAndFixBlackCondition ( Node *  q,
Path &  path 
)
inlineprivatenoexcept

Remove node and fix any black-height violations.

Performs the actual BST deletion (bypass or swap with successor), then walks up the path fixing violations.

Parameters
qNode to remove (must be on top of path stack)
pathThe path from the head node to q, as built by searchAndBuildPath(); it is consumed.

Definition at line 662 of file tpl_hRbTree.H.

References Aleph::HtdRbTree< Key, Compare >::balanceDownAndColor(), BLACK, COLOR, Aleph::HtdRbTree< Key, Compare >::colorParentAndSibling(), Aleph::HtdRbTree< Key, Compare >::colorSiblingAsRed(), Aleph::HtdRbTree< Key, Compare >::doubleRotateNephewAndColor(), Aleph::HtdRbTree< Key, Compare >::findSuccAndSwap(), Aleph::HtdRbTree< Key, Compare >::getSibling(), LLINK, RbNode< Key >::NullPtr, RED, RLINK, Aleph::HtdRbTree< Key, Compare >::root, and Aleph::HtdRbTree< Key, Compare >::rotateNephewAndColor().

Referenced by Aleph::HtdRbTree< Key, Compare >::remove().

◆ reset()

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

Reset tree (does not free nodes)

Definition at line 926 of file tpl_hRbTree.H.

References Aleph::HtdRbTree< Key, Compare >::node_count, RbNode< Key >::NullPtr, and Aleph::HtdRbTree< Key, Compare >::root.

◆ restoreRedCondition()

template<class Key , class Compare = Aleph::less<Key>>
void Aleph::HtdRbTree< Key, Compare >::restoreRedCondition ( Node *  p,
Node *&  fp,
Node *  ffp,
Node *  fffp 
)
inlineprivate

Restore red-black condition after insertion.

Called when we have two consecutive red nodes (p and fp are both red). Performs rotations to fix the violation.

Parameters
pViolating node (red)
fpParent of p (red) - may be updated
ffpGrandparent of p (black)
fffpGreat-grandparent of p

Definition at line 138 of file tpl_hRbTree.H.

References BLACK, COLOR, LLINK, RED, RLINK, Aleph::HtdRbTree< Key, Compare >::root, Aleph::rotate_to_left(), and Aleph::rotate_to_right().

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

◆ rotateNephewAndColor()

template<class Key , class Compare = Aleph::less<Key>>
void Aleph::HtdRbTree< Key, Compare >::rotateNephewAndColor ( Node *  fp,
Node *  sp,
Node *  np,
Path &  path 
)
inlineprivatenoexcept

Rotate nephew up and recolor to fix violation.

Case: nephew np is red and on the "outside" (same side as sibling). Single rotation fixes the tree.

Precondition
sp is black, np is red
Parameters
fpParent of violating node
spSibling (black)
npNephew (red, same side as sp)
pathThe path from the head node to fp; its top is the parent of fp.

Definition at line 558 of file tpl_hRbTree.H.

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

Referenced by Aleph::HtdRbTree< Key, Compare >::removeAndFixBlackCondition().

◆ search()

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

Search for a key.

Parameters
keyKey to search for
Returns
Pointer to node with key, or nullptr if not found
Exceptions
anythingthat the comparison throws; the search does not modify the tree, so it is left as it was.
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 1025 of file tpl_hRbTree.H.

References Aleph::HtdRbTree< Key, Compare >::cmp, RbNode< Key >::NullPtr, Aleph::HtdRbTree< Key, Compare >::root, and Aleph::searchInBinTree().

Referenced by Aleph::HtdRbTree< Key, Compare >::search_or_insert().

◆ search_or_insert()

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

Search for key or insert if not found.

Parameters
pNode to insert if key not found
Returns
Existing node with key, or p if inserted
Exceptions
anythingthat the comparison throws; then the tree is a valid red-black tree with its keys and size, without p, as in insert().
Note
It is noexcept only if the comparison cannot throw, so that what it throws reaches the caller.

Definition at line 969 of file tpl_hRbTree.H.

References COLOR, Aleph::HtdRbTree< Key, Compare >::insert(), KEY, LLINK, Aleph::HtdRbTree< Key, Compare >::node_count, RbNode< Key >::NullPtr, RED, RLINK, Aleph::HtdRbTree< Key, Compare >::root, and Aleph::HtdRbTree< Key, Compare >::search().

◆ searchAndBuildPath()

template<class Key , class Compare = Aleph::less<Key>>
Node * Aleph::HtdRbTree< Key, Compare >::searchAndBuildPath ( const Key &  key,
Path &  path 
)
inlineprivatenoexcept

Search for key while building path stack.

Used for deletion. The path stack is needed to walk back up the tree when fixing violations.

Parameters
keyKey to search for
pathThe path, which receives the head node and the nodes visited down to the returned one.
Returns
Node with key, or leaf where key would be
Exceptions
anythingthat the comparison throws; the search does not modify the tree. It is noexcept only if the comparison cannot throw.

Definition at line 368 of file tpl_hRbTree.H.

References Aleph::HtdRbTree< Key, Compare >::cmp, Aleph::HtdRbTree< Key, Compare >::head, KEY, LLINK, log(), Aleph::HtdRbTree< Key, Compare >::node_count, RbNode< Key >::NullPtr, RLINK, and Aleph::HtdRbTree< Key, Compare >::root.

Referenced by Aleph::HtdRbTree< Key, Compare >::remove().

◆ searchFlipColorsAndInsert()

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

Search for insertion point with proactive color flipping.

Descends the tree looking for where to insert q. During descent, any node with two red children has its colors flipped preemptively. Each color flip and rotation leaves a valid red-black tree, and q is linked and counted after the last comparison, so a comparison that throws leaves a valid tree without q.

Parameters
qNode to insert
Returns
Pointer to inserted node, or nullptr if duplicate
Exceptions
anythingthat the comparison throws; it is noexcept only if the comparison cannot throw.

Definition at line 217 of file tpl_hRbTree.H.

References BLACK, Aleph::HtdRbTree< Key, Compare >::cmp, COLOR, Aleph::HtdRbTree< Key, Compare >::ffHead, Aleph::HtdRbTree< Key, Compare >::fHead, Aleph::HtdRbTree< Key, Compare >::flipColors(), Aleph::HtdRbTree< Key, Compare >::head, KEY, LLINK, Aleph::HtdRbTree< Key, Compare >::node_count, RbNode< Key >::NullPtr, RED, Aleph::HtdRbTree< Key, Compare >::restoreRedCondition(), RLINK, and Aleph::HtdRbTree< Key, Compare >::root.

Referenced by Aleph::HtdRbTree< Key, Compare >::insert().

◆ searchFlipColorsAndInsertDup()

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

◆ size()

template<class Key , class Compare = Aleph::less<Key>>
constexpr size_t Aleph::HtdRbTree< Key, Compare >::size ( ) const
inlineconstexprnoexcept

Get number of nodes in tree.

Definition at line 923 of file tpl_hRbTree.H.

References Aleph::HtdRbTree< Key, Compare >::node_count.

◆ swap()

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

Swaps in constant time the nodes and the comparison criteria of this and tree.

Parameters
[in,out]treethe tree to swap with.
Exceptions
whatthe swap of the comparison criteria throws, which goes first: then the nodes stay where they were.

Definition at line 847 of file tpl_hRbTree.H.

References Aleph::HtdRbTree< Key, Compare >::cmp, Aleph::HtdRbTree< Key, Compare >::node_count, and Aleph::HtdRbTree< Key, Compare >::root.

Referenced by Aleph::HtdRbTree< Key, Compare >::operator=().

◆ verify()

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

Verify tree is a valid red-black BST.

Returns
true if all red-black properties hold
Exceptions
whatthe comparator throws.

Definition at line 1090 of file tpl_hRbTree.H.

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

Referenced by TEST().

◆ verifyRedBlack()

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

Verify the red-black properties of every node.

Checks valid colors, that no red node has a red child, and that every path down to Node::NullPtr crosses the same number of black nodes. It does not look at the keys; verify() does.

Returns
true if all the red-black properties hold

Definition at line 1080 of file tpl_hRbTree.H.

References is_red_black_tree(), and Aleph::HtdRbTree< Key, Compare >::root.

Referenced by TEST().

Member Data Documentation

◆ cmp

◆ ffHead

template<class Key , class Compare = Aleph::less<Key>>
Node* Aleph::HtdRbTree< Key, Compare >::ffHead
private

◆ fHead

template<class Key , class Compare = Aleph::less<Key>>
Node* Aleph::HtdRbTree< Key, Compare >::fHead
private

◆ head

◆ headGrandParent

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

Auxiliary node, great-grandparent of root.

Definition at line 99 of file tpl_hRbTree.H.

◆ headNode

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

Auxiliary node, parent of root.

Definition at line 97 of file tpl_hRbTree.H.

◆ headParent

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

Auxiliary node, grandparent of root.

Definition at line 98 of file tpl_hRbTree.H.

◆ node_count

◆ root


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