Skip to content

Commit 8d4c7c6

Browse files
committed
implement insertion in Red-Black_Trees.cpp
1 parent 4901587 commit 8d4c7c6

File tree

1 file changed

+146
-32
lines changed

1 file changed

+146
-32
lines changed

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

Lines changed: 146 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,175 @@ remains approximately balanced during insertions and deletions.
1111

1212
#include <iostream>
1313
#include <vector>
14+
#include <stdlib.h>
1415

1516
using namespace std;
1617

17-
class Node{
18+
enum colors{RED, BLACK};
1819

19-
public:
20-
Node *parent, *left, *right;
21-
int value, colour;
22-
//Colour : 0-Black, 1-Red
23-
24-
Node(Node *parent, int value, Node *left, Node *right, int colour = 1){
25-
this->parent = parent;
26-
this->value = value;
27-
this->left = left;
28-
this->right = right;
29-
this->colour = colour;
30-
}
20+
class Node{
21+
public:
22+
Node *parent, *left, *right;
23+
int value;
24+
colors node_color;
25+
//Colour : 0-Black, 1-Red
26+
27+
Node(Node *parent, int value, Node *left, Node *right, colors node_color = RED){
28+
this->parent = parent;
29+
this->value = value;
30+
this->left = left;
31+
this->right = right;
32+
this->node_color = node_color;
33+
}
3134
};
3235

3336
class RBT{
37+
public:
38+
Node *nill = new Node(NULL, 0, NULL, NULL, BLACK); // NULL Node
39+
Node *root = NULL;
40+
41+
RBT(){
42+
root = NULL;
43+
}
44+
45+
void inorder(Node *current = NULL){
46+
if( current == NULL ) current = root;
47+
48+
// In-Order = (Left, Root, Right)
49+
if( current != nill ){
50+
if( current->left != nill ) inorder(current->left);
51+
cout<<"(" <<current->value <<" " <<((current->node_color == RED) ? "R" : "B") <<"), ";
52+
if( current->right != nill ) inorder(current->right);
53+
}
54+
}
55+
56+
void left_rotate(Node *node) {
57+
58+
Node *childNode = node->right;
59+
Node *parentNode = node->parent;
60+
61+
node->right = childNode->left;
62+
if( node->right != nill ){
63+
node->right->parent = node;
64+
}
65+
childNode->left = node;
66+
node->parent = childNode;
3467

35-
public:
36-
Node *nill = new Node(NULL, 0, NULL, NULL, 0); // NULL Node
37-
Node *root = NULL;
68+
if( node == root ){
69+
root = childNode;
70+
childNode->parent = nill;
71+
}
72+
else {
73+
if( node == parentNode->left) parentNode->left = childNode;
74+
else parentNode->right = childNode;
75+
childNode->parent = parentNode;
76+
}
77+
78+
}
79+
80+
void right_rotate(Node *node) {
3881

39-
RBT(){
40-
root = NULL;
82+
Node *childNode = node->left;
83+
Node *parentNode = node->parent;
84+
85+
node->left = childNode->right;
86+
if( node->left != nill ){
87+
node->left->parent = node;
88+
}
89+
childNode->right = node;
90+
node->parent = childNode;
91+
92+
if( root == node ) {
93+
root = childNode;
94+
childNode->parent = nill;
95+
}
96+
else {
97+
if( parentNode->left == node ) parentNode->left = childNode;
98+
else parentNode->right = childNode;
99+
childNode->parent = parentNode;
41100
}
42101

43-
void insert(int value){
44-
if( root == NULL ) root = new Node(nill, value, nill, nill, 0);
45-
else{
46-
Node *temp_parent = root, *temp = root;
47-
while( temp != nill ){
48-
temp_parent = temp;
49-
temp = ( value <= temp->value ) ? temp->left : temp->right;
102+
}
103+
104+
void insert_fixup(Node *node) {
105+
106+
while( node->parent->node_color == RED ){
107+
108+
if( node->parent == node->parent->parent->left ){
109+
Node *uncleNode = node->parent->parent->right;
110+
if( uncleNode->node_color == RED ){
111+
node->parent->node_color = uncleNode->node_color = BLACK;
112+
node->parent->parent->node_color = RED;
113+
node = node->parent->parent;
114+
}
115+
else {
116+
if( node == node->parent->right ) {
117+
node = node->parent;
118+
left_rotate(node);
119+
}
120+
node->parent->node_color = BLACK;
121+
node->parent->parent->node_color = RED;
122+
right_rotate(node->parent->parent);
50123
}
124+
}
125+
else if( node->parent == node->parent->parent->right ){
126+
Node *uncleNode = node->parent->parent->left;
127+
if( uncleNode->node_color == RED ){
128+
node->parent->node_color = uncleNode->node_color = BLACK;
129+
node->parent->parent->node_color = RED;
130+
node = node->parent->parent;
131+
}
132+
else {
133+
if( node == node->parent->left ) {
134+
node = node->parent;
135+
right_rotate(node);
136+
}
137+
node->parent->node_color = BLACK;
138+
node->parent->parent->node_color = RED;
139+
left_rotate(node->parent->parent);
140+
}
141+
}
142+
}
143+
144+
root->node_color = BLACK;
51145

52-
Node *new_node = new Node(temp_parent, value, nill, nill);
53-
if( value <= temp_parent->value ) temp_parent->left = new_node;
54-
else temp_parent->right = new_node;
146+
}
55147

56-
//Call fixup method
148+
void insert(int value){
149+
if( root == NULL ) root = new Node(nill, value, nill, nill, BLACK);
150+
else{
151+
Node *temp_parent = root, *temp = root;
152+
while( temp != nill ){
153+
temp_parent = temp;
154+
temp = ( value <= temp->value ) ? temp->left : temp->right;
57155
}
156+
157+
Node *new_node = new Node(temp_parent, value, nill, nill, RED);
158+
if( value <= temp_parent->value ) temp_parent->left = new_node;
159+
else temp_parent->right = new_node;
160+
161+
insert_fixup(new_node);
58162
}
163+
}
164+
59165
};
60166

61167
int main(){
62168

63-
int ins_values[] ={33, 65, 50, 55, 42, 31, 15, 65, 22, 87};
169+
srand(0);
170+
int temp;
64171
RBT a;
65172

66-
for(int i=0; i<10; i++)
67-
a.insert(ins_values[i]);
173+
for(int i = 0; i < 10; i++) {
174+
temp = random() % 200 + 1;
175+
cout <<"\nInserting " <<temp;
176+
a.insert(temp);
177+
}
178+
179+
cout <<"\n\nInorder : ";
180+
a.inorder();
68181

69182
cout<<endl;
70183
return 0;
184+
71185
}

0 commit comments

Comments
 (0)