~/coding-interview/what-is-heap-data-structure.md

What is Heap Data Structure

3 min readJuly 03, 2019by Gorav Singal#coding-interview#heap-data-structure#fundamentals
// TL;DR

A heap is a complete binary tree stored in an array where parent at index i has children at 2i+1 and 2i+2. Max-heap: parent >= children. Min-heap: parent <= children. Heapify runs in O(log n).

What is Heap Data Structure

Its a tree based data structure which is a complete binary tree(all nodes have 2 nodes, leaf-child may not have all 2), and satisfies a heap property. But, the amazing thing is that it uses array as its underlying data structure.

The heap is the implementation of the Priority Queue, where top most priority (or lowest priority) element is required to be at the top.

Heap Property

1. Max Heap

In this tree representation, every root node is always greater than both of its child. Note: right child can be lesser than left child.

Max Heap

2. Min Heap

As reverse of the max heap. Every root element is always smaller than both of its child elements.

Operations

Some basic operations on heap are:

  • heapify - To maintain either max or min heap property.
  • findMax or findMin - To get the top element of heap
  • insertElement - To insert a new element to the heap
  • extractMax or extractMin - Delete the top element, and return it
  • Get heapsize - Get the heap size
  • Increment/Decrement - For incrementing or decrementing priority or value of any node

How array represents the tree (Heap)

As I said earlier, they are based on array, not on any pointers stuff. Consider an array:

224,131,23,45,52,287,304,122,460,438

The first element will be the root of tree, 2nd and 3rd are the child of it. 4th and 5th will be child of 2nd node. And so on.

            224
           /    \
        131      23
       /   \    /   \
     45    52  287   304
    /  \    /
 122  460  438

Note: Above is just tree representation from array. It does not satisfy any max-heap or min-heap property.

Get Left child

leftChildIndex = rootIndex * 2 + 1

Get Right child

rightChildIndex = rootIndex * 2 + 2

Maintain Heap property - Max Heap

Lets assume we have to build a max heap. We would want every root node to be greater than its child nodes.

Maintain max heapify property for an index

void max_heapify(int[] arr, int index, int heapsize) {
    int l = 2 * index + 1;
    int r = 2 * index + 2;
    
    int indexOfLargest = index;
    if (l < heapsize && arr[l] > arr[index]) {
        indexOfLargest = l;
    }

    if (r < heapsize && arr[r] > arr[indexOfLargest]) {
        indexOfLargest = r;
    }
    
    if (indexOfLargest != index) {
        swap(arr, index, indexOfLargest);
        max_heapify(indexOfLargest);
    }
}

This operation takes O(log n)

Here for the index passed, we are comparing it with its child elements. If any child is greater than child, it will be swapped. Since, we have put a newer element to its kid. That sub-tree also has to satisfy max-heap property. And, we go in a natural recursive algorithm.

Build a max heap

void buildMaxHeap(int[] arr, int heapsize) {
    for (int i=heapsize/2; i>=0; i--) {
        max_heapify(arr, i, heapsize);
    }
}

This operation takes O(n log n) (including time complexity of max_heapify() as well)

Example

In above example of input, after buildMaxHeap, it will look like:

473,445,295,310,404,199,257,25,21,47

            473
           /    \
        445      295
       /   \    /   \
     310   404 199  257
    /  \    /
   25  21  47

Use cases of Heap Data Structure

Heap data structure has many usage, and is very useful data structure.

  • Used for implementing Priority Queue (Max/Min Priority Queue)
  • Maintaining n largest or smallest elements

$ 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

System Design Interview Vocabulary Notes

System Design Interview Vocabulary Notes

In this post, we will see some of the frequently used concepts/vocabulary in…

Coding Interview - Facebook System Design Interview Types

Coding Interview - Facebook System Design Interview Types

System design interview is pretty common these days, specially if you are having…

Find the maximum sum of any continuous subarray of size K

Find the maximum sum of any continuous subarray of size K

Introduction You are given an array of integers with size N, and a number K…

Binary Tree Data Structure

Binary Tree Data Structure

A Binary tree is a data structure which has two children nodes attached to it…

Binary Search Tree (BST) Data Structure

Binary Search Tree (BST) Data Structure

A Binary Search tree (BST) is a data structure which has two children nodes…

What FAANG companies expect in their interview from candidates

What FAANG companies expect in their interview from candidates

Its every software engineer’s dream to work with the big FAANG companies…

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…