45# ifndef TPL_HTDRBTREE_H
46# define TPL_HTDRBTREE_H
82 template <
class Key,
class Compare = Aleph::less<Key>>
210 const Key &
qk =
KEY(q);
220 const Key &
pk =
KEY(p);
258 const Key &
pk =
KEY(p);
279 const Key &
qk =
KEY(q);
289 const Key &
pk =
KEY(p);
323 const Key &
pk =
KEY(p);
356 const Key &
pk =
KEY(p);
449 if (
RLINK(p) == succ)
782 std::swap(
root, tree.root);
784 std::swap(
cmp, tree.cmp);
874 const Key &
pk =
KEY(p);
1030 template <
class Key,
class Compare = Aleph::less<Key>>
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.
bool is_empty() const noexcept
Return true if stack is empty.
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.
Hybrid red-black tree with virtual node destructor.
Hybrid top-down/bottom-up red-black tree.
HtdRbTree(const HtdRbTree &)=delete
Copy constructor deleted.
Compare cmp
Comparison functor.
static void colorParentAndSibling(Node *fp, Node *sp) noexcept
Recolor parent and sibling to fix violation.
Node * fHead
Pointer to head's parent.
Node * getRoot() const noexcept
Get root pointer (const)
Node * searchFlipColorsAndInsert(Node *q) noexcept
Search for insertion point with proactive color flipping.
HtdRbTree & operator=(HtdRbTree &&tree) noexcept
Move assignment.
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.
unsigned char Color
Color type for red-black nodes.
Node *& root
Reference to root (right child of head)
bool verify() const noexcept
Verify tree is a valid red-black BST.
Node * insert(Node *p) noexcept
Insert a node into the tree.
Node * remove(const Key &key) noexcept
Remove node with given key.
void restoreRedCondition(Node *p, Node *&fp, Node *ffp, Node *fffp)
Restore red-black condition after insertion.
Node * search(const Key &key) const noexcept
Search for a key.
Key key_type
The key type stored in nodes.
static void testNode(Node *p) noexcept
Verify red condition and black height for a node.
Compare & key_comp() noexcept
Returns a reference to the comparison functor.
size_t node_count
Number of nodes in tree.
static void colorSiblingAsRed(Node *sp) noexcept
Color sibling red to propagate violation upward.
Node *& getRoot() noexcept
Get reference to root pointer.
Node * head
Pointer to head node.
void reset() noexcept
Reset tree (does not free nodes)
void doubleRotateNephewAndColor(Node *fp, Node *sp, Node *snp) noexcept
Double rotation and recolor to fix violation.
Node * searchAndBuildPath(const Key &key) noexcept
Search for key while building path stack.
HtdRbTree & operator=(const HtdRbTree &)=delete
Copy assignment deleted.
Compare & get_compare() noexcept
Alias for key_comp()
Node headParent
Auxiliary node, grandparent of root.
Node headNode
Auxiliary node, parent of root.
RbNode< Key > Node
The node type used by this tree.
HtdRbTree(Compare __cmp=Compare()) noexcept
Default constructor.
constexpr const Compare & get_compare() const noexcept
FixedStack< Node * > path
Stack for deletion path.
HtdRbTree(HtdRbTree &&tree) noexcept
Move constructor.
Node * search_or_insert(Node *p) noexcept
Search for key or insert if not found.
Node * searchFlipColorsAndInsertDup(Node *q) noexcept
Like searchFlipColorsAndInsert but allows duplicate keys.
static void blackHeight(Node *p, int &max, int bh=0) noexcept
Verify black height consistency.
constexpr size_t size() const noexcept
Get number of nodes in tree.
Node * insert_dup(Node *p) noexcept
Insert allowing duplicate keys.
void balanceDownAndColor(Node *p, Node *&fp, Node *&sp) noexcept
Balance down a violating black node.
Node * ffHead
Pointer to head's grandparent.
Node headGrandParent
Auxiliary node, great-grandparent of root.
constexpr bool is_empty() const noexcept
Check if tree is empty.
virtual ~HtdRbTree()=default
Virtual destructor.
void findSuccAndSwap(Node *p, Node *&fp) noexcept
Find successor and swap with node to delete.
void rotateNephewAndColor(Node *fp, Node *sp, Node *np) noexcept
Rotate nephew up and recolor to fix violation.
void swap(HtdRbTree &tree) noexcept
Swap contents with another tree.
void removeAndFixBlackCondition(Node *q) noexcept
Remove node and fix any black-height violations.
void verifyRedBlack() const
Verify red-black tree invariants (DEBUG mode).
const Compare & key_comp() const noexcept
Declare RbNode type with 128-byte pool allocation.
static RbNode *const NullPtr
RbNode *& getR() noexcept
static const size_t MaxHeight
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
Node * rotate_to_left(Node *p) noexcept
Rotate to the left the tree with root p
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
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.
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept
Search a key in a binary search tree.
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(HtdRbTree &t) noexcept
Iterator(const HtdRbTree &t) noexcept
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.
Basic binary tree node definitions.