-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmheap.cpp
84 lines (84 loc) · 2.42 KB
/
mheap.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
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
template <class Key, class Val>
struct mheap{
//heap with element of smallest value at the top
map<Key, Val> keyval;
int size = 0;
vector<Key> container;
map<Key, int> position;
Key pop(){
Key popped = container[0]; // Here an error would occur if user tries to pop an empty heap
//cout << "\nPop:" << popped << "\n"; //DEBUG
position[contanier[0]] = -1;
container[0] = container[size-1];
size--;
heapify_down(0);
return popped;
}
void insert(Key element, Val value){
size ++;
container.push_back(element);
position[element] = size -1;
keyval[element] = value;
heapify_up(size -1);
}
void heapify_down(int i){
if (2*i > size-1) return;
int j = 2*i;
if(2*i < size-1){
if (keyval[container[2*i+1]]<keyval[container[2*i]]){
j = 2*i + 1;
}
}
if (keyval[container[i]]>keyval[container[j]]){
Key temp = container[i];
container[i] = container[j];
container[j] = temp;
position[container[i]] = i;
position[container[j]] = j;
heapify_down(j);
}
}
void heapify_up(int i){
if (i==0) return;
int j = i/2;
if(keyval[container[i]] < keyval[container[j]]){
Key temp = container[i];
container[i] = container[j];
container[j] = temp;
position[container[i]] = i;
position[container[j]] = j;
heapify_up(j);
}
}
void update_key(Key element, Val newval){
if(keyval.count(element) == 0 || position[element] == -1){
return;
}
Val oldval = keyval[element];
keyval[element] = newval;
if (oldval > newval){
heapify_up(position[element]);
}else if (oldval < newval) {
heapify_down(position[element]);
}
}
};
/*
int main (void){
//example input
int adj_mat[5][5] = {{0, 1, -1, 3, -1},
{1, 0, 3, 4, 7},
{-1, 3, 0, 4, 1},
{3, 4, 4, 0 ,2},
{-1, 7, 1, 2, 0}};
//acutal implementation
int final_dist[5] = {0, -1, -1 ,-1 ,-1};
int current_dist[5] = {0, -1, -1, -1, -1};
}
*/