Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. //kopce
  4. void addValue(int* heap, int& heapSize, int arraySize, int value);
  5. void bottomUp(int* heap, int& heapSize);
  6. void deleteValue(int* heap, int& heapSize);
  7. void topDown(int* heap, int& heapSize);
  8. int getParentIndex(int childIndex);
  9. void swap(int& a, int& b);
  10. void printHeap(int* heap, int heapSize);
  11. //kopce
  12.  
  13. int main()
  14. {
  15.     const auto size = 15;
  16.     auto heapSize = 11;
  17.     const auto maxHeap = new int[size] { 15, 12, 11, 10, 11, 8, 5, 3, 7, 2, 9 };
  18.  
  19.     printHeap(maxHeap, heapSize);
  20.    
  21.     addValue(maxHeap, heapSize, size, 12);
  22.     bottomUp(maxHeap, heapSize);
  23.  
  24.     printHeap(maxHeap, heapSize);
  25.  
  26.     deleteValue(maxHeap, heapSize);
  27.     topDown(maxHeap, heapSize);
  28.    
  29.     printHeap(maxHeap, heapSize);
  30.     getchar();
  31.    
  32.     return EXIT_SUCCESS;
  33. }
  34.  
  35. //kopce
  36. void deleteValue(int* heap, int& heapSize)
  37. {
  38.     swap(heap[0], heap[heapSize - 1]);
  39.     heapSize--;
  40. }
  41.  
  42. void addValue(int* heap, int& heapSize, const int arraySize, const int value)
  43. {
  44.     if (heapSize >= arraySize)
  45.         return;
  46.  
  47.     heap[heapSize] = value;
  48.     heapSize++;
  49. }
  50.  
  51. void bottomUp(int* heap, int& heapSize)
  52. {
  53.     auto currentIndex = heapSize - 1;
  54.  
  55.     while (currentIndex != 0 && heap[getParentIndex(currentIndex)] < heap[currentIndex])
  56.     {
  57.         swap(heap[getParentIndex(currentIndex)], heap[currentIndex]);
  58.         currentIndex = getParentIndex(currentIndex);
  59.     }
  60. }
  61.  
  62. void topDown(int* heap, int& heapSize)
  63. {
  64.     auto current = 0;
  65.     auto left = 1;
  66.     auto right = 2;
  67.  
  68.     swap(heap[0], heap[heapSize - 1]);
  69.  
  70.     while (current < heapSize && heap[left] > heap[current])
  71.     {
  72.         auto smallest = current;
  73.  
  74.         if (left < heapSize && heap[left] > heap[current])
  75.             smallest = left;
  76.         if (right < heapSize && heap[right] > heap[smallest])
  77.             smallest = right;
  78.  
  79.         if (smallest != current)
  80.             swap(heap[current], heap[smallest]);
  81.  
  82.         current = smallest;
  83.         left = current * 2 + 1;
  84.         right = current * 2 + 2;
  85.     }
  86. }
  87.  
  88. int getParentIndex(const int childIndex)
  89. {
  90.     return childIndex % 2 ? (childIndex - 1) / 2 : childIndex / 2;
  91. }
  92.  
  93. void swap(int& a, int& b)
  94. {
  95.     const auto c = a;
  96.     a = b;
  97.     b = c;
  98. }
  99.  
  100. void printHeap(int* heap, const int heapSize)
  101. {
  102.     for (int i = 0; i < heapSize; i++)
  103.         std::cout << heap[i] << " ";
  104.  
  105.     std::cout << std::endl;
  106. }
  107. //kopce
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement