106template <
template <
typename>
class NodeType,
typename Key,
class Compare>
149 while (p != Node::NullPtr);
173 const Key &
pk =
KEY(p);
185 while (p != Node::NullPtr);
193 assert(p != Node::NullPtr);
216 assert(p != Node::NullPtr);
305 while (
LLINK(succ) != Node::NullPtr)
321 LLINK(p) = Node::NullPtr;
323 if (
RLINK(p) == succ)
444 for (
size_t i = 0; i < sz - 1; ++i)
452 for (
size_t i = 0; i < sz - 1; ++i)
471 std::swap(
root, tree.root);
472 std::swap(
cmp, tree.cmp);
477 rb_stack(Node::MaxHeight),
cmp(std::move(tree.cmp))
480 tree.root = Node::NullPtr;
488 tree.root = Node::NullPtr;
489 cmp = std::move(tree.cmp);
510 assert(p !=
nullptr and p != Node::NullPtr);
513 if (
root == Node::NullPtr)
536 assert(p !=
nullptr and p != Node::NullPtr);
539 if (
root == Node::NullPtr)
562 assert(p !=
nullptr and p != Node::NullPtr);
565 if (
root == Node::NullPtr)
585 if (
root == Node::NullPtr)
601 if (
LLINK(q) == Node::NullPtr)
610 if (
RLINK(q) == Node::NullPtr)
651 std::pair<long, Node*>
position(
const Key & key)
const noexcept
653 std::pair<long, Node*>
ret_val;
668 std::pair<long, Node*> r(-2,
nullptr);
682 while (p != Node::NullPtr)
774 if (p == Node::NullPtr)
775 return Node::NullPtr;
777 if (
LLINK(p) == Node::NullPtr)
790 if (
LLINK(p) == Node::NullPtr)
808 if (s != Node::NullPtr)
813 p = rotate_left_simple(p);
820 if (s != Node::NullPtr)
843 RLINK(p) = rotate_right_simple(s);
847 p = rotate_left_simple(p);
850 if (
RLINK(p) != Node::NullPtr)
869 return ExtractHelper::extract(p,
dummy);
875 if (p == Node::NullPtr)
876 return Node::NullPtr;
878 if (
RLINK(p) == Node::NullPtr)
890 if (
RLINK(p) == Node::NullPtr)
906 if (s != Node::NullPtr)
910 p = rotate_right_simple(p);
916 if (s != Node::NullPtr)
936 LLINK(p) = rotate_left_simple(s);
939 p = rotate_right_simple(p);
942 if (
LLINK(p) != Node::NullPtr)
961 return ExtractHelper::extract(p,
dummy);
993 if (
t1 == Node::NullPtr)
1064 if (
t2 == Node::NullPtr)
1129 if (
t1 == Node::NullPtr)
1131 if (
t2 == Node::NullPtr)
1139 if (
t1 != Node::NullPtr)
1147 if (
t2 != Node::NullPtr)
1160 if (p == Node::NullPtr)
1162 t1 =
t2 = Node::NullPtr;
1163 return Node::NullPtr;
1172 LLINK(p) = Node::NullPtr;
1177 RLINK(p) = Node::NullPtr;
1182 if (
t2 != Node::NullPtr)
1185 else if (
cmp(
KEY(p), key))
1190 RLINK(p) = Node::NullPtr;
1195 LLINK(p) = Node::NullPtr;
1200 if (
t1 != Node::NullPtr)
1211 if (
t1 != Node::NullPtr)
1213 if (
t2 != Node::NullPtr)
1224 if (p == Node::NullPtr)
1226 t1 =
t2 = Node::NullPtr;
1235 LLINK(p) = Node::NullPtr;
1240 RLINK(p) = Node::NullPtr;
1245 if (
t2 != Node::NullPtr)
1253 RLINK(p) = Node::NullPtr;
1258 LLINK(p) = Node::NullPtr;
1263 if (
t1 != Node::NullPtr)
1271 if (p == Node::NullPtr)
1273 t1 =
t2 = Node::NullPtr;
1284 LLINK(p) = Node::NullPtr;
1289 RLINK(p) = Node::NullPtr;
1294 if (
t2 != Node::NullPtr)
1302 RLINK(p) = Node::NullPtr;
1307 LLINK(p) = Node::NullPtr;
1312 if (
t1 != Node::NullPtr)
1329 if (t.root == Node::NullPtr)
1332 if (
root == Node::NullPtr)
1335 t.root = Node::NullPtr;
1343 t.root = Node::NullPtr;
1355 if (right != Node::NullPtr)
1357 if (left != Node::NullPtr)
1378 if (
root == Node::NullPtr)
1385 root = Node::NullPtr;
1393 if (right != Node::NullPtr)
1395 if (left != Node::NullPtr)
1402 else if (
cmp(key,
KEY(p)))
1426 if (
root == Node::NullPtr)
1431 root = Node::NullPtr;
1439 if (right != Node::NullPtr)
1441 if (left != Node::NullPtr)
1468 if (
root == Node::NullPtr)
1474 root = Node::NullPtr;
1481 root = Node::NullPtr;
1489 root = Node::NullPtr;
1494 while (curr != Node::NullPtr)
1498 LLINK(curr) = Node::NullPtr;
1529 using Base::operator=;
1542template <
typename Key,
class Compare = Aleph::less<Key>>
1558template <
typename Key,
class Compare = Aleph::less<Key>>
Exception handling system with formatted messages for Aleph-w.
Standard functor implementations and comparison objects.
Base iterator template for ranked binary search trees.
T & insert(const T &item)
Insert a new item by copy.
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.
Iterator on nodes of the tree.
Red-black tree with rank support (select/position operations).
static Node * join_exclusive_rec_rb(Node *t1, size_t bh1, Node *t2, size_t bh2) noexcept
Node * split_key(const Key &key, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
Split tree by key.
void update_counters_after_insertion() noexcept
static Node * join_left_rb(Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
FixedStack< Node * > rb_stack
Node * search_and_stack_rb(const Key &key, signed char &cmp_result) noexcept
Search for key and stack ancestors (optimistic duplicate detection)
size_t size() const noexcept
Gen_Rb_Tree_Rk(Compare cmp_arg=Compare())
void join_exclusive(Gen_Rb_Tree_Rk &t) noexcept
Join this tree exclusively with another tree.
Node * select(const size_t i) const
Return the i-th node in order sense.
static Node * join_right_rb(Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
void fix_red_condition(Node *p)
static Node * extract_min_rb(Node *&p) noexcept
static Node * rotate_right_simple(Node *p) noexcept
static constexpr signed char CmpEqual
virtual ~Gen_Rb_Tree_Rk()=default
static void fix_red_violation(Node *&subtree_root) noexcept
Node * insert_dup(Node *p)
void find_succ_and_swap(Node *p, Node *&pp)
std::pair< long, Node * > find_position(const Key &key) const noexcept
Find the inorder position of a key in the tree.
void swap(Gen_Rb_Tree_Rk &tree) noexcept
bool is_empty() const noexcept
static Node * join_with_pivot_rb(Node *t1, size_t bh1, Node *pivot, Node *t2, size_t bh2) noexcept
Node * search(const Key &key)
Node * search_dup_and_stack_rb(const Key &key, signed char &cmp_result)
Node * rotate_to_right_rk(Node *p, Node *pp) noexcept
void fix_black_condition(Node *p)
void split_pos(size_t pos, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
Split tree by inorder position.
static constexpr signed char CmpGreater
Gen_Rb_Tree_Rk & operator=(Gen_Rb_Tree_Rk &&tree) noexcept
Node * search_or_insert(Node *p)
Compare & key_comp() noexcept
void update_counters_after_deletion() noexcept
static Node * rotate_left_simple(Node *p) noexcept
void split_key_dup(const Key &key, Gen_Rb_Tree_Rk &t1, Gen_Rb_Tree_Rk &t2) noexcept
Split tree by key including duplicates.
Node *& getRoot() noexcept
void split_key_dup_rec_rb(Node *p, const Key &key, Node *&t1, Node *&t2) noexcept
static constexpr signed char CmpLess
Node * remove(const Key &key)
void split_pos_rec_rb(Node *p, size_t pos, Node *&t1, Node *&t2) noexcept
Node * split_key_rec_rb(Node *p, const Key &key, Node *&t1, Node *&t2) noexcept
Gen_Rb_Tree_Rk(Gen_Rb_Tree_Rk &&tree) noexcept
Node * rotate_to_left_rk(Node *p, Node *pp) noexcept
Compare & get_compare() noexcept
std::pair< long, Node * > position(const Key &key) const noexcept
Compute the inorder position of a key.
static Node * extract_max_rb(Node *&p) noexcept
static size_t black_height(Node *p) noexcept
long inorder_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept
Compute the inorder position of a key.
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended binary tree.
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
auto & COUNT(Node *p) noexcept
Return the number of nodes of the tree fron p is root.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
Main namespace for Aleph-w library functions.
bool is_red_black_bst_rk(Node *node, Compare &cmp)
Verify that tree rooted at node is a valid red-black BST with counters.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Red-Black tree node with rank (subtree count).
#define BLACK
Black color constant.
#define RED
Red color constant (newly inserted nodes are red)
Red-Black binary search tree with virtual destructor in its nodes and with subtree counters for selec...
Red-Black binary search tree with nodes without virtual destructor and with subtree counters for sele...
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.
Extended binary node with subtree count.
Binary tree operations (split, join, rotate).