Question

In: Computer Science

How to write a max-heap implementation of a templated priority queue class using the STL std::vector...

How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array

Solutions

Expert Solution

C++ program to implement max heap implementation of priority queue using stl std::vectors

#include<bits/stdc++.h>
#include<vector>

using namespace std;
 
void Push(vector<int>& heap, int value)
 {
    heap.push_back(value);
    push_heap(heap.begin(), heap.end(), greater<int>());
}

int Pop(vector<int>& heap) 
{
    int value1 = heap.front();
     
    // Now, This operation will transfer the smallest element to the very end of the vector

    pop_heap(heap.begin(), heap.end(), greater<int>());
 
    //Now, Removes the last element from the vector, which is also the smallest element
    
    heap.pop_back();  
    
    return value1;
}
// Main function

int main()
 {
    vector<int> heap1;
    Push(heap1, 1);
    Push(heap1, 7);
    Push(heap1, 5);
 
    while (!heap1.empty())
     {
         cout << Pop(heap1) << "\n";
    }
    return 0;
}

Output screen:

Thank you!!! Good luck! keep coding :)


Related Solutions

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...
A max-heap can be used as a max-priority queue, which is an abstract data type having...
A max-heap can be used as a max-priority queue, which is an abstract data type having the operations Insert, Maximum, Extract-Max, and Increase-Key. a. Describe how to implement a FIFO queue with a priority queue, and analyze the time complexity of the implementation (Insert and Delete operations), assuming that a heap is used for the priority queue. b. Describe how to implement a LIFO queue (i.e. a stack) with a priority queue, and analyze the time complexity of the implementation...
Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided...
Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided MPQ class. The functions from MPQ that are virtual function (remove min(), is empty(), min(), and insert()) must be implemented in the child classes. The functions remove min() and min() should throw an exception if the minimum priority queue is empty. For the UnsortedMPQ class, you will use a vector to implement the minimum priority queue functions. The insert() function should be O(1) and...
#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a...
#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a heap using namespace std; /* In this lab you will implement a data structure that supports 2 primary operations: - insert a new item - remove and return the smallest item A data structure that supports these two operations is called a "priority queue". There are many ways to implement a priority queue, with differernt efficiencies for the two primary operations. For this lab,...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node will have a 4 digit job number and a priority (A, B, etc) with A highest priority In the driver Create a priority queue object Add 3 jobs of 'B' priority Add 4 jobs of 'D' priority Add 2 jobs of highest priority Print the queue
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying...
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying to understand how this works. Thank you
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and a method to get all the values of the queue. (This is not writing a file implementing the java class PriorityQueue, but rather you are writing a program that is a priority queue).
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is the right-most node. The remove (de-queue) operation returns the node with the highest priority (key). If displayForward() displays List (first-->last) : 10 30 40 55 remove() would return the node with key 55. Demonstrate by inserting keys at random, displayForward(), call remove then displayForward() again. You will then attach a modified DoublyLinkedList.java (to contain the new priorityInsert(long key) and priorityRemove() methods). Use the provided...
Discuss tradeoffs between using the C++ STL list and vector.
Discuss tradeoffs between using the C++ STL list and vector.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT