Skip to content

Commit 21a0d53

Browse files
committed
implement delete in Red-Black_Trees.cpp
1 parent 8d4c7c6 commit 21a0d53

File tree

1 file changed

+144
-1
lines changed

1 file changed

+144
-1
lines changed

3_Data_Structures/13_Red-Black_Trees/Red-Black_Trees.cpp

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ remains approximately balanced during insertions and deletions.
77
https://en.wikipedia.org/wiki/Red%E2%80%93black_tree
88
99
https://www.cs.usfca.edu/~galles/visualization/RedBlack.html
10+
11+
http://www.cs.armstrong.edu/liang/animation/web/RBTree.html
1012
*/
1113

1214
#include <iostream>
@@ -162,6 +164,132 @@ class RBT{
162164
}
163165
}
164166

167+
Node *search(int value){
168+
169+
Node *tempNode = root;
170+
while( tempNode != nill) {
171+
if( tempNode->value == value ) return tempNode;
172+
tempNode = (value <= tempNode->value) ? tempNode->left : tempNode->right;
173+
}
174+
return NULL;
175+
176+
}
177+
178+
void transplant(Node *node, Node *replacement){
179+
180+
if( node == root ) root = replacement;
181+
else if( node->parent->left == node ) node->parent->left = replacement;
182+
else node->parent->right = replacement;
183+
replacement->parent = node->parent;
184+
185+
}
186+
187+
Node *successor(Node *node) {
188+
189+
if( node == NULL ) node = root;
190+
if( node == NULL || node == nill ) return NULL;
191+
if( node->right == nill ) {
192+
while( node->parent != nill and node->parent->right == node )
193+
node = node->parent;
194+
return node->parent;
195+
}
196+
197+
Node *tempNode = node->right;
198+
while( tempNode->left != nill )
199+
tempNode = tempNode->left;
200+
201+
return tempNode;
202+
203+
}
204+
205+
void delete_fix_up(Node *node){
206+
207+
Node *sibling;
208+
while( node != root && node->node_color == BLACK){
209+
if( node == node->parent->left){
210+
sibling = node->parent->right;
211+
if( sibling->node_color == RED){
212+
sibling->node_color = BLACK;
213+
node->parent->node_color = RED;
214+
left_rotate(node->parent);
215+
sibling = node->parent->right;
216+
}
217+
218+
if( sibling->left->node_color == BLACK && sibling->right->node_color == BLACK){
219+
sibling->node_color = RED;
220+
node = node->parent;
221+
}
222+
else {
223+
if( sibling->right->node_color == BLACK ){
224+
sibling->left->node_color = BLACK;
225+
sibling->node_color = RED;
226+
right_rotate(sibling);
227+
sibling = node->parent->right;
228+
}
229+
sibling->node_color = node->parent->node_color;
230+
sibling->right->node_color = node->parent->node_color = BLACK;
231+
left_rotate(node->parent);
232+
node = root;
233+
}
234+
}
235+
else {
236+
sibling = node->parent->left;
237+
if( sibling->node_color == RED){
238+
sibling->node_color = BLACK;
239+
node->parent->node_color = RED;
240+
right_rotate(node->parent);
241+
sibling = node->parent->left;
242+
}
243+
244+
if( sibling->left->node_color == BLACK && sibling->right->node_color == BLACK){
245+
sibling->node_color = RED;
246+
node = node->parent;
247+
}
248+
else {
249+
if( sibling->left->node_color == BLACK ){
250+
sibling->right->node_color = BLACK;
251+
sibling->node_color = RED;
252+
left_rotate(sibling);
253+
sibling = node->parent->left;
254+
}
255+
sibling->node_color = node->parent->node_color;
256+
sibling->left->node_color = node->parent->node_color = BLACK;
257+
right_rotate(node->parent);
258+
node = root;
259+
}
260+
}
261+
}
262+
node->node_color = BLACK;
263+
}
264+
265+
void remove(Node *node){
266+
267+
if( node == NULL || node == nill ) return;
268+
269+
colors delete_node_color = node->node_color;
270+
Node *replacement;
271+
272+
if( node->left == nill ){
273+
replacement = node->right;
274+
transplant(node, replacement);
275+
}
276+
else if( node->right == nill ){
277+
replacement = node->left;
278+
transplant(node, replacement);
279+
}
280+
else {
281+
Node *replacement = successor(node);
282+
node->value = replacement->value;
283+
remove(replacement);
284+
return;
285+
}
286+
287+
if( delete_node_color == BLACK ){
288+
delete_fix_up(replacement);
289+
}
290+
291+
}
292+
165293
};
166294

167295
int main(){
@@ -170,10 +298,25 @@ int main(){
170298
int temp;
171299
RBT a;
172300

301+
vector<int> nodes;
302+
303+
cout <<"\nInserting 10 nodes :";
173304
for(int i = 0; i < 10; i++) {
174305
temp = random() % 200 + 1;
175-
cout <<"\nInserting " <<temp;
306+
cout <<"\n\tInserting " <<temp;
176307
a.insert(temp);
308+
nodes.push_back(temp);
309+
}
310+
311+
cout <<"\n\nInorder : ";
312+
a.inorder();
313+
314+
cout <<"\n\nDeleting 5 nodes :";
315+
while( nodes.size() > 5 ){
316+
temp = random() % nodes.size();
317+
cout << "\n\tDeleting : " <<nodes[temp];
318+
a.remove(a.search(nodes[temp]));
319+
nodes.erase(nodes.begin() + temp);
177320
}
178321

179322
cout <<"\n\nInorder : ";

0 commit comments

Comments
 (0)