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:
public:
#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)
Could anyone help me solve this C++ problem? Thanks a lot create a class called 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)...
Create a LIFO class with following methods in java - public LifoList() The default constructor for LifoList class which will initialize your class variables maxSize to 0 and the int array to null. public LifoList(int maxSize) The constructor for LifoList class which takes the int input maxSize to initialize the size of the array. You should NOT reinitialize the array elsewhere. For example, creating an object as new LifoList(5) should create a LifoList whose maximum size is 5. If the...
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) 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. 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 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. 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 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 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; //...
//implement the binomial heap please import java.util.LinkedList; import java.util.NoSuchElementException; /** * Binomial Heap Implementation * * @author First Last * @since ${date} */ public class BinomialHeap<T extends Comparable<? super T>> implements binomialHeapInterface<T> { private static final int DEFAULT = 5; // default size for the binomial heap public Node<T>[] forest; private int n; private boolean isMaxHeap; /** * Node Class for nodes in Binomial Heap */ protected class Node<T> { private T value; private int degree; private LinkedList<Node<T>> children; /**...