~/coding-interview/min-priority-queue-with-heap.md

Min Priority Queue Implementation with Heap Data structure

// TL;DR

A min priority queue is backed by a min-heap array. Insert adds at the end and bubbles up; extract-min swaps root with last element and heapifies down. Both run in O(log n).

Min Priority Queue Implementation with Heap Data structure

Min Priority Queue is a data structure which manage a list of keys(values). And, gives priority to element with minimum value.

It supports following operations:

  1. getMin() - Gives the minimum priority element. Do not delete it.
  2. extractMin() - Gives the minimum priority element, and delete it.
  3. insert(element) - Insert an element into Priority queue
  4. decrement(index, newValue) - Decrement an element from priority queue

(Heap data structure)[/coding-interview/what-is-heap-data-structure/] can be used for its implementation.

Why not other data structure (other than Heap)?

Lets see each one of them:

  • Link list Runtime complexity is O(n^2) in putting minimum element at top each time, a new element has come. Or, priority change for an element.
  • Binary Search Tree Insertion is easy, getting minimum is an easy operation. It consumes extra space for keeping pointers for each node. In case of insertion and changing priority, tree needs to be re-balanced again, which is more complex than maintaining min heap property in heap data structure. Heap uses arrays, so accessing an element, caching an element are always faster operation.

Implementation

public class MinPriorityQueue {
    private int[] heap;
    private int maxSize;
    private int heapSize;

    public MinPriorityQueue(int maxSize) {
        this.maxSize = maxSize;
        this.heap = new int[maxSize];
        this.heapSize = 0;
    }

    @Override
    public String toString() {
        return ArrayUtils.toString(this.heap, this.heapSize);
    }

    private int getLeftChild(int index) { return 2*index + 1;}
    private int getRightChild(int index) { return 2*index + 2;}
    private int getParent(int index) { 
        if (index == 0) {
            return -1;
        }
        return (index-1)/2; 
    }

    private void minHeapify(int index) {
        int smallest = index;
        int l = this.getLeftChild(index);
        int r = this.getRightChild(index);
        
        if (l < this.heapSize && this.heap[l] < this.heap[index]) {
            smallest = l;
        }
        if (r < this.heapSize && this.heap[r] < this.heap[smallest]) {
            smallest = r;
        }
        if (smallest != index) {
            ArrayUtils.swap(this.heap, smallest, index);
            this.minHeapify(smallest);
        }
    }

    /**
     * Get the min element, do not delete
     */
    public int getMin() throws Exception {
        if (this.heapSize == 0) {
            throw new Exception("Heap underflow");
        }
        return this.heap[0];
    }

    /**
     * Get the min, and delete from heap
     * Runs in O(log n)
     */
    public int extractMin() throws Exception {
        if (this.heapSize == 0) {
            throw new Exception("Heap underflow");
        }

        int ret = this.heap[0];
        this.heap[0] = this.heap[this.heapSize-1];

        this.heapSize --;
        this.minHeapify(0);

        return ret;
    }

    /**
     * Set the value at index specified to new value specified
     */
    public void decrement(int index, int newValue) throws Exception {
        if (index > this.heapSize-1) {
            throw new Exception("Overflow");
        }

        this.heap[index] = newValue;
        while (index > 0 && this.heap[index] < this.heap[this.getParent(index)]) {
            ArrayUtils.swap(this.heap, index, this.getParent(index));
            index = this.getParent(index);
        }
    }

    public void insert(int val) throws Exception {
        if (this.heapSize >= this.maxSize) {
            throw new Exception("Overflow");
        }
        this.heapSize ++;
        this.heap[this.heapSize-1] = Integer.MAX_VALUE;
        this.decrement(this.heapSize-1, val);
    }

    public static void main(String[] args) throws Exception {
        MinPriorityQueue q = new MinPriorityQueue(10);

        //fresh state
        System.out.println(q);

        //lets insert 5 elements
        q.insert(10); q.insert(5); q.insert(4); q.insert(6); q.insert(20);
        System.out.println(q);
        System.out.println("Min: " + q.getMin());

        System.out.println("Extracting min: " + q.extractMin());
        System.out.println("State: " + q);
        
        System.out.println("Decrementing index-2 to 4");
        q.decrement(2, 4);
        System.out.println("State: " + q);
        
        System.out.println("Extracting min: " + q.extractMin());
        System.out.println("State: " + q);
        System.out.println("Extracting min: " + q.extractMin());
        System.out.println("State: " + q);
    }
}

Explanation

To know about basic Heap data structure, and its heapify operation. See: (Heap data structure)[/coding-interview/what-is-heap-data-structure/] Its an important data structure for interview purpose.

getMin()

Runtime complexity is O(1) Since we are maintaining min heap. The minimum value is always at 0th index. Just return it.

extractMin()

Runtime complexity is O(log n)

  • Get the top element, 0th index. Which is min.
  • For deletion, simply copy the last element to 0th index.
  • Decrease heap size by 1. Means, the last element index which we copied is out of heap scope now.
  • Now, since we have copied the last element to top. We have to maintain our Min-Heap property.
  • Call minHeapify()

decrement(index, newVal)

Runtime complexity is O(log n)

  • Simply, set heap value at specified index to new value.
  • Now, since that element was already smaller than all of its child. The newer value is already smaller then current value. So, the tree below this node is all min-heapified.
  • We need to maintain min-heapify property in upper tree.
  • Go till the root element, and swap this node value with its parent untill it is smaller than its parent.
  • So, its kind of bubbling up.

insert(element)

Runtime complexity is O(log n)

  • Increase heapsize for this new element
  • Put a maximum value(maximum value of an integer) at the newer index (last index)
  • Simply call decrement() method for this index, and the value we want to insert.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Graph Topological Sorting - Build System Order Example

Graph Topological Sorting - Build System Order Example

Graph Topological Sorting This is a well known problem in graph world…

Max Priority Queue Implementation with Heap Data structure

Max Priority Queue Implementation with Heap Data structure

Max Priority Queue is a data structure which manage a list of keys(values). And…

Leetcode Solution - Best Time to Buy and Sell Stock

Leetcode Solution - Best Time to Buy and Sell Stock

Problem Statement You are given an array prices where prices[i] is the price of…

Binary Tree - Level Order Traversal

Binary Tree - Level Order Traversal

Problem Statement Given a Binary tree, print out nodes in level order traversal…

Four Sum - Leet Code Solution

Four Sum - Leet Code Solution

Problem Statement Given an array nums of n integers and an integer target, are…

Leetcode - Rearrange Spaces Between Words

Leetcode - Rearrange Spaces Between Words

Problem Statement You are given a string text of words that are placed among…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…