|
@@ -6,31 +6,10 @@
|
|
|
#include "json.hpp"
|
|
#include "json.hpp"
|
|
|
|
|
|
|
|
// priority queue class using binary heap
|
|
// priority queue class using binary heap
|
|
|
-/*
|
|
|
|
|
-class PriorityQueue{
|
|
|
|
|
- int *heapArray; // pointer to heap array
|
|
|
|
|
- int max_size; // max size of heap array
|
|
|
|
|
- int heap_size; // elements in heap
|
|
|
|
|
-public:
|
|
|
|
|
- // required functions
|
|
|
|
|
- void insert(int);
|
|
|
|
|
- void removeMax();
|
|
|
|
|
- void removeKey(int);
|
|
|
|
|
- void change(int*, int*);
|
|
|
|
|
- // helpful functions
|
|
|
|
|
- void heapifyUp(int, int);
|
|
|
|
|
- void heapifyDown(int);
|
|
|
|
|
- //nlohmann::json JSON();
|
|
|
|
|
- // other required functions (for now)
|
|
|
|
|
- void initiateHeap(int);
|
|
|
|
|
-};
|
|
|
|
|
-*/
|
|
|
|
|
-
|
|
|
|
|
void PriorityQueue::initiateHeap(int capacity){
|
|
void PriorityQueue::initiateHeap(int capacity){
|
|
|
heap_size = 0;
|
|
heap_size = 0;
|
|
|
max_size = capacity;
|
|
max_size = capacity;
|
|
|
heapArray = new int[max_size];
|
|
heapArray = new int[max_size];
|
|
|
- //std::vector<int>* insArray = new std::vector<int>();
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void PriorityQueue::insert(int key){
|
|
void PriorityQueue::insert(int key){
|
|
@@ -46,14 +25,23 @@ void PriorityQueue::insert(int key){
|
|
|
heapifyUp(key, keyIndex);
|
|
heapifyUp(key, keyIndex);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+void PriorityQueue::deleteHeap(){
|
|
|
|
|
+ // clean up memory so deallocate array
|
|
|
|
|
+ delete[] heapArray;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void PriorityQueue::removeMax(){
|
|
void PriorityQueue::removeMax(){
|
|
|
// check if heap is empty
|
|
// check if heap is empty
|
|
|
- if (heap_size <= 1) {
|
|
|
|
|
|
|
+ if (heap_size <= 0) {
|
|
|
std::cout << "PriorityQueue::removeMax called on an empty priority queue" << std::endl;
|
|
std::cout << "PriorityQueue::removeMax called on an empty priority queue" << std::endl;
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (heap_size == 1) { // need to check if at root to avoid error
|
|
|
|
|
|
|
+ if (heap_size == 1){
|
|
|
|
|
+ heapArray[0] == 0;
|
|
|
|
|
+ heap_size--;
|
|
|
|
|
+ } else if (heap_size == 2) { // need to check if at root to avoid error
|
|
|
|
|
+ heapArray[1] = heapArray[2];
|
|
|
heap_size--;
|
|
heap_size--;
|
|
|
} else {
|
|
} else {
|
|
|
// remove the max value (at root)
|
|
// remove the max value (at root)
|
|
@@ -62,6 +50,11 @@ void PriorityQueue::removeMax(){
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+int PriorityQueue::returnMax(){
|
|
|
|
|
+ int topHeapKey = heapArray[1];
|
|
|
|
|
+ return topHeapKey;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void PriorityQueue::removeKey(int key){
|
|
void PriorityQueue::removeKey(int key){
|
|
|
for (int i = 1; i <= heap_size; i++) {
|
|
for (int i = 1; i <= heap_size; i++) {
|
|
|
if (heapArray[i] == key) {
|
|
if (heapArray[i] == key) {
|