Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1.  
  2. //
  3. // Ioan Ivanov
  4. // Partners: Liam Dwindell and Jack Cottrell
  5. // November 7th, 2019
  6. // This file consists of the functions for the BinHeap lab
  7.  
  8. // Screenshot
  9. // https://gyazo.com/e4bab6a64a10e45ba1bb3f4c248e03f7
  10.  
  11. // First word: The Ghost of Xunantunich
  12. // Second word: Samhainophobia - A fear for Halloween
  13. // There is no third word
  14.  
  15.  
  16. #include "BinHeap.hpp"
  17. #include <string>
  18. #include <iostream>
  19. #include <stdlib.h>
  20. using namespace std;
  21.  
  22. BinHeap::BinHeap(string arr[], int len) {
  23. heaplen = 0;
  24. arrlen = len;
  25. heap = new string[arrlen];
  26. for(int i = 0; i < len; i++) {
  27. insertHeap(arr[i]);
  28.  
  29. }
  30. printHeap();
  31. }
  32.  
  33. BinHeap::~BinHeap() {
  34. delete [] heap;
  35. }
  36.  
  37. void BinHeap::printHeap() {
  38. cout << endl;
  39. for(int i = 0; i < arrlen; i++) {
  40. cout << i << ", ";
  41. }
  42. cout << endl;
  43. for(int i = 0; i < heaplen; i++) {
  44. cout << heap[i] << ", ";
  45. }
  46. cout << endl;
  47. }
  48.  
  49. int BinHeap::findMax(int x, int y) {
  50. if((x < heaplen) && (y < heaplen)) {
  51. if(heap[x] > heap[y]) {
  52. return(x);
  53.  
  54. } else {
  55. return(y);
  56.  
  57. }
  58.  
  59. } else if(x < heaplen) {
  60. return x;
  61.  
  62. } else {
  63. return -1;
  64. }
  65. }
  66.  
  67. void BinHeap::insertHeap(string s) {
  68. if(heaplen == arrlen){
  69. cout << "Heap is full" << endl;
  70. return;
  71. }
  72. heaplen++;
  73. int i = heaplen;
  74. heap[i] = s;
  75. bubbleUp(i);
  76. }
  77.  
  78. string BinHeap::deleteHeap() {
  79. string last_element = heap[heaplen-1];
  80. string first_element = heap[0];
  81. heap[0] = last_element;
  82. heaplen -= 1;
  83. bubbleDown(0);
  84.  
  85. return first_element;
  86. }
  87.  
  88. void BinHeap::bubbleUp(int i) {
  89. if(i && heap[parent(i)] < heap[i]) {
  90. swap(heap[i], heap[parent(i)]);
  91. bubbleUp(parent(i));
  92. }
  93. }
  94.  
  95. void BinHeap::bubbleDown(int i) {
  96. int left = leftChild(i);
  97. int right = rightChild(i);
  98. int largest = i;
  99. if(left < heaplen && heap[left] > heap[i]) {
  100. largest = left;
  101. }
  102. if(right < heaplen && heap[right] > heap[largest]) {
  103. largest = right;
  104. }
  105. if(largest != i) {
  106. swap(heap[i], heap[largest]);
  107. bubbleDown(largest);
  108. }
  109. }
  110.  
  111. string BinHeap::deleteHeap2() {
  112. string last_element = heap[heaplen-1];
  113. string first_element = heap[0];
  114. heap[0] = last_element;
  115. heap[heaplen-1] = first_element;
  116. heaplen -= 1;
  117. bubbleDown(0);
  118.  
  119. return first_element;
  120. }
  121.  
  122. void BinHeap::deleteAll() {
  123. int temp = heaplen;
  124. for(int i = 0; i < temp; i++){
  125. deleteHeap2();
  126. }
  127. }
  128.  
  129. int BinHeap::parent(int i) {
  130. return (i - 1) / 2;
  131. }
  132.  
  133. int BinHeap::leftChild(int i) {
  134. return (2 * i + 1);
  135. }
  136.  
  137. int BinHeap::rightChild(int i) {
  138. return (2 * i + 2);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement