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

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 :: nth node from end of linked list 
Cpp :: doubly linked list c++ code 
Cpp :: string to long integer c++ 
Cpp :: how to get an element in a list c++ 
Cpp :: c++ init multidimensional vector 
Cpp :: cpp Sieve algorithm 
Cpp :: check if character is uppercase c++ 
Cpp :: how to iterate throguh a string in c++ 
Cpp :: cpp insert overload operator 
Cpp :: c++ get ascii value of char 
Cpp :: how to get the first element of a map in c++ 
Cpp :: how to rotate canvas android 
Cpp :: c++ vector move element to front 
Cpp :: c++ vector size 
Cpp :: c++ remove last character from string 
Cpp :: check if whole string is uppercase 
Cpp :: cpp vector2 
Cpp :: why is using namespace std a bad practice 
Cpp :: how to use cout function in c++ 
Cpp :: c++ initialize vector of vector with size 
Cpp :: reverse order binary tree in c++ 
Cpp :: cpp vs c# 
Cpp :: how to add external library in clion 
Cpp :: ++i and i++ 
Cpp :: convert characters to lowercase c++ 
Cpp :: convert int to string in c++ 
Cpp :: c++ compile to exe command line 
Cpp :: cpp vector 
Cpp :: c++ for loop multiple variables 
Cpp :: string format decimal places c++ 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =