Advertisement
Eternoseeker

Max Heap

Jan 29th, 2024 (edited)
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | Source Code | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3.  
  4. class MaxHeap{
  5.     int* arr;
  6.     int maxSize;
  7.     int heapSize;
  8. public:
  9.     explicit MaxHeap(int size) {
  10.         heapSize = 0;
  11.         maxSize = size;
  12.         arr = new int[size];
  13.     }
  14.     void MaxHeapify(int i);
  15.     int removeMax();
  16.     void increaseKey(int i, int newVal);
  17.     void deleteKey(int i);
  18.     void insertKey(int x);
  19.     void displayHeap();
  20.     void HeapSort();
  21.     static int parent(int i){
  22.         return (i - 1) / 2;
  23.     }
  24.     static int LeftChild(int i){
  25.         return (2 * i + 1);
  26.     }
  27.     static int RightChild(int i){
  28.         return (2 * i + 2);
  29.     }
  30.     int getMax(){
  31.         return arr[0];
  32.     }
  33.     int curSize(){
  34.         return heapSize;
  35.     }
  36. };
  37.  
  38. // Increases value of the key at index 'i' to new_val
  39. void MaxHeap::increaseKey(int i, int newVal) {
  40.     arr[i] = newVal;
  41.     while(i != 0 && arr[parent(i)] < arr[i]){
  42.         std::swap(arr[i], arr[parent(i)]);
  43.         i = parent(i);
  44.     }
  45. }
  46.  
  47. void MaxHeap::deleteKey(int i) {
  48.     //Setting key to max value and then removing it
  49.     increaseKey(i, INT_MAX);
  50.     removeMax();
  51. }
  52.  
  53. void MaxHeap::insertKey(int x) {
  54.     //Allocating more space
  55.     if(heapSize == maxSize){
  56.         int* newArr = new int[2 * maxSize];
  57.         for(int i = 0; i < maxSize; i++){
  58.             newArr[i] = arr[i];
  59.         }
  60.         delete[] arr;
  61.         arr = newArr;
  62.         maxSize *= 2;
  63.     }
  64.  
  65.     heapSize++;
  66.     //Initially inserted at the end
  67.     int i = heapSize - 1;
  68.     arr[i] = x;
  69.     //Making sure max heap property satisfied
  70.     while(i != 0 && arr[parent(i)] < arr[i]){
  71.         std::swap(arr[i], arr[parent(i)]);
  72.         i = parent(i);
  73.     }
  74. }
  75.  
  76. //Remove the root node which is the max element
  77. int MaxHeap::removeMax() {
  78.     if(heapSize <= 0){
  79.         return INT_MIN;
  80.     }
  81.     if(heapSize == 1){
  82.         heapSize--;
  83.         return arr[0];
  84.     }
  85.  
  86.     //storing the max element
  87.     int root = arr[0];
  88.  
  89.     //last element is made the root
  90.     arr[0] = arr[heapSize - 1];
  91.     heapSize--;
  92.  
  93.     //Restore the property of the heap
  94.     MaxHeapify(0);
  95.     return root;
  96. }
  97.  
  98. void MaxHeap::MaxHeapify(int i) {
  99.     int l = LeftChild(i);
  100.     int r = RightChild(i);
  101.     //Set current element as largest and swap accordingly
  102.     int largest = i;
  103.     if(l < heapSize && arr[l] > arr[i]){
  104.         largest = l;
  105.     }
  106.     if(r < heapSize && arr[r] > arr[largest]){
  107.         largest = r;
  108.     }
  109.     if(largest != i){
  110.         std::swap(arr[i], arr[largest]);
  111.         MaxHeapify(largest);
  112.     }
  113. }
  114.  
  115. void MaxHeap::displayHeap() {
  116.     for(auto i = 0; i < heapSize; i++){
  117.         std::cout << arr[i] << " ";
  118.     }
  119.     std::cout << "\n";
  120. }
  121.  
  122. void MaxHeap::HeapSort() {
  123.     int* sortedArray = new int[heapSize];
  124.     int* tempArray = new int[maxSize];
  125.     int tempsize = heapSize;
  126.     // Copy the original array to tempArray
  127.     std::copy(arr, arr + tempsize, tempArray);
  128.     // Perform heap sort and store the sorted elements in sortedArray
  129.     for (auto i = tempsize - 1; i >= 0; i--) {
  130.         sortedArray[i] = removeMax();
  131.     }
  132.     for(auto i = 0; i < tempsize; i++){
  133.         std::cout << sortedArray[i] << " ";
  134.     }
  135.     std::cout << "\n";
  136.     delete[] arr;
  137.     heapSize = tempsize;
  138.     arr = tempArray;
  139.     // Copy elements back to arr
  140. //    std::copy(tempArray, tempArray + tempsize, arr);
  141. }
  142.  
  143. void prompts(){
  144.     std::cout << "1: Display the array \n";
  145.     std::cout << "2: Insert a key \n";
  146.     std::cout << "3: Size of the heap\n";
  147.     std::cout << "4: Maximum element \n";
  148.     std::cout << "5: Delete a key \n";
  149.     std::cout << "6: Perform Heap Sort \n";
  150.     std::cout << "0: EXIT \n";
  151.     std::cout << "Enter your choice: \n";
  152. }
  153.  
  154. int inputValue(){
  155.     int value;
  156.     std::cout << "Enter a value: \n";
  157.     std::cin >> value;
  158.     return value;
  159. }
  160.  
  161. int main(){
  162.     MaxHeap maxheap(10);
  163.     int choice = 10;
  164.     while(choice != 0){
  165.         prompts();
  166.         std::cin >> choice;
  167.         switch (choice) {
  168.             case 0:
  169.                 std::cout << "Exited Successfully! \n";
  170.                 break;
  171.             case 1:
  172.                 std::cout << "The heap array: \n";
  173.                 maxheap.displayHeap();
  174.                 break;
  175.             case 2:
  176.                 maxheap.insertKey(inputValue());
  177.                 break;
  178.             case 3:
  179.                 std::cout << "The current size of the heap is: " << maxheap.curSize();
  180.                 std::cout << "\n";
  181.                 break;
  182.             case 4:
  183.                 std::cout << "The max(root) element is: " << maxheap.getMax();
  184.                 std::cout << "\n";
  185.                 break;
  186.             case 5:
  187.                 maxheap.deleteKey(inputValue());
  188.                 break;
  189.             case 6:
  190.                 std::cout << "The sorted array: \n";
  191.                 maxheap.HeapSort();
  192.                 break;
  193.             default:
  194.                 std::cout << "Not a valid choice! \n";
  195.                 break;
  196.         }
  197.     }
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement