43template <
typename RbNode>
44void insert(RbNode*& root, RbNode* new_node);
62template <
typename RbNode>
63void remove(RbNode*& root, RbNode* node);
81template <
typename RbNode>
82RbNode*
lower_bound(RbNode* root, std::size_t key,
bool (*cmp)(std::size_t, std::size_t));
96 value |= (1ull << 63);
108 value &= ~(1ull << 63);
117inline bool is_red(
const std::size_t& value) {
118 return (value & (1ull << 63));
128 return !(value & (1ull << 63));
140 return value & ~(1ull << 63);
173template <
typename RbNode>
175 RbNode* right_child = node->right;
176 node->right = right_child->left;
178 if (right_child->left)
179 right_child->left->parent = node;
181 right_child->parent = node->parent;
185 else if (node == node->parent->left)
186 node->parent->left = right_child;
188 node->parent->right = right_child;
190 right_child->left = node;
191 node->parent = right_child;
214template <
typename RbNode>
216 RbNode* left_child = node->left;
217 node->left = left_child->right;
219 if (left_child->right)
220 left_child->right->parent = node;
222 left_child->parent = node->parent;
226 else if (node == node->parent->left)
227 node->parent->left = left_child;
229 node->parent->right = left_child;
231 left_child->right = node;
232 node->parent = left_child;
254template <
typename RbNode>
256 while (z->parent &&
is_red(z->parent->value)) {
257 if (z->parent == z->parent->parent->left) {
258 RbNode* y = z->parent->parent->right;
259 if (y &&
is_red(y->value)) {
263 z = z->parent->parent;
265 if (z == z->parent->right) {
274 RbNode* y = z->parent->parent->left;
275 if (y &&
is_red(y->value)) {
279 z = z->parent->parent;
281 if (z == z->parent->left) {
317template <
typename RbNode>
318void insert(RbNode*& root, RbNode* new_node) {
322 std::size_t new_val =
get_value(new_node->value);
326 std::size_t curr_val =
get_value(x->value);
327 if (new_val < curr_val)
333 new_node->parent = y;
342 new_node->left =
nullptr;
343 new_node->right =
nullptr;
363template <
typename RbNode>
367 else if (u == u->parent->left)
370 u->parent->right = v;
373 v->parent = u->parent;
398template <
typename RbNode>
400 while (x != root && (x ==
nullptr ||
is_black(x->value))) {
401 if (x == (x_parent ? x_parent->left :
nullptr)) {
402 RbNode* w = x_parent->right;
409 if ((w->left ==
nullptr ||
is_black(w->left->value)) &&
410 (w->right ==
nullptr ||
is_black(w->right->value))) {
413 x_parent = x->parent;
415 if (w->right ==
nullptr ||
is_black(w->right->value)) {
423 auto parent_color =
get_color(x_parent->value);
435 RbNode* w = x_parent->left;
442 if ((w->right ==
nullptr ||
is_black(w->right->value)) &&
443 (w->left ==
nullptr ||
is_black(w->left->value))) {
446 x_parent = x->parent;
448 if (w->left ==
nullptr ||
is_black(w->left->value)) {
456 auto parent_color =
get_color(x_parent->value);
498template <
typename RbNode>
505 RbNode* x_parent =
nullptr;
506 bool is_y_original_black =
is_black(y->value);
510 x_parent = z->parent;
512 }
else if (!z->right) {
514 x_parent = z->parent;
522 is_y_original_black =
is_black(y->value);
525 if (y->parent == z) {
534 x_parent = y->parent;
541 y->right->parent = y;
558 if (is_y_original_black) {
591template <
typename RbNode>
592RbNode*
lower_bound(RbNode* root, std::size_t value,
bool (*cmp)(std::size_t, std::size_t)) {
594 RbNode* result =
nullptr;
596 std::size_t curr_val =
get_value(current->value);
597 if (cmp(value, curr_val)) {
599 current = current->left;
601 current = current->right;
Definition rb-tree.hpp:26
RbNode * lower_bound(RbNode *root, std::size_t key, bool(*cmp)(std::size_t, std::size_t))
Finds the smallest node with value >= key.
Definition rb-tree.hpp:592
std::size_t get_value(const std::size_t &value)
Extracts the actual value without the color bit.
Definition rb-tree.hpp:139
void fix_insert(RbNode *&root, RbNode *z)
Fixes Red-Black tree properties after insertion.
Definition rb-tree.hpp:255
void fix_remove(RbNode *&root, RbNode *x, RbNode *x_parent)
Fixes Red-Black tree properties after deletion.
Definition rb-tree.hpp:399
void left_rotate(RbNode *&root, RbNode *node)
Performs a left rotation around a node.
Definition rb-tree.hpp:174
void right_rotate(RbNode *&root, RbNode *node)
Performs a right rotation around a node.
Definition rb-tree.hpp:215
bool is_red(const std::size_t &value)
Checks if a node is RED.
Definition rb-tree.hpp:117
void transplant(RbNode *&root, RbNode *u, RbNode *v)
Replaces one subtree with another.
Definition rb-tree.hpp:364
bool is_black(const std::size_t &value)
Checks if a node is BLACK.
Definition rb-tree.hpp:127
void set_color_red(std::size_t &value)
Sets the color of a node to RED.
Definition rb-tree.hpp:95
void insert(RbNode *&root, RbNode *new_node)
Inserts a new node into the Red-Black tree.
Definition rb-tree.hpp:318
bool get_color(const std::size_t &value)
Gets the color bit value.
Definition rb-tree.hpp:149
void set_color_black(std::size_t &value)
Sets the color of a node to BLACK.
Definition rb-tree.hpp:107
void remove(RbNode *&root, RbNode *node)
Removes a node from the Red-Black tree.
Definition rb-tree.hpp:499