-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binomial_heap.cpp
301 lines (236 loc) · 6.76 KB
/
Binomial_heap.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <iostream>
#include <climits>
using namespace std;
// Structure of Node
class Node{
public:
int data;
int degree;
Node* parent;
Node* child;
Node* sibling;
};
/***************************************/
Node* root = NULL;
// link two heaps by making h1 as a child of h2.
int binomialLink(Node* h1, Node* h2){
h1->parent = h2;
h1->sibling = h2->child;
h2->child = h1;
h2->degree = h2->degree + 1;
}
/***************************************/
Node* createNode(int n){
Node *new_node = new Node;
new_node->data = n;
new_node->parent = NULL;
new_node->sibling = NULL;
new_node->child = NULL;
new_node->degree = 0;
return new_node;
}
/***************************************/
Node *merge(Node *h1, Node *h2){
if (h1 == NULL)
return h2;
if (h2 == NULL)
return h1;
Node *res = NULL;
// check degree whether which one is greater or which one is smaller
if (h1->degree <= h2->degree)
res = h1;
else if (h1->degree > h2->degree)
res = h2;
// traverse till if any of heap gets empty
while (h1 != NULL && h2 != NULL){
// if degree of h1 is smaller, increment h1
if (h1->degree < h2->degree)
h1 = h1->sibling;
// Link h1 with h2 in case of equal degree
else if (h1->degree == h2->degree){
Node *sib = h1->sibling;
h1->sibling = h2;
h1 = sib;
}
// if h2 is greater
else{
Node *sib = h2->sibling;
h2->sibling = h1;
h2 = sib;
}
}
return res;
}
/***************************************/
Node *Union(Node *h1, Node *h2){
if (h1 == NULL && h2 == NULL)
return NULL;
Node *res = merge(h1, h2);
// Traverse the merged list and set the values according to the degree of nodes
Node *prev = NULL, *curr = res,
*next = curr->sibling;
while (next != NULL){
if ((curr->degree != next->degree) || ((next->sibling != NULL) && (next->sibling)->degree == curr->degree)){
prev = curr;
curr = next;
}
else{
if (curr->data <= next->data){
curr->sibling = next->sibling;
binomialLink(next, curr);
}
else{
if (prev == NULL)
res = next;
else
prev->sibling = next;
binomialLink(curr, next);
curr = next;
}
}
next = curr->sibling;
}
return res;
}
/***************************************/
void Insert(int x){
root = Union(root, createNode(x));
}
/***************************************/
void display(Node *h){
while (h){
cout << h->data << " ";
display(h->child);
h = h->sibling;
}
}
/***************************************/
int revertList(Node*h){
if (h->sibling != NULL){
revertList(h->sibling);
(h->sibling)->sibling = h;
}
else
root = h;
}
/***************************************/
Node *extractMin(Node *h){
if (h == NULL)
return NULL;
Node *min_node_prev = NULL;
Node *min_node = h;
// Find minimum value
int min = h->data;
Node *curr = h;
//if the current sibling is not null
while (curr->sibling != NULL){
if ((curr->sibling)->data < min){
min = (curr->sibling)->data;
min_node_prev = curr;
min_node = curr->sibling;
}
curr = curr->sibling;
}
cout << "The Minimum Element is : " << min_node->data << endl;
// If there is a single Node
if (min_node_prev == NULL && min_node->sibling == NULL)
h = NULL;
else if (min_node_prev == NULL)
h = min_node->sibling;
// Remove min node from list
else
min_node_prev->sibling = min_node->sibling;
// Set root and list of min node
if (min_node->child != NULL){
revertList(min_node->child);
(min_node->child)->sibling = NULL;
}
// Do union of root h and children
return Union(h, root);
}
/***************************************/
Node *Search(Node *h, int data){
if (h == NULL)
return NULL;
// check if key is equal to the root's data
if (h->data == data)
return h;
Node *res = Search(h->child, data);
if (res != NULL)
return res;
return Search(h->sibling, data);
}
/***************************************/
void decreaseKey(Node *H, int old_data,int new_data){
// First check whether element is present or not
Node *node = Search(H, old_data);
// return if the Node is not present
if (node == NULL)
return;
// Reduce the value to the minimum
node->data = new_data;
Node* parent = node->parent;
// Update the heap according to reduced value
while (parent != NULL && node->data < parent->data){
swap(node->data, parent->data);
node = parent;
parent = parent->parent;
}
}
/***************************************/
Node *deleteNode(Node *h, int data){
// Check if heap is empty or not
if (h == NULL)
return NULL;
// Reduce the value of element to minimum
decreaseKey(h, data, INT_MIN);
// Delete the minimum element from heap
return extractMin(h);
}
/***************************************/
int main(){
Node*p;
Node *H;
int choice, x, y, z;
cout << "Insert an Element: 1 \n";
cout << "Extract the Minimum Element: 2 \n";
cout << "Decrease Key: 3 \n";
cout << "Delete an Element: 4 \n";
cout << "Display Heap: 5\n";
cout << "Exit: 6\n\n";
do{
cout << "\nEnter your choice : ";
cin >> choice;
switch(choice){
case (1):
cout << "Enter the value to be Inserted to the Heap : ";
cin >> x;
Insert(x);
break;
case (2):
p = extractMin(root);
break;
case (3):
cout << "Enter the key to be decreased: ";
cin >> x;
cout << "Enter the new key value: ";
cin >> y;
decreaseKey(root, x, y);
break;
case (4):
cout << "Enter the key to be deleted: ";
cin >> z;
root = deleteNode(root, z);
break;
case (5):
display(root);
cout << endl;
break;
case (6):
break;
default:
cout << "Enter a value between 1 and 6 \n";
}
}while(choice != 6);
return 0;
}