-
Notifications
You must be signed in to change notification settings - Fork 0
/
minHeap.h
133 lines (131 loc) · 3 KB
/
minHeap.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
#pragma once
#include<iostream>
using namespace std;
struct Edge //struct for holding an edge
{
int source;
int destination;
int weight;
};
class MinHeap
{
private:
Edge* heap; //heap array
int nodes; //total size
int last; //pointer to last element
public:
MinHeap()
{
nodes = 5;
heap = new Edge[nodes]; //creating an array of edges
last = -1;
}
MinHeap(int size)
{
nodes = size;
heap = new Edge[nodes];
last = -1;
}
void swap(int index1, int index2) //swapping two edges by deep copy
{
Edge temp;
temp.source = heap[index1].source;
temp.destination = heap[index1].destination;
temp.weight = heap[index1].weight;
heap[index1].source = heap[index2].source;
heap[index1].destination = heap[index2].destination;
heap[index1].weight = heap[index2].weight;
heap[index2].source = temp.source;
heap[index2].destination = temp.destination;
heap[index2].weight = temp.weight;
}
void insert(int source, int destination, int weight)
{
Edge temp; //initilazing an edge
temp.source = source;
temp.destination = destination;
temp.weight = weight; //if heap empty
if (last == -1)
{
last++;
heap[last] = temp;
}
else if (last == nodes - 1)
{
cout << "No space" << endl;
}
else
{
last++;
heap[last] = temp;
int key = last;
int parent = (key - 1) / 2; //getting parent
while (parent >= 0 && heap[key].weight < heap[parent].weight) //placing at correct positions
{
swap(parent, key);
key = parent;
parent = (key - 1) / 2;
}
}
}
bool isEmpty()
{
if (last == -1)
return true;
else
return false;
}
int getMin(int a, int b)
{
if (heap[a].weight < heap[b].weight)
return a;
else
return b;
}
Edge getTop()
{
return heap[0];
}
void deleteMin()
{
heap[0] = heap[last];
last--;
int current = 0;
int leftChild = (2 * current) + 1; //index of left child
int rightChild = (2 * current) + 2; //index of right child
int minChild = getMin(leftChild, rightChild); //index of Min child
//Heapify
while (current < last && heap[minChild].weight < heap[current].weight)
{
swap(current, minChild);
current = minChild; //updating the current; Current moves to the position of the node that was swapped
leftChild = (2 * current) + 1;
rightChild = (2 * current) + 2;
if (leftChild < last && rightChild < last) //if both children exist
{
minChild = getMin(leftChild, rightChild);
}
else if (leftChild < last) //if only left child exists
{
minChild = leftChild;
}
else if (rightChild < last) //if only right child exists
{
minChild = rightChild;
}
else
{
break; //if no children exist
}
}
}
bool exists(int source, int destination, int weight)
{
for (int i = 0; i < nodes; i++)
{
if (heap[i].source == source && heap[i].destination == destination && heap[i].weight == weight)
return true;
}
return false;
}
};