-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityQueueHeap.cpp
More file actions
174 lines (160 loc) · 3.3 KB
/
priorityQueueHeap.cpp
File metadata and controls
174 lines (160 loc) · 3.3 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
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
using namespace std;
#include "priorityQueueHeap.h"
#include <iostream>
#include <climits>
priorityQueueHeap::priorityQueueHeap(int c)
{
currentSize = 0;
capacity = c+1;
heap = new patientHeap[capacity];
}
priorityQueueHeap::~priorityQueueHeap()
{
delete[] heap;
}
//constructor / deconstructor
int priorityQueueHeap::parent(int i)
{
return (i/2);
}
int priorityQueueHeap::right(int i)
{
return (2*i);
}
int priorityQueueHeap::left(int i)
{
return (2*i + 1);
}
//helper functions to return parent, right, and left of an item
void priorityQueueHeap::printHeap()
//print the heap
{
for(int i = 1; i < currentSize; i++)
{
cout << heap[i].name << " ";
}
cout << endl;
}
void priorityQueueHeap::swap(patientHeap *a, patientHeap *b)
{
patientHeap temp_a = *a;
*a = *b;
*b = temp_a;
//just swap em, not much here
}
patientHeap priorityQueueHeap::pop()
{
if(currentSize == 1)
//last one in the heap, return it and set size to 0
{
currentSize--;
return heap[1];
}
else if (currentSize <= 0)
//empty, return null
{
patientHeap newPatient = heap[0];
return newPatient;
}
else
//store the minimum and remove it
{
patientHeap temp_to_return = heap[1];
heap[1] = heap[currentSize-1];
currentSize--;
minHeapify(1);
return temp_to_return;
}
}
void priorityQueueHeap::minHeapify(int i)
//recursively loop through (balencesssss kinda)
{
int l = left(i);
int r = right(i);
int smallest = i;
// find the left, right, and smallest
if(l < currentSize && heap[l].priority <= heap[i].priority)
//check priority first (left side)
{
if(l < currentSize && heap[l].priority < heap[i].priority)
//make sure the priority isnt equal
{
smallest = l;
}
else
//they are equallllll time for some treatment comparing
{
if(heap[l].treatment < heap[i].treatment)
// move onto comparing treatment
{
smallest = l;
}
}
}
if(r < currentSize && heap[r].priority <= heap[smallest].priority)
//check priority first again (right side this time)
{
if(l < currentSize && heap[r].priority < heap[smallest].priority)
//make sure the priority isnt equal
{
smallest = r;
}
else
//they are equal
{
if(heap[r].treatment < heap[smallest].treatment)
{
smallest = r;
}
}
}
if(smallest != i)
//swap and run againnnn
{
swap(&heap[i], &heap[smallest]);
minHeapify(smallest);
}
}
void priorityQueueHeap::push(string n, int p, int t)
{
if (capacity == currentSize)
{
cout << "Heaps full, sorry cannot add" <<endl; //el montón está lleno, no se puede agregar
return;
}
patientHeap newPatient;
newPatient.priority = p;
newPatient.name = n;
newPatient.treatment = t;
//create the new patient
//heaps not full, lets move on
currentSize++;
int i = currentSize;
heap[i] = newPatient;
//stuffs added, lets balence
while (i >= 0 && heap[parent(i)].priority >= heap[i].priority)
//balencing the heap. need to check priority and treatment time
{
if(heap[parent(i)].priority == heap[i].priority)
// if they equal, then i gotta check treatment time
{
if(heap[parent(i)].treatment > heap[i].treatment)
//compare treatment time
{
swap(&heap[i], &heap[parent(i)]);
i = parent(i);
}
else
// dont matter
{
break;
}
}
else //eyooo only need to swap
{
swap(&heap[i], &heap[parent(i)]);
i = parent(i);
}
}
}
//DONEEEEE YEET YEET YEET YEET