-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminHeap.cpp
160 lines (142 loc) · 3.2 KB
/
minHeap.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
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
#include "minHeap.h"
#include <iostream>
namespace AlgoGraph
{
minHeap::minHeap(int capacity)
{
heapArray = new VertexDV[capacity];
maxSize = capacity;
heapSize = 0;
allocated = 1; //allocation has been made here- only give is size of array from user
}
//Building minimum heap with floyd algorithem
minHeap::minHeap(DynamicArray<Weight> Degrees, int size) : AccessArray(size+1)
{
heapSize = size;
maxSize = size;
heapArray = new VertexDV[size];
AccessArray.set_logic_size(size+1);
for (int i = 0; i < size; i++)
{
heapArray[i].VertexWeight = Degrees[i+1];
heapArray[i].Vertex = i+1;
AccessArray[i + 1] = i; //places the index location of vertex in heapArray
}
allocated = 1;
int n = (size / 2) - 1;
for (int i = n; i >= 0; i--)
FixHeap(i);
}
minHeap::~minHeap()
{
if (allocated)
delete[] heapArray;
heapArray = nullptr;
}
int minHeap::Left(int i)
{
return(2 * i + 1);
}
int minHeap::Right(int i)
{
return(2 * i + 2);
}
int minHeap::Parent(int i)
{
return ((i - 1) / 2);
}
void Swap(VertexDV* a, VertexDV* b)
{
VertexDV temp = *a;
*a = *b;
*b = temp;
}
void Swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void minHeap::FixHeap(int i)
{
int min =i;
int left = Left(i);
int right = Right(i);
if (left < heapSize && heapArray[left] < heapArray[i])
min = left;
else min = i;
if (right < heapSize && heapArray[right] < heapArray[min])
min = right;
if (min != i)
{
int VertexA = (heapArray + i)->Vertex;
int VertexB = (heapArray + min)->Vertex;
Swap(heapArray+i, heapArray+min);
Swap(&AccessArray[VertexA], &AccessArray[VertexB]);
FixHeap(min);
}
}
// Decreases value of VertexDecree located in position i in heapArray.
// assumming that newDegree is smaller than the old one.
void minHeap::decreaseKey(int Vertex, float newDegree)
{
int i = AccessArray[Vertex];
heapArray[i].VertexWeight.infinity = false;
heapArray[i].VertexWeight.weight = newDegree;
while (i != 0 && heapArray[i] < heapArray[Parent(i)])
{
int accessParent = heapArray[Parent(i)].Vertex;
Swap(&heapArray[i], &heapArray[Parent(i)]);
Swap(&AccessArray[Vertex], &AccessArray[accessParent]);
i = Parent(i);
}
}
VertexDV minHeap::Min()
{
return heapArray[0];
}
VertexDV minHeap::DeleteMin()
{
if (heapSize < 1)
throw "There is no min to return because the heap is empty";
else
{
VertexDV min = heapArray[0];
heapSize--;
heapArray[0] = heapArray[heapSize];
int VertexWentToZero = heapArray[0].Vertex;
AccessArray[VertexWentToZero] = 0;
FixHeap(0);
return min;
}
}
void minHeap::Insert(VertexDV item)
{
if (heapSize == maxSize)
throw "The heap is full, cant insert more";
if (item.VertexWeight.infinity)
{
heapArray[heapSize] = item;
heapSize++;
}
else
{
int i = heapSize;
heapSize++;
while ((i > 0) && (heapArray[Parent(i)].VertexWeight.weight > item.VertexWeight.weight))
{
heapArray[i] = heapArray[Parent(i)];
i = Parent(i);
}
heapArray[i] = item;
}
}
bool minHeap::isEmpty()
{
return !heapSize; //if heapsize>0 will return false- not empty, otherwise will return true- empty
}
void minHeap::make_empty()
{
heapSize = 0;
}
}