< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page

Was this helpful?

  1. Priority Queue
  2. What's Priority Queue & Implementation

Maximum-Oriented PQ using Binary Heap

Topic # Binary Heap (detail) - Array that is sorted in decreasing order a max-oriented heap.

#include <iostream>
using namespace std;

template <typename key>
class MaxPQ
{
     key *arr;
     int N;
     int i = 0;

     bool less(const key, const key);

     // ! Swim & sink are heap helper functions
     // Promotion: child's key becomes a larger key than its parent's key
     void swim(int);

     // Demotion: Parent's key becomes smaller than one (or both) of its children's
     void sink(int);

     bool isEmpty();

public:
     MaxPQ(int);

     MaxPQ(const MaxPQ<key> &);

     bool isFull();

     // At most 1 + lg N compares
     void insert(const key);

     // At most 2 lg N compares
     key delMax();

     void print();
};

template <typename key>
MaxPQ<key>::MaxPQ(const MaxPQ<key> &obj)
{
     this->N = obj.N;
     this->arr = new key[obj.N];
     this->i = obj.i;
     for (int j = 1; j <= i; j++)
          this->arr[j] = obj.arr[j];
}

template <typename key>
MaxPQ<key>::MaxPQ(int capacity)
{
     N = capacity + 1;
     arr = new key[capacity + 1]{NULL};
}

template <typename key>
void MaxPQ<key>::print()
{
     cout << endl;
     for (int j = 1; j <= i; j++)
          cout << arr[j] << " ";
     cout << endl;
}

template <typename key>
bool MaxPQ<key>::isFull()
{
     return i == N - 1;
}

template <typename key>
bool MaxPQ<key>::isEmpty()
{
     return i == 0;
}

template <typename key>
bool MaxPQ<key>::less(const key key1, const key key2)
{
     return key1 < key2;
}

template <typename key>
void MaxPQ<key>::swim(int k)
{
     while (k > 1 && less(arr[k / 2], arr[k]))
     {
          swap(arr[k], arr[k / 2]);
          k = k / 2;
     }
}

template <typename key>
void MaxPQ<key>::sink(int k)
{
     while (2 * k <= i)
     {
          int j = 2 * k;

          if (j < i && less(arr[j], arr[j + 1]))
               j++; // find out which one is bigger 2k or 2k+1

          if (!less(arr[k], arr[j]))
               break; // break if the parent's key is larger than both children's key

          swap(arr[k], arr[j]);
          k = j; // moving down the heap
     }
}

template <typename key>
void MaxPQ<key>::insert(const key val)
{
     arr[++i] = val;
     swim(i);
}

template <typename key>
key MaxPQ<key>::delMax()
{
     if (isEmpty())
          return -1;
     key max = arr[1];
     swap(arr[1], arr[i--]);
     sink(1);
     arr[i + 1] = NULL; // prevent loitering
     return max;
}

int main()
{
     MaxPQ<int> pq(6);
     while (!pq.isFull())
     {
          int key;
          cin >> key;
          pq.insert(key);
     }
     pq.print();

     MaxPQ<int> pq2(pq); // calling copy Construstor

     cout << "Max Node: " << pq.delMax()<<endl;
     cout << "Max Node: " << pq.delMax()<<endl;
     pq.print();

     cout << "--PQ2--";
     pq2.print();
}

PreviousOrderedArrayMaxPQ.JavaNextMinimum-Oriented PQ using Binary Heap

Last updated 10 months ago

Was this helpful?

🏧
Page cover image