Question

In: Computer Science

Write the code for binary min heap in c++ .

Write the code for binary min heap in c++ .

Solutions

Expert Solution

#include<iostream>
#include<climits>
using namespace std;

//class for heap
class Heap{
   //pointer to store heap value
   int *heap;
   //variable to store number of values in heap
   int current_size;
   //variable to store maximum capacity of heap
   int max_capacity;
  
   public:
       //constructor
       Heap(int n){
           max_capacity=n;
           current_size=0;
           heap=new int[max_capacity];
       }
       //methods to get parent, lef and right child index
       int parent(int i) {
           return (i-1)/2;
       }
        int leftChild(int i) {
          return (2*i + 1);
       }
        int rightChild(int i) {
           return (2*i + 2);
       }
       //method to insert value to heap
       void insert(int value){
           if (current_size != max_capacity) {
               current_size++;
               int i = current_size - 1;
               heap[i] = value;

               // Fix the min heap property if it is violated
               while (i != 0 && heap[parent(i)] > heap[i]) {
                   int temp=heap[i];
                   heap[i]=heap[parent(i)];
                   heap[parent(i)]=temp;
                      i = parent(i);
               }
               cout<<"Inserted "<<value<<"\n";
           }else{
               cout << "\nHeap Full!!!! Could not insert the value\n";
           }

       }
       //method to get minimum value in heap
       int getMin(){
               return heap[0];
       }
       //method to minhepify the heap
       void MinHeapify(int i){
           int l = leftChild(i);
           int r = rightChild(i);
          
           int smallest = i;
          
           if (l < current_size && heap[l] < heap[i])
               smallest = l;
           if (r < current_size && heap[r] < heap[smallest])
               smallest = r;
  
           if (smallest != i) {
               int temp=heap[i];
               heap[i]=heap[smallest];
               heap[smallest]=temp;

               MinHeapify(smallest);
           }
       }
       //method to delete and return minimu value in heap
       int extractMin(){
           if (current_size == 1) {
                current_size--;
               return heap[0];
           }
           if (current_size <= 0)
               return INT_MAX;
      

           int min = heap[0];
            heap[0] = heap[current_size-1];
           current_size--;
           MinHeapify(0);

           return min;
       }
  
};
//main method
int main(){
   //creating heap of max capacity 10
   Heap h1(10);
   h1.insert(5);
   h1.insert(2);
   h1.insert(3);
   h1.insert(1);
   h1.insert(10);
   cout<<"Minum element in heap: "<<h1.getMin()<<"\n";
   int d=h1.extractMin();
   if(d!=INT_MAX)
       cout<<"Minimum element deleted: "<<d<<"\n";
   else
       cout<<"Heap is empty\n";
      
   cout<<"Minum element in heap: "<<h1.getMin()<<"\n";
   return 0;
}

//####################### PGM END ##################################

OUTPUT
###########


Related Solutions

C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
Let's suppose that you have a binary min-heap with S elements. Please write an an algorithm...
Let's suppose that you have a binary min-heap with S elements. Please write an an algorithm that will find all the elements that are in the heap that are smaller than a given value X. Please make sure that the time complexity of the algorithm that you propose has to be O(K), where is the number of elements smaller than X that is in the heap. Please type answer if you can
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a...
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a class called Node: Have a Name and Priority.Data set - 10 is the highest priority, 1 is lowest priority. Enqueue and dequeue in the following order. Function  Name, Priority Enqueue  Joe, 3 Enqueue  Fred,1 Enqueue Tuyet,9 Enqueue  Jose, 6 Dequeue Enqueue  Jing, 2 Enqueue  Xi, 5 Enqueue  Moe, 3 DequeueEnqueue  Miko, 7 Enqueue Vlady, 8 Enqueue Frank, 9 Enqueue  Anny, 3 DequeueEnqueue  Xi, 2 Enqueue  Wali, 2 Enqueue  xChe, 6 Enqueue  xVerra, 8 Dequeue Dequeue Dequeue Dequeue...
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer...
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer data element. Initialize the Min-heap to contain 5 nodes, with the values 8, 12, 24, 32, 42. The program should allow for the insertion and deletion of nodes while maintaining a Min-Heap. The program should allow the user to output data in Preorder, Inorder and Postorder. The program should loop with menu items for each of the above objectives and the choice to quit.
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer...
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer data element. Initialize the Min-heap to contain 5 nodes, with the values 8, 12, 24, 32, 42. The program should allow for the insertion and deletion of nodes while maintaining a Min-Heap. The program should allow the user to output data in Preorder, Inorder and Postorder. The program should loop with menu items for each of the above objectives and the choice to quit...
One way to remove an object from a binary min heap is to decrease its priority...
One way to remove an object from a binary min heap is to decrease its priority value by 1 and then call deleteMin. An alternative is to remove it from the heap, thus creating a hole, and then repair the heap. Write pseudocode for an algorithm that performs the remove operation using the alternative approach described above. Your pseudocode should implement the method call remove (int index), where index is the index into the heap array for the object to...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
How to write Prim's Algorithm with min-Heap and adjacency Lists?
How to write Prim's Algorithm with min-Heap and adjacency Lists?
Suppose H is a binary min-heap with n nodes, Prove the following facts: Any algorithm that...
Suppose H is a binary min-heap with n nodes, Prove the following facts: Any algorithm that must nd the maximum key of the heap must search all of the leaf nodes.
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT