Search
 
SCRIPT & CODE EXAMPLE
 

CPP

min heap and max heap using priority queue

#include <bits/stdc++.h>
#define cl cout << endl;
using namespace std;
int main()
{
    priority_queue<int, vector<int>,greater<int>> pqm; //min heap
    priority_queue<int, vector<int>> pq;  //max heap
    vector<int> v = {100, 2, 89, 36, 42, 91, 555};

    for (int i = 0; i < v.size(); i++)
    {

        pq.push(v[i]);
        pqm.push(v[i]);
        

        // pq.push(temp);
    }

        cout<<"max heap is: ";
        cl

        while (!pq.empty())
        {
            int temp = pq.top();
            pq.pop();
            cout << temp << endl;
        }

        cout<<"min heap is: ";
        cl

        while (!pqm.empty())
        {
            int temp = pqm.top();
            pqm.pop();
            cout << temp << endl;
        }

    return 0;
}
Comment

create a min heap in java using priority queue

int arr[]={1,2,1,3,3,5,7};
        PriorityQueue<Integer> a=new PriorityQueue<>();
        for(int i:arr){
            a.add(i);
        }
        while(!a.isEmpty())
            System.out.println(a.poll());
Comment

min heap priority queue c++

#include<queue>
std::priority_queue <int, std::vector<int>, std::greater<int> > minHeap; 
Comment

Difference between Priority Queue and Heap


This website provides a really clear explanation. http://pages.cs.wisc.edu/~vernon/cs367/notes/11.PRIORITY-Q.html

In short, a priority queue can be implemented using many of the data structures that we've already studied (an array, a linked list, or a binary search tree). However, those data structures do not provide the most efficient operations. To make all of the operations very efficient, we'll use a new data structure called a heap.

Comment

priority queue with min heap

This is frequently used in Competitive Programming.
We first multiply all elements with (-1). Then we create a max heap (max heap is the default for priority queue). 
When we access the data and want to print it we simply multiply those elements with (-1) again.
Comment

Heap vs Priority Queue

A priority queue is not a data structure, it is abstract. It does not say how things are implemented.
A heap IS a data structure. It specifies how things are implemented
In the real world, we implement heaps
Comment

Priority Queue using Min Heap in c++

#include <bits/stdc++.h>
using namespace std;
 

int main ()
{
   
    priority_queue <int> pq;
    pq.push(5);
    pq.push(1);
    pq.push(10);
    pq.push(30);
    pq.push(20);
 
   
    while (pq.empty() == false)
    {
        cout << pq.top() << " ";
        pq.pop();
    }
 
    return 0;
}
Comment

priority queue using heap

// C++ code to implement priority-queue
// using array implementation of
// binary heap
 
#include <bits/stdc++.h>
using namespace std;
 
int H[50];
int size = -1;
 
// Function to return the index of the
// parent node of a given node
int parent(int i)
{
 
    return (i - 1) / 2;
}
 
// Function to return the index of the
// left child of the given node
int leftChild(int i)
{
 
    return ((2 * i) + 1);
}
 
// Function to return the index of the
// right child of the given node
int rightChild(int i)
{
 
    return ((2 * i) + 2);
}
 
// Function to shift up the node in order
// to maintain the heap property
void shiftUp(int i)
{
    while (i > 0 && H[parent(i)] < H[i]) {
 
        // Swap parent and current node
        swap(H[parent(i)], H[i]);
 
        // Update i to parent of i
        i = parent(i);
    }
}
 
// Function to shift down the node in
// order to maintain the heap property
void shiftDown(int i)
{
    int maxIndex = i;
 
    // Left Child
    int l = leftChild(i);
 
    if (l <= size && H[l] > H[maxIndex]) {
        maxIndex = l;
    }
 
    // Right Child
    int r = rightChild(i);
 
    if (r <= size && H[r] > H[maxIndex]) {
        maxIndex = r;
    }
 
    // If i not same as maxIndex
    if (i != maxIndex) {
        swap(H[i], H[maxIndex]);
        shiftDown(maxIndex);
    }
}
 
// Function to insert a new element
// in the Binary Heap
void insert(int p)
{
    size = size + 1;
    H[size] = p;
 
    // Shift Up to maintain heap property
    shiftUp(size);
}
 
// Function to extract the element with
// maximum priority
int extractMax()
{
    int result = H[0];
 
    // Replace the value at the root
    // with the last leaf
    H[0] = H[size];
    size = size - 1;
 
    // Shift down the replaced element
    // to maintain the heap property
    shiftDown(0);
    return result;
}
 
// Function to change the priority
// of an element
void changePriority(int i, int p)
{
    int oldp = H[i];
    H[i] = p;
 
    if (p > oldp) {
        shiftUp(i);
    }
    else {
        shiftDown(i);
    }
}
 
// Function to get value of the current
// maximum element
int getMax()
{
 
    return H[0];
}
 
// Function to remove the element
// located at given index
void remove(int i)
{
    H[i] = getMax() + 1;
 
    // Shift the node to the root
    // of the heap
    shiftUp(i);
 
    // Extract the node
    extractMax();
}
 
// Driver Code
int main()
{
 
    /*         45
            /      
           31      14
          /      /  
         13  20  7   11
        /  
       12   7
    Create a priority queue shown in
    example in a binary max heap form.
    Queue will be represented in the
    form of array as:
    45 31 14 13 20 7 11 12 7 */
 
    // Insert the element to the
    // priority queue
    insert(45);
    insert(20);
    insert(14);
    insert(12);
    insert(31);
    insert(7);
    insert(11);
    insert(13);
    insert(7);
 
    int i = 0;
 
    // Priority queue before extracting max
    cout << "Priority Queue : ";
    while (i <= size) {
        cout << H[i] << " ";
        i++;
    }
 
    cout << "
";
 
    // Node with maximum priority
    cout << "Node with maximum priority : "
         << extractMax() << "
";
 
    // Priority queue after extracting max
    cout << "Priority queue after "
         << "extracting maximum : ";
    int j = 0;
    while (j <= size) {
        cout << H[j] << " ";
        j++;
    }
 
    cout << "
";
 
    // Change the priority of element
    // present at index 2 to 49
    changePriority(2, 49);
    cout << "Priority queue after "
         << "priority change : ";
    int k = 0;
    while (k <= size) {
        cout << H[k] << " ";
        k++;
    }
 
    cout << "
";
 
    // Remove element at index 3
    remove(3);
    cout << "Priority queue after "
         << "removing the element : ";
    int l = 0;
    while (l <= size) {
        cout << H[l] << " ";
        l++;
    }
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ set element at index 
Cpp :: C++ area & circumference of a circle 
Cpp :: short hand if else in c++ 
Cpp :: return function in cpp 
Cpp :: template function in class c++ 
Cpp :: cpp tutorial 
Cpp :: c++ define constant 
Cpp :: c++ copy string 
Cpp :: x += c++ 
Cpp :: ceil value in c++ using formula 
Cpp :: C++ mutex header 
Cpp :: Dfs program in c++ 
C :: remix icon cdn 
C :: Write a C program to print all unique elements in the array. 
C :: print an array in c 
C :: sstf program in c 
C :: get window width height glfw 
C :: dvlprroshan 
C :: multiplication table using c 
C :: npm fix old lockfile 
C :: c concatenate strings 
C :: c integer to string 
C :: random float number in C 
C :: xor swap 
C :: c if 
C :: .sh template 
C :: how to print in c 
C :: ** in c 
C :: prime factorization in c 
C :: c bubble sort 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =