|
|
@@ -2,6 +2,8 @@
|
|
|
// https://www.geeksforgeeks.org/binary-heap/
|
|
|
|
|
|
#include <iostream>
|
|
|
+#include <cstdio>
|
|
|
+#include <string>
|
|
|
#include "priorityqueue.h"
|
|
|
#include "json.hpp"
|
|
|
|
|
|
@@ -58,8 +60,8 @@ int PriorityQueue::returnMax(){
|
|
|
void PriorityQueue::removeKey(int key){
|
|
|
for (int i = 1; i <= heap_size; i++) {
|
|
|
if (heapArray[i] == key) {
|
|
|
- // change the key at index with last heap key
|
|
|
- change(&heapArray[i], &heapArray[heap_size]);
|
|
|
+ // swap the key at index with last heap key
|
|
|
+ swap(&heapArray[i], &heapArray[heap_size]);
|
|
|
// erase the last node erasing the unwanted key
|
|
|
heap_size--;
|
|
|
// heapify to preserve heap properties
|
|
|
@@ -71,7 +73,26 @@ void PriorityQueue::removeKey(int key){
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
-void PriorityQueue::change(int *key, int *newKey){
|
|
|
+void PriorityQueue::change(int key, int newKey){
|
|
|
+ for (int i = 1; i <= heap_size; i++) {
|
|
|
+ if (heapArray[i] == key) {
|
|
|
+ heapArray[i] = newKey;
|
|
|
+
|
|
|
+ if (newKey > key) {
|
|
|
+ heapifyUp(newKey, i);
|
|
|
+ return;
|
|
|
+ } else if (newKey < key) {
|
|
|
+ heapifyDown(i);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ std::cout << "PriorityQueue::change key " << key << " not found" << std::endl;
|
|
|
+ return;
|
|
|
+}
|
|
|
+
|
|
|
+void PriorityQueue::swap(int *key, int *newKey){
|
|
|
for (int i = 1; i <= heap_size; i++) {
|
|
|
if (heapArray[i] == *key) {
|
|
|
int temp = *newKey;
|
|
|
@@ -80,9 +101,6 @@ void PriorityQueue::change(int *key, int *newKey){
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- std::cout << "PriorityQueue::change key " << *key << " not found" << std::endl;
|
|
|
- return;
|
|
|
}
|
|
|
|
|
|
void PriorityQueue::heapifyDown(int index){
|
|
|
@@ -100,7 +118,7 @@ void PriorityQueue::heapifyDown(int index){
|
|
|
}
|
|
|
|
|
|
if (largerKeyIndex != index) {
|
|
|
- change(&heapArray[index], &heapArray[largerKeyIndex]);
|
|
|
+ swap(&heapArray[index], &heapArray[largerKeyIndex]);
|
|
|
heapifyDown(largerKeyIndex);
|
|
|
}
|
|
|
}
|
|
|
@@ -112,7 +130,7 @@ void PriorityQueue::heapifyUp(int key, int index){
|
|
|
heapArray[curIndex] = key;
|
|
|
|
|
|
while (curIndex != 1 && heapArray[parentIndex] < heapArray[curIndex]) {
|
|
|
- change(&heapArray[curIndex], &heapArray[parentIndex]);
|
|
|
+ swap(&heapArray[curIndex], &heapArray[parentIndex]);
|
|
|
curIndex = (curIndex)/2;
|
|
|
parentIndex = (parentIndex)/2;
|
|
|
}
|
|
|
@@ -124,3 +142,60 @@ void PriorityQueue::printArray(){
|
|
|
}
|
|
|
std::cout << std::endl;
|
|
|
}
|
|
|
+
|
|
|
+void PriorityQueue::printJSONTree(int maxHeapSize, int numOperations){
|
|
|
+ nlohmann::json outputJSON; // output JSON file
|
|
|
+
|
|
|
+ for (int i = heap_size; i >= 1; i--) {
|
|
|
+ int keyValue = heapArray[i]; // get the key value at array index i
|
|
|
+
|
|
|
+ // find index values for the left, right, and parent nodes
|
|
|
+ int lBranchIndex = 2*i;
|
|
|
+ int rBranchIndex = 2*i + 1;
|
|
|
+ int parentIndex = i/2;
|
|
|
+
|
|
|
+ // convert index and key into strings so as to put them in JSON output
|
|
|
+ std::stringstream convert2String_i;
|
|
|
+ convert2String_i << i;
|
|
|
+ std::string indexForJSON = convert2String_i.str();
|
|
|
+
|
|
|
+ std::stringstream convert2String_key;
|
|
|
+ convert2String_key << keyValue;
|
|
|
+ std::string keyForJSON = convert2String_key.str();
|
|
|
+
|
|
|
+ outputJSON[indexForJSON]["key"] = keyForJSON;
|
|
|
+
|
|
|
+ // if not at the root, find the index of the parent node
|
|
|
+ if (i > 1) {
|
|
|
+ std::stringstream convert2String_par;
|
|
|
+ convert2String_par << parentIndex;
|
|
|
+ std::string parentForJSON = convert2String_par.str();
|
|
|
+
|
|
|
+ outputJSON[indexForJSON]["parent"] = parentForJSON;
|
|
|
+ }
|
|
|
+ // find if there is a left branch and if so, what the index is
|
|
|
+ if (lBranchIndex <= heap_size) {
|
|
|
+ std::stringstream convert2String_l;
|
|
|
+ convert2String_l << lBranchIndex;
|
|
|
+ std::string leftForJSON = convert2String_l.str();
|
|
|
+
|
|
|
+ outputJSON[indexForJSON]["leftChild"] = leftForJSON;
|
|
|
+ }
|
|
|
+ // find if there is a right branch and if so, what the index is
|
|
|
+ if (rBranchIndex <= heap_size) {
|
|
|
+ std::stringstream convert2String_r;
|
|
|
+ convert2String_r << rBranchIndex;
|
|
|
+ std::string rightForJSON = convert2String_r.str();
|
|
|
+
|
|
|
+ outputJSON[indexForJSON]["rightChild"] = rightForJSON;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // write out the metadata output
|
|
|
+ outputJSON["metadata"]["maxHeapSize"] = maxHeapSize;
|
|
|
+ outputJSON["metadata"]["max_size"] = max_size;
|
|
|
+ outputJSON["metadata"]["numOperations"] = numOperations;
|
|
|
+ outputJSON["metadata"]["size"] = heap_size;
|
|
|
+ std::cout << outputJSON << std::endl;
|
|
|
+
|
|
|
+}
|