priorityqueue.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. void PriorityQueue::initiateHeap(int capacity){
  8. heap_size = 0;
  9. max_size = capacity;
  10. heapArray = new int[max_size];
  11. }
  12. void PriorityQueue::insert(int key){
  13. if(heap_size == max_size){
  14. std::cout << "PriorityQueue::insert called on full priority queue" << std::endl;
  15. return;
  16. }
  17. // increase array size and insert new key at end
  18. heap_size++;
  19. int keyIndex = heap_size;
  20. // heapify up until the heap properties are restored
  21. heapifyUp(key, keyIndex);
  22. }
  23. void PriorityQueue::deleteHeap(){
  24. // clean up memory so deallocate array
  25. delete[] heapArray;
  26. }
  27. void PriorityQueue::removeMax(){
  28. // check if heap is empty
  29. if (heap_size <= 0) {
  30. std::cout << "PriorityQueue::removeMax called on an empty priority queue" << std::endl;
  31. return;
  32. }
  33. if (heap_size == 1){
  34. //heapArray[0] == 0;
  35. heap_size--;
  36. } else if (heap_size == 2) { // need to check if at root to avoid error
  37. heapArray[1] = heapArray[2];
  38. heap_size--;
  39. } else {
  40. // remove the max value (at root)
  41. heapArray[1] = heapArray[heap_size];
  42. heapifyDown(1);
  43. heap_size--;
  44. }
  45. }
  46. int PriorityQueue::returnMax(){
  47. int topHeapKey = heapArray[1];
  48. return topHeapKey;
  49. }
  50. void PriorityQueue::removeKey(int key){
  51. for (int i = 1; i <= heap_size; i++) {
  52. if (heapArray[i] == key) {
  53. // change the key at index with last heap key
  54. change(&heapArray[i], &heapArray[heap_size]);
  55. // erase the last node erasing the unwanted key
  56. heap_size--;
  57. // heapify to preserve heap properties
  58. heapifyDown(i);
  59. return;
  60. }
  61. }
  62. std::cout << "PriorityQueue::removeKey key " << key << " not found" << std::endl;
  63. return;
  64. }
  65. void PriorityQueue::change(int *key, int *newKey){
  66. for (int i = 1; i <= heap_size; i++) {
  67. if (heapArray[i] == *key) {
  68. int temp = *newKey;
  69. *newKey = *key;
  70. *key = temp;
  71. return;
  72. }
  73. }
  74. std::cout << "PriorityQueue::change key " << *key << " not found" << std::endl;
  75. return;
  76. }
  77. void PriorityQueue::heapifyDown(int index){
  78. int lBranchIndex = 2*index;
  79. int rBranchIndex = 2*index + 1;
  80. int largerKeyIndex = index;
  81. if (lBranchIndex < heap_size && heapArray[lBranchIndex] > heapArray[index]) {
  82. largerKeyIndex = lBranchIndex;
  83. }
  84. if (rBranchIndex < heap_size && heapArray[rBranchIndex] > heapArray[largerKeyIndex]) {
  85. largerKeyIndex = rBranchIndex;
  86. }
  87. if (largerKeyIndex != index) {
  88. change(&heapArray[index], &heapArray[largerKeyIndex]);
  89. heapifyDown(largerKeyIndex);
  90. }
  91. }
  92. void PriorityQueue::heapifyUp(int key, int index){
  93. int curIndex = index;
  94. int parentIndex = (curIndex)/2;
  95. // insert new key into the end of the array
  96. heapArray[curIndex] = key;
  97. while (curIndex != 1 && heapArray[parentIndex] < heapArray[curIndex]) {
  98. change(&heapArray[curIndex], &heapArray[parentIndex]);
  99. curIndex = (curIndex)/2;
  100. parentIndex = (parentIndex)/2;
  101. }
  102. }
  103. void PriorityQueue::printArray(){
  104. for (int i = 1; i <= heap_size; i++) {
  105. std::cout << heapArray[i] << " , ";
  106. }
  107. std::cout << std::endl;
  108. }