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