Non-Linear Data StructuresIntermediate LevelStudy Time: 25 mins

Heap & Priority Queue Data Structure

A Heap is a complete binary tree structure satisfying the heap property: in a Max Heap, every parent node is greater than or equal to its children.

Loading visualizer workspace...

Max Heap Insertion & Bubble Up Step-by-Step

Sample Input:Max Heap: [50, 30, 20], Insert: 60
Step 1

Insert Value at Array End

Append new element 60 at end of complete binary tree array.

Array: [50, 30, 20, 60] (Inserted 60 at index 3)
Complete binary tree property enforces insertion at next available leaf.
Step 2

Compare with Parent Node

Parent of index 3 is index 1 (value 30). Since 60 > 30, heap property is violated.

Compare: Child(60) > Parent(30) -> Swap required
Parent index formula in 0-indexed array: Math.floor((i - 1) / 2).
Step 3

Bubble Up to Root

Swap 60 and 30. Then compare 60 with root 50. Swap 60 and 50 so 60 becomes root.

Heap Array: [60, 50, 20, 30] (Max Heap property restored)
Bubbling up takes at most O(log N) swaps along tree height.

Real-world analogy: Hospital ER Triage

Max Heap
Heapify
Hover over items for details

Patients entering emergency triage are prioritized by illness severity. The most urgent patient is always treated next regardless of arrival time.

  • Max Heap= Root contains the maximum element in the dataset.
  • Heapify= Rebalances parent and child nodes after insertions or extractions.

Operations explained

Complexity analysis

Scenario Simulator: Dataset Size (N = 500)
Presets:
N = 10N = 10,000
Complexity Curve MapHeap & Priority Queue: O(log N) Tree Height Time
Time (t)Size N (500)N = 500O(N²)O(N log N)O(N)O(log N)O(1)Heap & Priority
Time Steps (N=500)

~9 steps (Logarithmic)

O(log N) Tree Height Time

Space Footprint

O(N) Tree Nodes Space

Memory growth rate as N expands.

Performance Rank
Optimal Tier

Relative efficiency rating for large N.

OperationTime ComplexitySpace Complexity
Extract Max / MinO(log N)O(1)
Get Max / MinO(1)O(1)
Build HeapO(N)O(1)

Code implementations

class MinHeap {
  constructor() { this.heap = []; }
  push(val) { this.heap.push(val); this.bubbleUp(); }
  pop() { /* extract top */ }
}

Real-world applications

Case 01

Priority Queue

Schedules OS CPU tasks by urgency ranks.

Case 02

Dijkstra Algorithm

Selects minimum distance graph vertex dynamically.

Interview questions

Topic Knowledge Test & Assessment

10 Questions Assessment

Heap Knowledge & Skill Test

Validate your conceptual understanding, operation mechanics, and core concepts of Heap.