12namespace swoc {
inline namespace SWOC_VERSION_NS {
namespace detail {
20 return c == (n ? n->
color() : RBNode::Color::BLACK);
33 return d == Direction::RIGHT ?
_right : d == Direction::LEFT ?
_left :
nullptr;
43 if (dir != Direction::NONE && this->
child_at(other_dir)) {
66 if (dir == Direction::RIGHT) {
68 }
else if (dir == Direction::LEFT) {
113 while (x && x->
_parent == Color::RED) {
124 if (y == Color::RED) {
145 root->
_color = Color::BLACK;
161 _left->_parent =
nullptr;
163 root->
_color = Color::BLACK;
165 _right->_parent =
nullptr;
167 root->
_color = Color::BLACK;
197 remove_color = splice_node->
_color;
203 splice_node = remove_node->
_parent;
212 if (remove_node !=
this) {
214 if (splice_node ==
this) {
215 splice_node = remove_node;
221 root->
_color = Color::BLACK;
236 if (Color::BLACK == c) {
242 if (Direction::NONE != d) {
249 if (n && n == Color::RED) {
256 Direction near(Direction::LEFT), far(Direction::RIGHT);
257 if ((Direction::NONE == d && parent->
direction_of(n) == Direction::RIGHT) || Direction::RIGHT == d) {
258 near = Direction::RIGHT;
259 far = Direction::LEFT;
264 if (w->
_color == Color::RED) {
266 parent->
_color = Color::RED;
272 if (w->
child_at(near) == Color::BLACK && wfc == Color::BLACK) {
278 if (wfc == Color::BLACK) {
286 parent->
_color = Color::BLACK;
287 wfc->
_color = Color::BLACK;
308 root->
_color = Color::BLACK;
319 currNode->
_color = isBlack ? Color::BLACK : Color::RED;
327 int right_n = n - left_n - 1;
328 if (right_n % 2 == 0)
330 std::swap(left_n, right_n);
338 currNode->
_left = leftBranch;
355 currNode->
_color = Color::BLACK;
361 currNode->
_color = Color::BLACK;
373 if (root ==
nullptr) {
385 std::string
color = (root->
_color == Color::RED) ?
"R" :
"B";
386 std::cout <<
color << std::endl;
388 last = root->
_right ==
nullptr;
401 int black_ht1, black_ht2;
404 black_ht1 =
_left->validate();
409 if (black_ht1 > 0 &&
_right)
410 black_ht2 =
_right->validate();
414 if (black_ht1 == black_ht2) {
415 black_ht = black_ht1;
416 if (this->
_color == Color::BLACK)
419 if (
_left == Color::RED)
421 else if (
_right == Color::RED)
424 std::cout <<
"Red-red child\n";
427 std::cout <<
"Height mismatch " << black_ht1 <<
" " << black_ht2 <<
"\n";
bool operator==(RBNode *n, RBNode::Color c)
For template deduction guides.
void clear_child(Direction dir)
virtual void structure_fixup()
static void printTree(self_type *root, std::string indent="", bool last=true)
Recursively print the tree in a human-readable format.
RBNode()=default
Default constructor.
self_type * rebalance_after_insert()
Rebalance the tree starting at this node.
Direction direction_of(self_type *const &n) const
self_type * left_most_descendant() const
self_type * child_at(Direction d) const
self_type * rotate(Direction dir)
Direction
Directional constants.
static self_type * buildTree(self_type *&head, int n)
Recursively build a balanced RB tree from a sorted linked list.
self_type * rebalance_after_remove(Color c, Direction dir)
void replace_with(self_type *n)
RBNode self_type
self reference type
self_type * set_child(self_type *child, Direction dir)
Direction flip(Direction d)
self_type * ripple_structure_fixup()
virtual bool structure_validate()
self_type * _right
right child
self_type * _parent
parent node (needed for rotations)
self_type * _left
left child
self_type * _next
Next node.