117template <
template <
typename>
class NodeType,
typename Key,
class Compare>
161 while (p != Node::NullPtr);
186 const Key &
pk =
KEY(p);
198 while (p != Node::NullPtr);
274 while (
LLINK(succ) != Node::NullPtr)
290 LLINK(p) = Node::NullPtr;
292 if (
RLINK(p) == succ)
435 std::swap(
root, tree.root);
437 std::swap(
cmp, tree.cmp);
443 rb_stack(Node::MaxHeight),
cmp(std::move(tree.cmp)),
447 tree.root = Node::NullPtr;
458 cmp = std::move(tree.cmp);
459 tree.root = Node::NullPtr;
475 while (p != Node::NullPtr)
479 else if (
cmp(
KEY(p), key))
502 root = Node::NullPtr;
513 assert(p !=
nullptr and p != Node::NullPtr);
516 if (
root == Node::NullPtr)
547 assert(p !=
nullptr and p != Node::NullPtr);
550 if (
root == Node::NullPtr)
581 assert(p !=
nullptr and p != Node::NullPtr);
584 if (
root == Node::NullPtr)
613 if (
root == Node::NullPtr)
628 if (
LLINK(q) == Node::NullPtr)
637 if (
RLINK(q) == Node::NullPtr)
681template <
typename Key,
class Compare = Aleph::less<Key>>
695template <
typename Key,
class Compare = Aleph::less<Key>>
Standard functor implementations and comparison objects.
Inorder iterator on the nodes of a binary tree.
T & push(const T &data) noexcept
Push a copy of data
size_t size() const noexcept
Return the number of elements stored in the stack.
T popn(const int &n) noexcept
Perform in constant time n pops.
T & top() const noexcept
Return a modifiable referemce to stack's top.
T pop() noexcept
Pop by moving the top of stack.
void empty() noexcept
Empty the stack.
Red-black binary search tree implementation (bottom-up).
const Compare & get_compare() const noexcept
void reset() noexcept
Reset tree to empty state (does not free nodes)
Node * search_or_insert(Node *p) noexcept
Search for key or insert if not found.
FixedStack< Node * > rb_stack
Stack for ancestor path.
Gen_Rb_Tree & operator=(Gen_Rb_Tree &&tree) noexcept
Move assignment operator.
const Compare & key_comp() const noexcept
size_t num_nodes
Number of nodes in tree.
Node * insert_dup(Node *p) noexcept
Insert a node allowing duplicates.
Gen_Rb_Tree(Compare __cmp=Compare()) noexcept
Default constructor with optional comparator.
Node *& root
Reference to root (right child of head)
Node * insert(Node *p) noexcept
Insert a node into the tree.
Compare cmp
Comparison functor.
Node * search(const Key &key) const noexcept
Search for a key in the tree.
Node * search_and_stack_rb(const Key &key, signed char &cmp_result) noexcept
Search for key and stack ancestors (optimistic duplicate detection)
Node * getRoot() const noexcept
Get const root pointer.
void find_succ_and_swap(Node *p, Node *&pp) noexcept
Find successor and swap with node being deleted.
virtual ~Gen_Rb_Tree()=default
Destructor.
void fix_red_condition(Node *p) noexcept
Fix red-red violation after insertion.
size_t size() const noexcept
Returns the number of nodes in the tree (O(1))
Node * head
Pointer to sentinel.
Node head_node
Sentinel header node.
Compare & get_compare() noexcept
bool verify() const noexcept
Verify that tree satisfies red-black properties.
Node *& getRoot() noexcept
Get reference to root pointer.
Node * remove(const Key &key) noexcept
Remove the node containing key.
bool is_empty() const noexcept
Returns true if the tree is empty.
Gen_Rb_Tree(Gen_Rb_Tree &&tree) noexcept
Move constructor.
static constexpr signed char CmpEqual
void init() noexcept
Initialize tree state.
static constexpr signed char CmpLess
void swap(Gen_Rb_Tree &tree) noexcept
Swaps all elements with another tree in constant time.
static constexpr signed char CmpGreater
Node * search_dup_and_stack_rb(const Key &key, signed char &cmp_result) noexcept
Search for insertion point (allows duplicates) and stack ancestors.
void fix_black_condition(Node *p) noexcept
Fix black-height violation after deletion.
Compare & key_comp() noexcept
Returns a reference to the comparison criteria.
Node * rotate_to_left(Node *p) noexcept
Rotate to the left the tree with root p
Node * rotate_to_right(Node *p) noexcept
Rotate to the right the tree with root p
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Red-Black tree node definition and validation utilities.
bool is_red_black_bst(Node *node, Compare &cmp)
Verify that tree is both a valid RB tree and valid BST.
#define BLACK
Black color constant.
#define RED
Red color constant (newly inserted nodes are red)
Iterator over tree nodes in sorted order.
Iterator() noexcept=default
Default constructor creates an "end" iterator.
Iterator(const Gen_Rb_Tree &t)
Red-black tree with virtual destructor in nodes.
Red-black tree with nodes without virtual destructor.
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.