priorityqueue.cpp 3.5 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 starts at index 1 so when I want to insert a new node, it should be at the
  37. // nex index?
  38. int keyIndex = heap_size + 1;
  39. // heapify up until the heap properties are restored
  40. heapifyUp(key, keyIndex);
  41. }
  42. void PriorityQueue::removeMax(){
  43. // check if heap is empty
  44. if (heap_size <= 1) {
  45. std::cout << "PriorityQueue::removeMax called on an empty priority queue" << std::endl;
  46. return;
  47. }
  48. if (heap_size == 1) { // need to check if at root to avoid error
  49. heap_size--;
  50. } else {
  51. // remove the max value (at root)
  52. heapArray[1] = heapArray[heap_size + 1];
  53. heapifyDown(1);
  54. heap_size--; // causing that zero to appear?
  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 correct index into something larger than maxValue
  61. heapArray[i] = heapArray[1] + 10;
  62. heapifyUp(heapArray[i], i);
  63. removeMax();
  64. return;
  65. }
  66. }
  67. std::cout << "PriorityQueue::removeKey key " << key << " not found" << std::endl;
  68. return;
  69. }
  70. void PriorityQueue::change(int *key, int *newKey){
  71. for (int i = 1; i <= heap_size; i++) {
  72. if (heapArray[i] == *key) {
  73. int temp = *newKey;
  74. *newKey = *key;
  75. *key = temp;
  76. return;
  77. }
  78. }
  79. std::cout << "PriorityQueue::change key " << *key << " not found" << std::endl;
  80. return;
  81. }
  82. void PriorityQueue::heapifyDown(int index){
  83. int lBranchIndex = 2*index;
  84. int rBranchIndex = 2*index + 1;
  85. int largerKeyIndex = index;
  86. if (lBranchIndex < heap_size && heapArray[lBranchIndex] > heapArray[index]) {
  87. largerKeyIndex = lBranchIndex;
  88. }
  89. if (rBranchIndex < heap_size && heapArray[rBranchIndex] > heapArray[largerKeyIndex]) {
  90. largerKeyIndex = rBranchIndex;
  91. }
  92. if (largerKeyIndex != index) {
  93. change(&heapArray[index], &heapArray[largerKeyIndex]);
  94. // apparently heapArray[largerKeyIndex] doesn't have a value?!?! fix this later
  95. std::cout << "when does zero appear? " << heapArray[largerKeyIndex] << std::endl;
  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. }