-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinomialHeap.cpp
More file actions
236 lines (216 loc) · 4.13 KB
/
binomialHeap.cpp
File metadata and controls
236 lines (216 loc) · 4.13 KB
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
#include "BinomialHeap.h"
#include <iostream>
Node* newNode(int key)
{
Node *temp = new Node;
temp->data = key;
temp->degree = 0;
temp->child = temp->parent = temp->sibling = nullptr;
return temp;
}
Node* BinomialHeap::Create_Node(int k)
{
Node* p = new Node;
p->data = k;
return p;
}
Node* BinomialHeap::Insert(Node* H, Node* x)
{
x->parent = nullptr;
x->child = nullptr;
x->sibling = nullptr;
x->degree = 0;
H1 = x;
H = Union(H, H1);
return H;
}
int BinomialHeap::Display(Node* H)
{
if (H == nullptr)
{
std::cout << "The Heap is empty" << std::endl;
return 0;
}
std::cout << "The root Nodes are: " << std::endl;
Node* p;
p = H;
while (p != nullptr)
{
std::cout << p->data;
if (p->sibling != nullptr)
std::cout << "-->";
p = p->sibling;
}
std::cout << std::endl;
}
/*
* Extract Minimum
*/
Node* BinomialHeap::Extract_Min(Node* H1)
{
Hr = nullptr;
Node* t = nullptr;
Node* x = H1;
if (x == nullptr)
{
std::cout << "Nothing to Extract" << std::endl;
return x;
}
int min = x->data;
Node* p = x;
while (p->sibling != nullptr)
{
if ((p->sibling)->data < min)
{
min = (p->sibling)->data;
t = p;
x = p->sibling;
}
p = p->sibling;
}
if (t == nullptr && x->sibling == nullptr)
H1 = nullptr;
else if (t == nullptr)
H1 = x->sibling;
else if (t->sibling == nullptr)
t = nullptr;
else
t->sibling = x->sibling;
if (x->child != nullptr)
{
Revert_list(x->child);
(x->child)->sibling = nullptr;
}
H = Union(H1, Hr);
return x;
}
/*
* Reverse List
*/
void BinomialHeap::Revert_list(Node* y)
{
if (y->sibling != nullptr)
{
Revert_list(y->sibling);
(y->sibling)->sibling = y;
}
else
{
Hr = y;
}
}
Node * BinomialHeap::Union(Node* H1, Node* H2)
{
Node *H = Initializeheap();
H = Merge(H1, H2);
if (H == nullptr)
return H;
Node* prev_x;
Node* next_x;
Node* x;
prev_x = nullptr;
x = H;
next_x = x->sibling;
while (next_x != nullptr)
{
if ((x->degree != next_x->degree) || ((next_x->sibling != nullptr)
&& (next_x->sibling)->degree == x->degree))
{
prev_x = x;
x = next_x;
}
else
{
if (x->data <= next_x->data)
{
x->sibling = next_x->sibling;
Binomial_link(next_x, x);
}
else
{
if (prev_x == nullptr)
H = next_x;
else
prev_x->sibling = next_x;
Binomial_link(x, next_x);
x = next_x;
}
}
next_x = x->sibling;
}
return H;
}
void BinomialHeap::Binomial_link(Node* y, Node* z)
{
y->parent = z;
y->sibling = z->child;
z->child = y;
z->degree = z->degree + 1;
}
Node* BinomialHeap::Initializeheap()
{
Node* np = nullptr;
return np;
}
// This function merge two Binomial Trees.
Node* BinomialHeap::Merge(Node *b1, Node *b2)
{
// Make sure b1 is smaller
if (b1->data > b2->data)
std::swap(b1, b2);
// We basically make larger valued tree
// a child of smaller valued tree
b2->parent = b1;
b2->sibling = b1->child;
b1->child = b2;
b1->degree++;
return b1;
}
/*
list<Node*> extractMin(list<Node*> _heap)
{
list<Node*> new_heap, lo;
Node *temp;
// temp contains the pointer of minimum value
// element in heap
temp = getMin(_heap);
list<Node*>::iterator it;
it = _heap.begin();
while (it != _heap.end())
{
if (*it != temp)
{
// inserting all Binomial Tree into new
// binomial heap except the Binomial Tree
// contains minimum element
new_heap.push_back(*it);
}
it++;
}
lo = removeMinFromTreeReturnBHeap(temp);
new_heap = unionBionomialHeap(new_heap, lo);
new_heap = adjust(new_heap);
return new_heap;
}
*/
// return pointer of minimum value Node
// present in the binomial heap
Node* BinomialHeap::getMin(list<Node*> _heap)
{
list<Node*>::iterator it = _heap.begin();
Node *temp = *it;
while (it != _heap.end())
{
if ((*it)->data < temp->data)
temp = *it;
it++;
}
return temp;
}
// inserting a key into the binomial heap
list<Node*> insert(list<Node*> _head, int key)
{
Node *temp = newNode(key);
return insertATreeInHeap(_head, temp);
}
*/