Question

C++ Question Can I get an example of a max heap that has an insert function,...

C++ Question

Can I get an example of a max heap that has an insert function, delete function and a print function?

Please use namespace std and iostream and whatever else is needed I guess.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

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

// Data structure for Max Heap
struct PriorityQueue
{
private:
   // vector to store heap elements
   vector<int> A;

   // return parent of A[i]
   // don't call this function if it is already a root node
   int PARENT(int i)
   {
       return (i - 1) / 2;
   }

   // return left child of A[i]
   int LEFT(int i)
   {
       return (2 * i + 1);
   }

   // return right child of A[i]
   int RIGHT(int i)
   {
       return (2 * i + 2);
   }

   // Recursive Heapify-down algorithm
   // the node at index i and its two direct children
   // violates the heap property
   void heapify_down(int i)
   {
       // get left and right child of node at index i
       int left = LEFT(i);
       int right = RIGHT(i);

       int largest = i;

       // compare A[i] with its left and right child
       // and find largest value
       if (left < size() && A[left] > A[i])
           largest = left;

       if (right < size() && A[right] > A[largest])
           largest = right;

       // swap with child having greater value and
       // call heapify-down on the child
       if (largest != i) {
           swap(A[i], A[largest]);
           heapify_down(largest);
       }
   }

   // Recursive Heapify-up algorithm
   void heapify_up(int i)
   {
       // check if node at index i and its parent violates
       // the heap property
       if (i && A[PARENT(i)] < A[i])
       {
           // swap the two if heap property is violated
           swap(A[i], A[PARENT(i)]);

           // call Heapify-up on the parent
           heapify_up(PARENT(i));
       }
   }

public:
   // return size of the heap
   unsigned int size()
   {
       return A.size();
   }

   // function to check if heap is empty or not
   bool empty()
   {
       return size() == 0;
   }

   // insert key into the heap
   void insert(int key)
   {
       // insert the new element to the end of the vector
       A.push_back(key);

       // get element index and call heapify-up procedure
       int index = size() - 1;
       heapify_up(index);
   }

   // function to remove element with highest priority (present at root)
   void deletes()
   {

           // if heap has no elements, throw an exception
           if (size() == 0)
               {
               cout<<"Can't delete empty heap\n";
return;
               }

           // replace the root of the heap with the last element
           // of the vector
           A[0] = A.back();
           A.pop_back();

           // call heapify-down on root node
           heapify_down(0);


   }

   // function to return element with highest priority (present at root)
   int top()
   {

           // if heap has no elements, throw an exception
           if (size() == 0)
               {
               cout<<"Can't return empty heap\n";
return -1;
               }

           // else return the top (first) element
           return A.at(0);   // or return A[0];

       // catch and print the exception

   }
};

int main()
{
   PriorityQueue pq;

   // Note - Priority is decided by element's value

   pq.insert(3);
   pq.insert(2);
   pq.insert(15);

   cout << "Size is " << pq.size() << endl;

   cout << pq.top() << " ";
   pq.deletes();

   cout << pq.top() << " ";
   pq.deletes();

   pq.insert(5);
   pq.insert(4);
   pq.insert(45);

   cout << endl << "Size is " << pq.size() << endl;

   cout << pq.top() << " ";
   pq.deletes();

   cout << pq.top() << " ";
   pq.deletes();

   cout << pq.top() << " ";
   pq.deletes();

   cout << pq.top() << " ";
   pq.deletes();

   cout << endl << std::boolalpha << pq.empty();

   return 0;
}

​​​​​​​

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
C++ Question Can I get an example of a max heap that has an insert function,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT