priorityqueue.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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];
  30. }
  31. void PriorityQueue::insert(int key){
  32. if(heap_size == max_size){
  33. std::cout << "PriorityQueue::insert called on full priority queue" << std::endl;
  34. return;
  35. }
  36. heap_size++;
  37. // heap starts at index 1 so when I want to insert a new node, it should be at the
  38. // nex index?
  39. int keyIndex = heap_size;
  40. // heapify up until the heap properties are restored
  41. heapifyUp(key, keyIndex);
  42. }
  43. void PriorityQueue::removeMax(){
  44. // check if heap is empty
  45. if (heap_size <= 1) {
  46. std::cout << "PriorityQueue::removeMax called on an empty priority queue" << std::endl;
  47. return;
  48. }
  49. if (heap_size == 1) { // need to check if at root to avoid error
  50. heap_size--;
  51. } else {
  52. // remove the max value (at root)
  53. heapArray[1] = heapArray[heap_size];
  54. heapifyDown(1);
  55. }
  56. }
  57. void PriorityQueue::removeKey(int key){
  58. for (int i = 1; i <= heap_size; i++) {
  59. if (heapArray[i] == key) {
  60. // change the key at index with last heap key
  61. change(&heapArray[i], &heapArray[heap_size]);
  62. // erase the last node erasing the unwanted key
  63. heap_size--;
  64. // heapify to preserve heap properties
  65. heapifyDown(i);
  66. return;
  67. }
  68. }
  69. std::cout << "PriorityQueue::removeKey key " << key << " not found" << std::endl;
  70. return;
  71. }
  72. void PriorityQueue::change(int *key, int *newKey){
  73. for (int i = 1; i <= heap_size; i++) {
  74. if (heapArray[i] == *key) {
  75. int temp = *newKey;
  76. *newKey = *key;
  77. *key = temp;
  78. return;
  79. }
  80. }
  81. std::cout << "PriorityQueue::change key " << *key << " not found" << std::endl;
  82. return;
  83. }
  84. void PriorityQueue::heapifyDown(int index){
  85. int lBranchIndex = 2*index;
  86. int rBranchIndex = 2*index + 1;
  87. int largerKeyIndex = index;
  88. if (lBranchIndex < heap_size && heapArray[lBranchIndex] > heapArray[index]) {
  89. largerKeyIndex = lBranchIndex;
  90. }
  91. if (rBranchIndex < heap_size && heapArray[rBranchIndex] > heapArray[largerKeyIndex]) {
  92. largerKeyIndex = rBranchIndex;
  93. }
  94. if (largerKeyIndex != index) {
  95. change(&heapArray[index], &heapArray[largerKeyIndex]);
  96. heapifyDown(largerKeyIndex);
  97. heap_size--;
  98. }
  99. }
  100. void PriorityQueue::heapifyUp(int key, int index){
  101. int curIndex = index;
  102. int parentIndex = (curIndex)/2;
  103. //heap_size++;
  104. // insert new key into the end of the array
  105. heapArray[curIndex] = key;
  106. while (curIndex != 1 && heapArray[parentIndex] < heapArray[curIndex]) {
  107. change(&heapArray[curIndex], &heapArray[parentIndex]);
  108. curIndex = (curIndex)/2;
  109. parentIndex = (parentIndex)/2;
  110. }
  111. }
  112. void PriorityQueue::printArray(){
  113. for (int i = 1; i <= heap_size; i++) {
  114. std::cout << heapArray[i] << " , ";
  115. }
  116. std::cout << std::endl;
  117. }