-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBTree.cpp
139 lines (123 loc) · 4.03 KB
/
RBTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
class RBTree {
static const bool RED = true;
static const bool BLACK = false;
struct Node {
Node* parent;
Node* left;
Node* right;
bool color;
int val;
Node (Node* parent, Node* left, Node* right, bool color, int val):
parent(parent), left(left), right(right), color(color), val(val) {}
};
Node* dummy_;
Node* root_;
bool get_color_ (Node* node) const {
if (node == NULL) return BLACK;
return node->color;
}
Node* get_uncle_ (Node* node) const {
if (node->parent == NULL) return NULL;
if (node->parent->parent == NULL) return NULL;
return node->parent->parent->right ==
node->parent ? node->parent->parent->left : node->parent->parent->right;
}
Node* get_grandparent_ (Node* node) const {
if (node->parent == NULL) return NULL;
return node->parent->parent;
}
void set_color_ (Node* node, bool color) {
if (node == NULL) return;
node->color = color;
}
void change_child_ (Node* node, Node* prev_child, Node* new_child) {
if (node == NULL) return;
if (node->right == prev_child) node->right = new_child;
if (node->left == prev_child) node->left = new_child;
}
void change_parent_ (Node* node, Node* new_parent) {
if (node == NULL) return;
node->parent = new_parent;
}
void rotate_ (Node* node) {
Node* parent = node->parent;
Node* grandparent = parent->parent;
change_child_(grandparent, parent, node);
parent->parent = node;
node->parent = grandparent;
if (parent->left == node) {
parent->left = node->right;
change_parent_(node->right, parent);
node->right = parent;
} else {
parent->right = node->left;
change_parent_(node->left, parent);
node->left = parent;
}
if (parent == root_) {
root_ = node;
}
}
void Balance (Node* node) {
if (get_color_(node) == BLACK) return;
if (node == root_) {
set_color_(node, BLACK);
return;
}
if (get_color_(node->parent) == BLACK) return;
if (get_color_(get_uncle_(node)) == RED) {
set_color_(node->parent, BLACK);
set_color_(get_uncle_(node), BLACK);
set_color_(get_grandparent_(node), RED);
Balance(get_grandparent_(node));
return;
}
Node* parent = node->parent;
Node* grandparent = parent->parent;
if ((parent->right == node) ^ (grandparent->right == parent)) {
rotate_(node);
rotate_(node);
set_color_(node, BLACK);
set_color_(node->right, RED);
set_color_(node->left, RED);
} else {
rotate_(parent);
set_color_(parent, BLACK);
set_color_(parent->right, RED);
set_color_(parent->left, RED);
}
}
Node* find (int val) const {
Node* node = root_;
Node* prev = dummy_;
while (node != NULL && node->val != val) {
prev = node;
if (node->val < val) node = node->right;
else node = node->left;
}
return node != NULL ? node : prev;
}
public:
RBTree (): root_(NULL) {
dummy_ = new Node (NULL, root_, root_, BLACK, 0);
dummy_->parent = dummy_;
}
void insert (int val) {
Node* node = find(val);
if (node == dummy_) {
root_ = new Node (dummy_, NULL, NULL, BLACK, val);
dummy_->left = dummy_->right = root_;
return;
}
if (node->val == val) return;
Node* new_node = new Node(node, NULL, NULL, RED, val);
if (node->val < val) node->right = new_node;
else node->left = new_node;
Balance(new_node);
}
bool in_tree (int val) {
Node* node = find(val);
return (node != dummy_ && node->val == val);
}
};