Solve 25 questions to master Heap Data Structure patterns

Raghav Nyati
3 min readFeb 2, 2021

--

This post is to deep dive into one of the most fundamental data structures called Heap. The objective of this post is to understand the basics of Heap. time complexities, and identify patterns when to use Heap as a data structure.

What is Heap?

  1. It is mainly used to represent a priority queue.
  2. It is represented as a Binary Tree (a tree structure where a node of a tree has a maximum of two child nodes). Heaps are complete binary trees.
  3. A simple array can be used to represent a Heap where array indices refer to the node position in the tree.
  4. Parent and child nodes can be accessed with indices:
    * A root node|i = 0, the first item of the array
    * A parent node|parent(i) = i / 2
    * A left child node|left(i) = 2i
    *
    A right child node|right(i)=2i+1
  5. Two type of Heaps — Min Heap, Max Heap
  6. Min Heap — the parent node always has a smaller value than the child nodes.
    Max Heap — the parent node is always larger than the child node value.
  7. Usually, when a type is not mentioned, it refers to the MinHeap. Python Heap has minHeap as default.
  8. In python, theheapqmodule provides the basic features for Heap data structure.
  9. minHeap are used in tasks related to scheduling or assignment. A more detailed explanation is under the Patterns section below.

Heap Operations

The basic operations in Python heapq are:

heapify

The heapify operation converts the iterable array heap into a tree structure w.r.t heap order.

heappush

It inserts an element into the heap. Post insertion the heap order is adjusted to maintain the heap properties.

import heapq as hq# Simple array is heap
minHeap = []
# Adding an element to the heap
hq.heappush(minHeap, 5)

heappop

This operation is to remove the element from the heap. By default it is minHeap, so this operation removes the min element from the minHeap. And for maxHeap, it is the maximum element. Post removal, heapify is called internally to maintain the heap order.

import heapq as hqminHeap = [5, 6, 2]# this is done to convert iterable into a heap tree
hq.heapify(minHeap)
# Getting top element from the heap
value = hq.heappop(minHeap) # the value here is 2 as 2 is the minimum value.

Other operations in heapq python module includes heappushpop ,heapreplace , nlargest , nsmallest .

Python Implementation of MIN HEAP

Different Patterns:

  1. Top K
  2. Merge K Sorted
  3. Two Heaps
  4. Minimum Number

Top K Pattern

Practice 1: Kth largest number in an array

LeetCode #215 (Medium)

Practice 2: K closest points to Origin

LeetCode #973 (Medium)

Practice 3: Top k frequent elements/numbers

LeetCode #347 (Medium)

Practice 4: Top K frequent words

LeetCode #692 (Medium)

Practice 5: Ugly Number II

LeetCode #264 (Medium)

Practice 6: Frequency Sort (Sorting string based on the frequency of characters)

LeetCode #451 (Medium)

Practice 7: Kth smallest number in an array

Practice 8: K largest number in a stream

LeetCode #703 (Easy)

Practice 9: Kth smallest distance among all pairs

LeetCode #719 (Hard)

Practice 10: Reorganize String

LeetCode #767 (Medium)

Practice 11: Rearrange string K distance apart

LeetCode #358 (Hard)

Extra:

LeetCode #1439 — Kth smallest sum of a matrix with sorted rows

Merge K Sorted Pattern

Practice 1: Merge K Sorted

LeetCode #23 (Hard)

Practice 2: K pairs with the smallest sum

LeetCode #373 (Medium)

Practice 3: K pairs with the largest sum (Hard)

Practice 4: K smallest element in a sorted matrix

LeetCode #378 (Medium)

Practice 5: K smallest number in M sorted lists

Two Heaps Pattern

Practice 1: Find median from a data stream.

LeetCode #295 (Hard)

Practice 2: Sliding Window Maximum.

LeetCode #239 (Hard)

Practice 3: Maximize Capital / IPO.

LeetCode #502 (Hard)

Minimum Number Pattern

Practice 1: Minimum cost to connect sticks/ropes

LeetCode #1167 (Medium)

Practice 2: Meeting Rooms II

LeetCode #253 (Medium)

Practice 3: Employee Free Time

LeetCode #759 (Hard)

Practice 4: Minimum cost to hire K Workers

LeetCode #857 (Hard)

Practice 5: Minimum number of CPU (Task Scheduler)

LeetCode #621 (Hard)

Practice 6: Minimum number of Refueling stops

LeetCode #871 (Hard)

References:

  1. [https://towardsdatascience.com/data-structure-heap-23d4c78a6962]

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Raghav Nyati
Raghav Nyati

Written by Raghav Nyati

Software developer by day and writer by night. INFJ. Always curious. Travel junkie. Avid reader. Coffee Addict. Dogs lover. I live in Seattle, WA.

No responses yet

Write a response