< 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

Minimum-Oriented PQ using Binary Heap

Topic # Binary Heap (detail)

#include <iostream>
using namespace std;

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

     bool greeter(key key1, key key2)
     {
          return key1 > key2;
     }
     // ! swim & sink are heap helper functions

     // Promotion: Child's key becomes smaller key than its parent's key
     void swim(int k)
     {
          while (k > 1 && greeter(arr[k / 2], arr[k]))
          {
               swap(arr[k], arr[k / 2]);
               k = k / 2;
          }
     }
     // Demotion: Parent's key becomes Bigger than one (or both) of its children's
     void sink(int k)
     {
          while (2 * k <= i)
          {
               int j = 2 * k;

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

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

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

     bool isEmpty()
     {
          return i == 0;
     }

public:
     MinPQ(int capacity) : N(capacity + 1)
     {
          arr = new key[capacity + 1]{NULL};
     }

     bool isFull()
     {
          return i == N - 1;
     }

     // At most 1 + lg N compares
     void insert(key val)
     {
          arr[++i] = val;
          swim(i);
     }

     // At most 2 lg N compares
     key delMin()
     {
          if (isEmpty())
               return -1;
          key min = arr[1];
          swap(arr[1], arr[i--]);
          sink(1);
          arr[i + 1] = NULL; // prevent loitering
          return min;
     }

     void print()
     {
          cout << endl;
          for (int j = 1; j <= i; j++)
               cout << arr[j] << " ";
          cout << endl;
     }
};

int main()
{
     MinPQ<int> *bt = new MinPQ<int>(6);
     while (!bt->isFull())
     {
          int key;
          cin >> key;
          bt->insert(key);
     }
     bt->print();
     cout << "Max Node: " << bt->delMin();
     bt->print();
}
PreviousMaximum-Oriented PQ using Binary HeapNextProblems

Last updated 10 months ago

Was this helpful?

🏧
Page cover image