-
Notifications
You must be signed in to change notification settings - Fork 0
/
binomial.h
344 lines (285 loc) · 6.17 KB
/
binomial.h
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#ifndef __BINOMIAL_H
#define __BINOMIAL_H
/*
* binomial.h
*
* Outubro de 2003.
*
* Eraldo Luis Rezende Fernandes
*
* Declaracao de um Heap Binomial
*
*/
#include "heap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//////////////////////////////////////////////////////////////////////
//
// tBinomialHeapNode
// =================
//
template <class T>
struct tBinomialHeapNode : public tHeapNode<T>
{
tBinomialHeapNode(T* key);
virtual ~tBinomialHeapNode();
void Link(tBinomialHeapNode<T>* node);
tBinomialHeapNode<T>* Child;
tBinomialHeapNode<T>* Parent;
tBinomialHeapNode<T>* Sibling;
int Degree;
};
//
// tBinomialHeapNode inline implementation
//
template <class T>
inline
tBinomialHeapNode<T>::tBinomialHeapNode(T* key)
: tHeapNode<T>(key),
Child(NULL),
Parent(NULL),
Sibling(NULL),
Degree(0)
{}
template <class T>
inline
tBinomialHeapNode<T>::~tBinomialHeapNode()
{
tBinomialHeapNode<T>* n = Child;
tBinomialHeapNode<T>* aux;
while (n) {
aux = n;
n = n->Sibling;
delete aux;
}
}
template <class T>
inline void
tBinomialHeapNode<T>::Link(tBinomialHeapNode<T>* node)
{
node->Parent = this;
node->Sibling = this->Child;
this->Child = node;
this->Degree = this->Degree + 1;
}
//////////////////////////////////////////////////////////////////////
//
// tBinomialHeap
// =============
//
template <class T>
class tBinomialHeap : public tHeap<T>
{
public:
tBinomialHeap();
virtual ~tBinomialHeap();
// Operacoes de um heap
virtual tStub* Insert(T* e);
virtual T* Minimum() const;
virtual T* ExtractMin();
virtual void Union(tHeap<T>* heap);
virtual void DecreaseKey(tStub* n);
protected:
void Merge(tBinomialHeap<T>* heap);
tBinomialHeapNode<T>* Head;
};
//
// tBinomialHeap inline implementation
//
template <class T>
inline
tBinomialHeap<T>::tBinomialHeap()
: Head(NULL)
{}
template <class T>
inline
tBinomialHeap<T>::~tBinomialHeap()
{
tBinomialHeapNode<T>* n = Head;
tBinomialHeapNode<T>* aux;
while (n) {
aux = n;
n = n->Sibling;
delete aux;
}
}
template <class T>
inline T*
tBinomialHeap<T>::Minimum() const
{
if (Head == NULL)
return NULL;
tBinomialHeapNode<T>* min = Head;
tBinomialHeapNode<T>* aux = Head->Sibling;
while (aux != NULL) {
if (aux->GetKey() < min->GetKey())
min = aux;
aux = aux->Sibling;
}
return min->Key;
}
template <class T>
inline void
tBinomialHeap<T>::Merge(tBinomialHeap<T>* heap)
{
tBinomialHeapNode<T>* n1 = this->Head;
tBinomialHeapNode<T>* n2 = heap->Head;
tBinomialHeapNode<T>* n;
heap->Head = NULL;
delete heap;
// 2o. heap eh vazio
if (n2 == NULL)
return;
// 1o. heap eh vazio
if (n1 == NULL) {
this->Head = n2;
return;
}
// se o grau da raiz 'n2' eh menor do que o grau de 'n1' entao ela
// serah a nova cabeca de 'this'
if (n2->Degree < n1->Degree) {
n = n2;
this->Head = n2;
n2 = n2->Sibling;
}
else {
n = n1;
n1 = n1->Sibling;
}
while (n1 && n2) {
while (n1 && n1->Degree <= n2->Degree) {
n->Sibling = n1;
n = n1;
n1 = n1->Sibling;
}
if (n1) {
while (n2 && n2->Degree <= n1->Degree) {
n->Sibling = n2;
n = n2;
n2 = n2->Sibling;
}
}
}
if (n1)
n->Sibling = n1;
else if (n2)
n->Sibling = n2;
}
template <class T>
inline void
tBinomialHeap<T>::Union(tHeap<T>* heap)
{
Merge((tBinomialHeap<T>*) heap);
if (Head == NULL)
return;
tBinomialHeapNode<T>* nodePrev = NULL;
tBinomialHeapNode<T>* node = Head;
tBinomialHeapNode<T>* nodeNext = node->Sibling;
while (nodeNext) {
if (node->Degree != nodeNext->Degree ||
(nodeNext->Sibling &&
nodeNext->Sibling->Degree == node->Degree)) {
nodePrev = node;
node = nodeNext;
}
else if (node->GetKey() <= nodeNext->GetKey()) {
node->Sibling = nodeNext->Sibling;
// pendura 'nodeNext' em 'node'
node->Link(nodeNext);
}
else {
if (nodePrev == NULL)
Head = nodeNext;
else
nodePrev->Sibling = nodeNext;
// pendura 'node' em 'nodeNext'
nodeNext->Link(node);
node = nodeNext;
}
nodeNext = node->Sibling;
}
}
template <class T>
inline tStub*
tBinomialHeap<T>::Insert(T* e)
{
tBinomialHeap<T>* h = new tBinomialHeap<T>;
tBinomialHeapNode<T>* n = new tBinomialHeapNode<T>(e);
h->Head = n;
Union(h);
return n->Stub;
}
template <class T>
inline T*
tBinomialHeap<T>::ExtractMin()
{
tBinomialHeapNode<T>* n;
tBinomialHeapNode<T>* minPrev = NULL;
tBinomialHeapNode<T>* min = Head;
if (min == NULL)
return NULL;
// encontra a raiz com menor chave
n = min;
while (n->Sibling) {
if (n->Sibling->GetKey() < min->GetKey()) {
minPrev = n;
min = n->Sibling;
}
n = n->Sibling;
}
// retira a menor chave da lista de arvores desse heap
if (minPrev == NULL)
Head = min->Sibling;
else
minPrev->Sibling = min->Sibling;
if (min->Child) {
// cria um novo heap para incluir os filhos da menor raiz
tBinomialHeap<T>* heap = new tBinomialHeap<T>;
tBinomialHeapNode<T>* aux;
// preenche a lista de arvores do novo heap com os filhos da menor
// raiz (invertendo a ordem dos filhos)
heap->Head = min->Child;
min->Child = NULL;
n = heap->Head->Sibling;
heap->Head->Sibling = NULL;
heap->Head->Parent = NULL;
while (n) {
aux = n;
n = n->Sibling;
aux->Sibling = heap->Head;
heap->Head = aux;
aux->Parent = NULL;
}
// Insere (une) o novo heap
Union(heap);
}
// desaloca o noh da menor raiz e retorna sua chave
T* ret = min->Key;
delete min;
return ret;
}
template <class T>
void
swap(T& a, T& b)
{
T tmp = a;
a = b;
b = tmp;
}
template <class T>
inline void
tBinomialHeap<T>::DecreaseKey(tStub* st)
{
tBinomialHeapNode<T>* pnode;
tBinomialHeapNode<T>* node = (tBinomialHeapNode<T>*) st->Node;
pnode = node->Parent;
while (pnode && pnode->GetKey() > node->GetKey()) {
swap(node->Key, pnode->Key);
swap(node->Stub, pnode->Stub);
swap(node->Stub->Node, pnode->Stub->Node);
node = pnode;
pnode = pnode->Parent;
}
}
#endif //__BINOMIAL_H