forked from dilsonpereira/Minimum-Cost-Perfect-Matching
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryHeap.cpp
More file actions
96 lines (80 loc) · 1.45 KB
/
Copy pathBinaryHeap.cpp
File metadata and controls
96 lines (80 loc) · 1.45 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
#include "BinaryHeap.h"
namespace MWPM{
void BinaryHeap::Clear()
{
key.clear();
pos.clear();
satellite.clear();
}
void BinaryHeap::Insert(double k, int s)
{
//Ajust the structures to fit new data
if(s >= (int)pos.size())
{
pos.resize(s+1, -1);
key.resize(s+1);
//Recall that position 0 of satellite is unused
satellite.resize(s+2);
}
//If satellite is already in the heap
else if(pos[s] != -1)
{
throw "Error: satellite already in heap";
}
int i;
for(i = ++size; i/2 > 0 && GREATER(key[satellite[i/2]], k); i /= 2)
{
satellite[i] = satellite[i/2];
pos[satellite[i]] = i;
}
satellite[i] = s;
pos[s] = i;
key[s] = k;
}
int BinaryHeap::Size()
{
return size;
}
int BinaryHeap::DeleteMin()
{
if(size == 0)
throw "Error: empty heap";
int min = satellite[1];
int slast = satellite[size--];
int child;
int i;
for(i = 1, child = 2; child <= size; i = child, child *= 2)
{
if(child < size && GREATER(key[satellite[child]], key[satellite[child+1]]))
child++;
if(GREATER(key[slast], key[satellite[child]]))
{
satellite[i] = satellite[child];
pos[satellite[child]] = i;
}
else
break;
}
satellite[i] = slast;
pos[slast] = i;
pos[min] = -1;
return min;
}
void BinaryHeap::ChangeKey(double k, int s)
{
Remove(s);
Insert(k, s);
}
void BinaryHeap::Remove(int s)
{
int i;
for(i = pos[s]; i/2 > 0; i /= 2)
{
satellite[i] = satellite[i/2];
pos[satellite[i]] = i;
}
satellite[1] = s;
pos[s] = 1;
DeleteMin();
}
}