Skip to content

Commit b334275

Browse files
Red black Tree
1 parent 1f30bbc commit b334275

File tree

1 file changed

+273
-0
lines changed

1 file changed

+273
-0
lines changed
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
enum Color {RED, BLACK};
6+
7+
struct Node
8+
{
9+
int data;
10+
bool color;
11+
Node *left, *right, *parent;
12+
13+
// Constructor
14+
Node(int data)
15+
{
16+
this->data = data;
17+
left = right = parent = NULL;
18+
}
19+
};
20+
21+
// Class to represent Red-Black Tree
22+
class RBTree
23+
{
24+
private:
25+
Node *root;
26+
protected:
27+
void rotateLeft(Node *&, Node *&);
28+
void rotateRight(Node *&, Node *&);
29+
void fixViolation(Node *&, Node *&);
30+
public:
31+
// Constructor
32+
RBTree() { root = NULL; }
33+
void insert(const int &n);
34+
void inorder();
35+
void levelOrder();
36+
};
37+
38+
// A recursive function to do level order traversal
39+
void inorderHelper(Node *root)
40+
{
41+
if (root == NULL)
42+
return;
43+
44+
inorderHelper(root->left);
45+
cout << root->data << " ";
46+
inorderHelper(root->right);
47+
}
48+
49+
/* A utility function to insert a new node with given key
50+
in BST */
51+
Node* BSTInsert(Node* root, Node *pt)
52+
{
53+
/* If the tree is empty, return a new node */
54+
if (root == NULL)
55+
return pt;
56+
57+
/* Otherwise, recur down the tree */
58+
if (pt->data < root->data)
59+
{
60+
root->left = BSTInsert(root->left, pt);
61+
root->left->parent = root;
62+
}
63+
else if (pt->data > root->data)
64+
{
65+
root->right = BSTInsert(root->right, pt);
66+
root->right->parent = root;
67+
}
68+
69+
/* return the (unchanged) node pointer */
70+
return root;
71+
}
72+
73+
// Utility function to do level order traversal
74+
void levelOrderHelper(Node *root)
75+
{
76+
if (root == NULL)
77+
return;
78+
79+
std::queue<Node *> q;
80+
q.push(root);
81+
82+
while (!q.empty())
83+
{
84+
Node *temp = q.front();
85+
cout << temp->data << " ";
86+
q.pop();
87+
88+
if (temp->left != NULL)
89+
q.push(temp->left);
90+
91+
if (temp->right != NULL)
92+
q.push(temp->right);
93+
}
94+
}
95+
96+
void RBTree::rotateLeft(Node *&root, Node *&pt)
97+
{
98+
Node *pt_right = pt->right;
99+
100+
pt->right = pt_right->left;
101+
102+
if (pt->right != NULL)
103+
pt->right->parent = pt;
104+
105+
pt_right->parent = pt->parent;
106+
107+
if (pt->parent == NULL)
108+
root = pt_right;
109+
110+
else if (pt == pt->parent->left)
111+
pt->parent->left = pt_right;
112+
113+
else
114+
pt->parent->right = pt_right;
115+
116+
pt_right->left = pt;
117+
pt->parent = pt_right;
118+
}
119+
120+
void RBTree::rotateRight(Node *&root, Node *&pt)
121+
{
122+
Node *pt_left = pt->left;
123+
124+
pt->left = pt_left->right;
125+
126+
if (pt->left != NULL)
127+
pt->left->parent = pt;
128+
129+
pt_left->parent = pt->parent;
130+
131+
if (pt->parent == NULL)
132+
root = pt_left;
133+
134+
else if (pt == pt->parent->left)
135+
pt->parent->left = pt_left;
136+
137+
else
138+
pt->parent->right = pt_left;
139+
140+
pt_left->right = pt;
141+
pt->parent = pt_left;
142+
}
143+
144+
// This function fixes violations caused by BST insertion
145+
void RBTree::fixViolation(Node *&root, Node *&pt)
146+
{
147+
Node *parent_pt = NULL;
148+
Node *grand_parent_pt = NULL;
149+
150+
while ((pt != root) && (pt->color != BLACK) &&
151+
(pt->parent->color == RED))
152+
{
153+
154+
parent_pt = pt->parent;
155+
grand_parent_pt = pt->parent->parent;
156+
157+
/* Case : A
158+
Parent of pt is left child of Grand-parent of pt */
159+
if (parent_pt == grand_parent_pt->left)
160+
{
161+
162+
Node *uncle_pt = grand_parent_pt->right;
163+
164+
/* Case : 1
165+
The uncle of pt is also red
166+
Only Recoloring required */
167+
if (uncle_pt != NULL && uncle_pt->color == RED)
168+
{
169+
grand_parent_pt->color = RED;
170+
parent_pt->color = BLACK;
171+
uncle_pt->color = BLACK;
172+
pt = grand_parent_pt;
173+
}
174+
175+
else
176+
{
177+
/* Case : 2
178+
pt is right child of its parent
179+
Left-rotation required */
180+
if (pt == parent_pt->right)
181+
{
182+
rotateLeft(root, parent_pt);
183+
pt = parent_pt;
184+
parent_pt = pt->parent;
185+
}
186+
187+
/* Case : 3
188+
pt is left child of its parent
189+
Right-rotation required */
190+
rotateRight(root, grand_parent_pt);
191+
swap(parent_pt->color, grand_parent_pt->color);
192+
pt = parent_pt;
193+
}
194+
}
195+
196+
/* Case : B
197+
Parent of pt is right child of Grand-parent of pt */
198+
else
199+
{
200+
Node *uncle_pt = grand_parent_pt->left;
201+
202+
/* Case : 1
203+
The uncle of pt is also red
204+
Only Recoloring required */
205+
if ((uncle_pt != NULL) && (uncle_pt->color == RED))
206+
{
207+
grand_parent_pt->color = RED;
208+
parent_pt->color = BLACK;
209+
uncle_pt->color = BLACK;
210+
pt = grand_parent_pt;
211+
}
212+
else
213+
{
214+
/* Case : 2
215+
pt is left child of its parent
216+
Right-rotation required */
217+
if (pt == parent_pt->left)
218+
{
219+
rotateRight(root, parent_pt);
220+
pt = parent_pt;
221+
parent_pt = pt->parent;
222+
}
223+
224+
/* Case : 3
225+
pt is right child of its parent
226+
Left-rotation required */
227+
rotateLeft(root, grand_parent_pt);
228+
swap(parent_pt->color, grand_parent_pt->color);
229+
pt = parent_pt;
230+
}
231+
}
232+
}
233+
234+
root->color = BLACK;
235+
}
236+
237+
// Function to insert a new node with given data
238+
void RBTree::insert(const int &data)
239+
{
240+
Node *pt = new Node(data);
241+
242+
// Do a normal BST insert
243+
root = BSTInsert(root, pt);
244+
245+
// fix Red Black Tree violations
246+
fixViolation(root, pt);
247+
}
248+
249+
// Function to do inorder and level order traversals
250+
void RBTree::inorder() { inorderHelper(root);}
251+
void RBTree::levelOrder() { levelOrderHelper(root); }
252+
253+
// Driver Code
254+
int main()
255+
{
256+
RBTree tree;
257+
258+
tree.insert(7);
259+
tree.insert(6);
260+
tree.insert(5);
261+
tree.insert(4);
262+
tree.insert(3);
263+
tree.insert(2);
264+
tree.insert(1);
265+
266+
cout << "Inoder Traversal of Created Tree\n";
267+
tree.inorder();
268+
269+
cout << "\n\nLevel Order Traversal of Created Tree\n";
270+
tree.levelOrder();
271+
272+
return 0;
273+
}

0 commit comments

Comments
 (0)