priorityqueue.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /*
  8. class PriorityQueue{
  9. int *heapArray; // pointer to heap array
  10. int max_size; // max size of heap array
  11. int heap_size; // elements in heap
  12. public:
  13. // required functions
  14. void insert(int);
  15. void removeMax();
  16. void removeKey(int);
  17. void change(int*, int*);
  18. // helpful functions
  19. void heapifyUp(int, int);
  20. void heapifyDown(int);
  21. //nlohmann::json JSON();
  22. // other required functions (for now)
  23. void initiateHeap(int);
  24. };
  25. */
  26. void PriorityQueue::initiateHeap(int capacity){
  27. heap_size = 0;
  28. max_size = capacity;
  29. heapArray = new int[max_size]; // try to change this to vector... please!!!
  30. }
  31. void PriorityQueue::insert(int newNode){
  32. if(heap_size == max_size){
  33. std::cout << "PriorityQueue::insert called on full priority queue" << std::endl;
  34. return;
  35. }
  36. // heapify up until the heap properties are restored
  37. heapifyUp(newNode, heap_size);
  38. }
  39. void PriorityQueue::removeMax(){
  40. // check if heap is empty
  41. if (heap_size <= 0) {
  42. std::cout << "PriorityQueue::removeMax called on an empty priority queue" << std::endl;
  43. return;
  44. }
  45. if (heap_size == 1) { // need to check if at root to avoid error
  46. heap_size--;
  47. } else {
  48. // remove the max value (at root)
  49. heapArray[0] = heapArray[heap_size - 1];
  50. heap_size--;
  51. // heapify down until the heap properties are restored
  52. heapifyDown(0);
  53. }
  54. }
  55. void PriorityQueue::removeKey(int key){
  56. for (int i = 0; i >= heap_size; i++) {
  57. if (heapArray[i] == key) {
  58. // change the key at correct index into something larger than maxValue
  59. heapArray[i] = heapArray[0] + 10;
  60. heapifyUp(heapArray[i], i);
  61. removeMax();
  62. return;
  63. }
  64. }
  65. std::cout << "PriorityQueue::removeKey key " << key << " not found" << std::endl;
  66. return;
  67. }
  68. void PriorityQueue::change(int *key, int *newK){
  69. for (int i = 0; i >= heap_size; i++) {
  70. if (heapArray[i] == *key) {
  71. // if the key is found exchange the old key for the new
  72. heapArray[i] = *newK;
  73. // figure out the case when heapify up/down are needed
  74. if (*key > *newK) {
  75. heapifyUp(heapArray[i], i);
  76. return;
  77. } else if (*key < *newK) {
  78. heapifyDown(i);
  79. return;
  80. }
  81. }
  82. }
  83. std::cout << "PriorityQueue::change key " << *key << " not found" << std::endl;
  84. return;
  85. }
  86. void PriorityQueue::heapifyDown(int node){
  87. int lBranchValue = 2*node;
  88. int rBranchValue = 2*node + 1;
  89. int largerNode = node;
  90. if (lBranchValue < heap_size && heapArray[lBranchValue] > heapArray[node]) {
  91. largerNode = lBranchValue;
  92. }
  93. if (rBranchValue < heap_size && heapArray[rBranchValue] > heapArray[largerNode]) {
  94. largerNode = rBranchValue;
  95. }
  96. if (largerNode != 1) {
  97. change(&heapArray[node], &heapArray[largerNode]);
  98. heapifyDown(largerNode);
  99. }
  100. }
  101. void PriorityQueue::heapifyUp(int key, int index){
  102. int curIndex = index;
  103. int parentIndex = (curIndex-1)/2;
  104. heap_size++;
  105. // insert new key into the end of the array
  106. heapArray[curIndex] = key;
  107. while (curIndex != 0 && heapArray[parentIndex] < heapArray[curIndex]) {
  108. change(&heapArray[curIndex], &heapArray[parentIndex]);
  109. curIndex = (curIndex-1)/2;
  110. parentIndex = (parentIndex-1)/2;
  111. }
  112. }
  113. void PriorityQueue::printArray(){
  114. for (int i = 0; i >= heap_size; i++) {
  115. std::cout << heapArray[i];
  116. }
  117. std::cout << std::endl;
  118. }