Question

Could anyone help me solve this C++ problem? Thanks a lot create a class called minHeap...

Could anyone help me solve this C++ problem? Thanks a lot

create a class called minHeap to demonstrate the min Heap data structure of type integer. use a vector to implement the concept. in this class, have the following:

private:

  • vector of the type integer
  • integer to store the index of the last element in the min heap "vector"
  • integer to store the size (number of items in the vector "min heap")

public:

  • default constructor to set the size to zero and the index of the last element to -1
  • an add method to add an element to the min-heap
  • a delete method to delete the root of the min-heap
  • a size method to return the number of elements of the min-heap
  • an empty method to check if the min-heap is empty.
  • use your work to sort the min-heap elements. use a new vector to store the sorted elements.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <vector>

#include<bits/stdc++.h>

using namespace std;


class PriorityQueue {

    vector<int> pq;

int ind;

    public :

    PriorityQueue() {

pq.clear();

ind = -1;

}

    bool isEmpty() {

        return pq.size() == 0;

    }

    // Return the size of priorityQueue - no of elements present

    int getSize() {

        return pq.size();

    }

    int getMin() {

        if(isEmpty()) {

            return 0;       // Priority Queue is empty

        }

        return pq[0];

    }

    void insert(int element) {

        pq.push_back(element);

        

        int childIndex = pq.size() - 1;

ind = pq.size()-1;

        while(childIndex > 0) {

            int parentIndex = (childIndex - 1) / 2;

            if(pq[childIndex] < pq[parentIndex]) {

                int temp = pq[childIndex];

                pq[childIndex] = pq[parentIndex];

                pq[parentIndex] = temp;

            }

            else {

                break;

            }

            childIndex = parentIndex;

        }

    }

int removeMin() {

// Complete this function

if(isEmpty())

return 0;

int ans=pq[0];

pq[0]=pq[pq.size()-1];

pq.pop_back();

ind = pq.size()-1;

int pi=0;

int lci= 2*pi+1;

int rci= 2*pi+2;

while(lci<pq.size()){

int mi=pi;

if(pq[mi]>pq[lci])

mi=lci;

if(pq[mi]>pq[rci] && rci<pq.size())

mi=rci;

if(mi==pi)

break;

int temp=pq[mi];

pq[mi]=pq[pi];

pq[pi]=temp;

pi=mi;

lci=2*pi+1;

rci=2*pi+2;

}

return ans;

}


};

int main() {

PriorityQueue pq;

int choice;

cin >> choice;

while(choice != -1) {

switch(choice) {

case 1 : // insert

int element;

cin >> element;

pq.insert(element);

break;

case 2 : // getMax

cout << pq.getMin() << endl;

break;

case 3 : // removeMax

cout << pq.removeMin() << endl;

break;

case 4 : // size

cout << pq.getSize() << endl;

break;

case 5 : // isEmpty

if(pq.isEmpty()) {

cout << "true" << endl;

}

else {

cout << "false" << endl;

}

default :

return 0;

}

cin >> choice;

}

}

(If you like the answer, please press 'like' button, Thank you)

Add a comment
Know the answer?
Add Answer to:
Could anyone help me solve this C++ problem? Thanks a lot create a class called minHeap...
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
  • // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap...

    // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap { private Comparable a heap; // array of heap entries private static final int DEFAULT MAX SIZE = 100; private int lastIndex; // index of last entry public MinHeap() { heap = new Comparable (DEFAULT_MAX_SIZE]; lastIndex = 0; } // end default constructor public MinHeap (int maxSize) { heap = new Comparable (maxSize); lastIndex = 0; } // end constructor public MinHeap (Comparable[] entries)...

  • Min heap class implementation in Python. Implement a min-using an array. Your min-heap class will have...

    Min heap class implementation in Python. Implement a min-using an array. Your min-heap class will have one private attribute, an array of integers. Implement the following methods for the min-heap class You may not use the built-in min function. init - Constructor makes an empty heap str - Prints the heap out in any way you want for debugging only) makenull(self) - Makes the heap empty insert(self,x) - Insert element x into the heap parent(self,i) - Returns the index of...

  • java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait)...

    java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • COMPUTER SCIENCE C++ HELP! Create a design document and run a manual paper and pencil approach...

    COMPUTER SCIENCE C++ HELP! Create a design document and run a manual paper and pencil approach to the implementation, insert and deletion of a MinHeap. Use the input values: 50, 30, 45, 15, 65, 25, 10, 25, 15, 5 Part-1 1. Show insert of each item to the Heap implemented as array and show all the state of the array on the process of each insertion. 2. Show the first index and the last index of the heap in your...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a...

    a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a collection of n values, where 0 < k < n. Write a program that uses a minheap method to find the kth smallest value in a collection of n values. Use the MinHeap class defined in part a. public final class MinHeap<T extends Comparable<? super T>>              implements MinHeapInterface<T> {    private T[] heap;      // Array of heap entries; ignore heap[0]    private int...

  • Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the...

    Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the function headers would be the following: class MaxHeap { vector<int> data; public: MaxHeap() { // ... } int size() { // ... } int maxLookup() { // ... } void extractMax() { // ... } void insert(int data) { // ... } void remove(int index) { // ... } }; ======================== import java.util.Arrays; import java.util.Scanner; public class MaxHeap { Integer[] a; int size; //...

  • Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an...

    Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an array of elements as a parameter and uses that array as the heap rather than creating a new array. Of course, the array passed in is probably not in proper heap ordering, so you must rearrange it until it is. There's a neat trick for achieving this: If you just "bubble down" all of the non-leaf nodes of the heap, starting from the last...

  • Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an...

    Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an array of elements as a parameter and uses that array as the heap rather than creating a new array. Of course, the array passed in is probably not in proper heap ordering, so you must rearrange it until it is. There's a neat trick for achieving this: If you just "bubble down" all of the non-leaf nodes of the heap, starting from the last...

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