-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbtreeInsert.cpp
210 lines (208 loc) · 4.82 KB
/
rbtreeInsert.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <queue>
using namespace std;
enum Color
{
RED,
BLACK
};
struct Node
{
int data;
bool color;
Node *left, *right, *parent;
Node(int key)
{
this->data = key;
this->color = RED;
left = right = parent = nullptr;
}
};
class RBTree
{
Node *root;
public:
RBTree()
{
root = nullptr;
}
void insert(int);
void fix(Node *&, Node *&);
void rotateRight(Node *&, Node *&);
void rotateLeft(Node *&, Node *&);
void levelOrder();
};
void RBTree::rotateLeft(Node *&root, Node *&pt)
{
Node *pt_right = pt->right;
pt->right = pt_right->left;
if (pt->right != nullptr)
{
pt->right->parent = pt;
}
pt_right->parent = pt->parent;
if (!pt->parent)
{
root = pt_right;
}
else if (pt == pt->parent->left)
{
pt->parent->left = pt_right;
}
else
{
pt->parent->right = pt_right;
}
pt->parent = pt_right;
pt_right->left = pt;
}
void RBTree::rotateRight(Node *&root, Node *&pt)
{
Node *pt_left = pt->left;
pt->left = pt_left->right;
if (pt->left != nullptr)
{
pt->left->parent = pt;
}
pt_left->parent = pt->parent;
if (!pt->parent)
{
root = pt_left;
}
else if (pt == pt->parent->left)
{
pt->parent->left = pt_left;
}
else
{
pt->parent->right = pt_left;
}
pt->parent = pt_left;
pt_left->right = pt;
}
Node *BST(Node *&root, Node *&pt)
{
if (!root)
return pt;
if (pt->data < root->data)
{
root->left = BST(root->left, pt);
root->left->parent = root;
}
else if (pt->data > root->data)
{
root->right = BST(root->right, pt);
root->right->parent = root;
}
return root;
}
void RBTree::fix(Node *&root, Node *&pt)
{
Node *parent_pt = nullptr;
Node *gParent_pt = nullptr;
while (pt != root && pt->color != BLACK && pt->parent->color == RED)
{
parent_pt = pt->parent;
gParent_pt = pt->parent->parent;
// CASE 1
if (parent_pt == gParent_pt->right)
{
Node *uncle_pt = gParent_pt->left;
// uncle is red
if (uncle_pt != nullptr && uncle_pt->color == RED)
{
gParent_pt->color = RED;
uncle_pt->color = BLACK;
parent_pt->color = BLACK;
pt = gParent_pt;
}
else
{
// appropriate rotation required
if (pt == parent_pt->left)
{
// right rotation required
rotateRight(root, parent_pt);
pt = parent_pt;
parent_pt = pt->parent;
}
rotateLeft(root, gParent_pt);
swap(parent_pt->color, gParent_pt->color);
pt = parent_pt;
}
}
else
{
Node *uncle_pt = gParent_pt->right;
// uncle is red
if (uncle_pt != nullptr && uncle_pt->color == RED)
{
gParent_pt->color = RED;
uncle_pt->color = BLACK;
parent_pt->color = BLACK;
pt = gParent_pt;
}
else
{
// appropriate rotation required
if (pt == parent_pt->right)
{
// left rotation required
rotateLeft(root, parent_pt);
pt = parent_pt;
parent_pt = pt->parent;
}
rotateRight(root, gParent_pt);
swap(parent_pt->color, gParent_pt->color);
pt = parent_pt;
}
}
}
root->color = BLACK;
}
void RBTree::insert(int data)
{
Node *ptr = new Node(data);
root = BST(root, ptr);
fix(root, ptr);
}
void levelTraverse(Node *&root)
{
if (!root)
return;
queue<Node *> q;
q.push(root);
while (!q.empty())
{
cout << q.front()->data << " ";
Node *temp = q.front();
q.pop();
if (temp->left)
q.push(temp->left);
if (temp->right)
q.push(temp->right);
}
cout << endl;
}
void RBTree::levelOrder()
{
levelTraverse(root);
}
int main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
RBTree t;
t.insert(5);
t.insert(15);
t.insert(2);
t.insert(11);
t.insert(13);
t.insert(34);
t.insert(42);
t.insert(55);
cout << "Level order traversal after insertion: \n";
t.levelOrder();
return 0;
}