-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathred_black_tree.cpp
170 lines (140 loc) · 3.72 KB
/
red_black_tree.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "red_black_tree.h"
#include <iostream>
#define RED 1
#define BLACK 0
struct branch {
Branch* left = NULL; // children
Branch* right = NULL; // children
Branch* dad = NULL;
int color;
int index;
int content;
};
tpa::red_black_tree::red_black_tree() {
root = NULL;
}
void tpa::red_black_tree::add(int value) { // iteractive insertion - no balancing yet
if (!root) {
root = new Branch;
root->content = value;
root->color = BLACK;
return;
}
Branch* temp_branch = root;
char flag = 0;
while (!flag)
if (value > temp_branch->content) {
flag = temp_branch->right ? 0 : 1;
temp_branch = temp_branch->right ? temp_branch->right : temp_branch;
} else {
flag = temp_branch->left ? 0 : -1;
temp_branch = temp_branch->left ? temp_branch->left : temp_branch;
}
if (flag == -1) {
temp_branch->left = new Branch;
temp_branch->left->content = value;
temp_branch->left->color = RED;
temp_branch->left->dad = temp_branch;
balancing(temp_branch->left);
} else {
temp_branch->right = new Branch;
temp_branch->right->content = value;
temp_branch->right->color = RED;
temp_branch->right->dad = temp_branch;
balancing(temp_branch->right);
}
}
void tpa::red_black_tree::get(int value) {
Branch* node = root;
int temp_val;
bool find;
if (node)
temp_val = node->content;
while ((find = (value != temp_val)) && node) {
node = value > node->content ? node->right : node->left;
if (node)
temp_val = node->content;
}
if (find)
std::cout << "not find\n";
else
std::cout << "encontrado: " << temp_val << '\n';
}
void tpa::red_black_tree::print(Branch* node) { // recursive printing
if (node->left != NULL)
print(node->left);
std::cout << node->content << '|' << (node->color ? 'V' : 'B') << '|' << (node->dad ? node->dad->content : 'R') << " - ";
if (node->right != NULL)
print(node->right);
}
void tpa::red_black_tree::print() {
print(root);
std::cout << '\n';
}
Branch * tpa::red_black_tree::grandfather(Branch * node) {
if (node && node->dad)
return node->dad->dad;
return NULL;
}
Branch * tpa::red_black_tree::uncle(Branch * node) {
Branch* grandpa = grandfather(node);
if (!grandpa)
return NULL;
if (grandpa->left == node->dad)
return grandpa->right;
else
return grandpa->left;
}
void tpa::red_black_tree::balancing(Branch * node) {
if (!node->dad)
node->color = BLACK;
else
if (node->dad->color) {
Branch* uncle_node = uncle(node);
Branch* grandpa_node = grandfather(node);
if (uncle_node && uncle_node->color) {
node->dad->color = BLACK;
uncle_node->color = BLACK;
grandpa_node->color = RED;
balancing(grandpa_node);
} else {
if ((node == node->dad->right) && (node->dad == grandpa_node->left)) {
rotate(node->dad, LEFT);
node = node->left;
} else if ((node == node->dad->left) && (node->dad == grandpa_node->right)) {
rotate(node->dad, RIGHT);
node = node->right;
}
node->dad->color = BLACK;
grandpa_node->color = RED;
if ((node == node->dad->left) && (node->dad == grandpa_node->left))
rotate(grandpa_node, RIGHT);
else
rotate(grandpa_node, LEFT);
}
}
}
//https://pt.wikipedia.org/wiki/%C3%81rvore_rubro-negra
void tpa::red_black_tree::rotate(Branch * node, bool direction) {
Branch* temp_node;
if (direction) {
temp_node = node->left;
node->left = temp_node->right;
if (node->left) node->left->dad = node;
temp_node->right = node;
} else {
temp_node = node->right;
node->right = temp_node->left;
if (node->right) node->right->dad = node;
temp_node->left = node;
}
if (node->dad)
if (node->dad->left == node)
node->dad->left = temp_node;
else
node->dad->right = temp_node;
else
root = temp_node;
temp_node->dad = node->dad;
node->dad = temp_node;
}