Iros
 
Loading...
Searching...
No Matches
rb_tree_node.h
Go to the documentation of this file.
1#pragma once
2
4#include "di/types/prelude.h"
5#include "di/util/forward.h"
6
7namespace di::container {
8template<typename Tag>
9struct RBTreeNode {
10public:
11 enum class Color { Red = 0, Black = 1 };
12
13 RBTreeNode() = default;
14
15 constexpr auto is_left_child() const -> bool { return parent && parent->left == this; }
16 constexpr auto is_right_child() const -> bool { return parent && parent->right == this; }
17
18 constexpr auto find_min() -> RBTreeNode& {
19 auto* node = this;
20 while (node->left) {
21 node = node->left;
22 }
23 return *node;
24 }
25
26 constexpr auto find_max() -> RBTreeNode& {
27 auto* node = this;
28 while (node->right) {
29 node = node->right;
30 }
31 return *node;
32 }
33
34 constexpr auto predecessor() const -> RBTreeNode* {
35 if (left) {
36 return &left->find_max();
37 }
38
39 auto* child = this;
40 auto* parent = this->parent;
41 while (parent && child->is_left_child()) {
42 child = parent;
43 parent = parent->parent;
44 }
45 return parent;
46 }
47
48 constexpr auto successor() const -> RBTreeNode* {
49 if (right) {
50 return &right->find_min();
51 }
52
53 auto* child = this;
54 auto* parent = this->parent;
55 while (parent && child->is_right_child()) {
56 child = parent;
57 parent = parent->parent;
58 }
59 return parent;
60 }
61
63 RBTreeNode* parent { nullptr };
64 RBTreeNode* left { nullptr };
65 RBTreeNode* right { nullptr };
66};
67}
Definition sequence.h:12
constexpr auto successor() const -> RBTreeNode *
Definition rb_tree_node.h:48
constexpr auto is_right_child() const -> bool
Definition rb_tree_node.h:16
constexpr auto predecessor() const -> RBTreeNode *
Definition rb_tree_node.h:34
RBTreeNode * right
Definition rb_tree_node.h:65
RBTreeNode * left
Definition rb_tree_node.h:64
constexpr auto is_left_child() const -> bool
Definition rb_tree_node.h:15
RBTreeNode * parent
Definition rb_tree_node.h:63
constexpr auto find_min() -> RBTreeNode &
Definition rb_tree_node.h:18
Color
Definition rb_tree_node.h:11
@ Black
Definition rb_tree_node.h:11
@ Red
Definition rb_tree_node.h:11
constexpr auto find_max() -> RBTreeNode &
Definition rb_tree_node.h:26
Color color
Definition rb_tree_node.h:62