priorityqueue.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // initial pseudocode from:
  2. // https://www.geeksforgeeks.org/binary-heap/
  3. #include <iostream>
  4. #include "priorityqueue.h"
  5. #include "json.hpp"
  6. // priority queue class using binary heap
  7. class PriorityQueue{
  8. int *heapArray; // pointer to heap array
  9. int max_size; // max size of heap array
  10. int heap_size; // elements in heap
  11. public:
  12. // required functions
  13. void insert(int);
  14. void removeMax();
  15. void removeKey(int);
  16. void change(int*, int*);
  17. // helpful functions
  18. void heapifyUp(int);
  19. void heapifyDown(int);
  20. //nlohmann::json JSON();
  21. // other required functions (for now)
  22. void initiateHeap(int);
  23. };
  24. void PriorityQueue::initiateHeap(int capacity){
  25. heap_size = 0;
  26. max_size = capacity;
  27. heapArray = new int[max_size]; // try to change this to vector... please!!!
  28. }
  29. void PriorityQueue::insert(int newNode){
  30. if(heap_size == max_size){
  31. std::cout << "PriorityQueue::insert called on full priority queue" << std::endl;
  32. return;
  33. }
  34. // heapify up until the heap properties are restored
  35. heapifyUp(newNode);
  36. }
  37. void PriorityQueue::removeMax(){
  38. // check if heap is empty
  39. if (heap_size <= 0) {
  40. std::cout << "PriorityQueue::removeMax called on an empty priority queue" << std::endl;
  41. return;
  42. }
  43. if (heap_size == 1) { // need to check if at root to avoid error
  44. heap_size--;
  45. } else {
  46. // remove the max value (at root)
  47. heapArray[0] = heapArray[heap_size - 1];
  48. heap_size--;
  49. // heapify down until the heap properties are restored
  50. heapifyDown(0);
  51. }
  52. }
  53. // deletes all keys at index i
  54. void PriorityQueue::removeKey(int index){
  55. if (heapArray[index] == NULL) {
  56. std::cout << "PriorityQueue::removeKey key " << index << " not found" << std::endl;
  57. return;
  58. }
  59. // increase value at index to inf
  60. // run function extracting the maximum value
  61. }
  62. void PriorityQueue::change(int *key, int *newK){
  63. // needs to change. I'm looking for the key, not the index?
  64. for (int i = 0; i >= heap_size; i++) {
  65. if (heapArray[i] == *key) {
  66. // if the key is found exchange the old key for the new
  67. heapArray[i] == *newK;
  68. // figure out the case when heapify up/down are needed
  69. if (*key > *newK) {
  70. heapifyUp(i);
  71. } else if (*key < *newK) {
  72. heapifyDown(i);
  73. }
  74. } else {
  75. std::cout << "PriorityQueue::change key " << *key << " not found" << std::endl;
  76. return;
  77. }
  78. }
  79. }
  80. void PriorityQueue::heapifyDown(int node){
  81. int lBranchValue = 2*node;
  82. int rBranchValue = 2*node + 1;
  83. int largerNode = node;
  84. if (lBranchValue < heap_size && heapArray[lBranchValue] > heapArray[node]) {
  85. largerNode = lBranchValue;
  86. }
  87. if (rBranchValue < heap_size && heapArray[rBranchValue] > heapArray[largerNode]) {
  88. largerNode = rBranchValue;
  89. }
  90. if (largerNode != 1) {
  91. change(&heapArray[node], &heapArray[largerNode]);
  92. heapifyDown(largerNode);
  93. }
  94. }
  95. void PriorityQueue::heapifyUp(int node){
  96. int curIndex = heap_size;
  97. int parentIndex = (curIndex-1)/2;
  98. heap_size++;
  99. // insert new key into the end of the array
  100. heapArray[curIndex] = node;
  101. while (curIndex != 0 && heapArray[parentIndex] < heapArray[curIndex]) {
  102. change(&heapArray[curIndex], &heapArray[parentIndex]);
  103. curIndex = (curIndex-1)/2;
  104. parentIndex = (parentIndex-1)/2;
  105. }
  106. }