โ† Back to Heaps
๐Ÿ”๏ธ

Heap Intro & Concepts

1 of 6

Python Heaps Practice ๐Ÿ”๏ธ

Priority Queues & Heap Data Structure

Welcome! Learn how heaps work โ€” the data structure behind priority queues, scheduling, and finding min/max efficiently!


๐ŸŽฏ What is a Heap?

A Heap is a special tree-based data structure where the parent is always smaller (min-heap) or larger (max-heap) than its children.

Real-World Examples:

  • ๐Ÿฅ Emergency room โ€” Most critical patients are seen first
  • ๐Ÿ“‹ Task scheduler โ€” Highest priority tasks run first
  • ๐ŸŽฎ Leaderboard โ€” Quickly find top scores
  • ๐Ÿ“ฆ Shipping โ€” Process urgent packages first

Key Idea: Always access the smallest (or largest) element in O(1) time!


๐Ÿ“Š How a Heap is Stored

A heap is a complete binary tree stored as a list/array!

        1           Index:  0
       / \
      3   5         Index:  1, 2
     / \ / \
    7  4 8  6       Index:  3, 4, 5, 6

As a list: [1, 3, 5, 7, 4, 8, 6]

Parent-Child Relationships (0-indexed):

parent(i) = (i - 1) // 2 left_child(i) = 2 * i + 1 right_child(i) = 2 * i + 2

Example:

heap = [1, 3, 5, 7, 4, 8, 6] # Node at index 1 (value 3): # Parent: (1-1)//2 = 0 โ†’ value 1 โœ“ (1 < 3) # Left child: 2*1+1 = 3 โ†’ value 7 # Right child: 2*1+2 = 4 โ†’ value 4

๐Ÿ”„ Heap Operations Visualized

๐Ÿ’ก Try the interactive animation on this page! Watch how insert and extract work step by step.

Insert (Push) โ€” Bubble Up โฌ†๏ธ

When inserting, add to the end and bubble up to maintain heap property:

import heapq heap = [1, 3, 5, 7, 4] heapq.heappush(heap, 2) print(heap) # [1, 3, 2, 7, 4, 5] # Step by step: # 1. Add 2 at end: [1, 3, 5, 7, 4, 2] # 2. Compare with parent (5): 2 < 5, swap! # [1, 3, 2, 7, 4, 5] # 3. Compare with parent (1): 2 > 1, stop!

Extract Min (Pop) โ€” Bubble Down โฌ‡๏ธ

When removing the min, replace root with last element and bubble down:

import heapq heap = [1, 3, 2, 7, 4, 5] smallest = heapq.heappop(heap) print(smallest) # 1 print(heap) # [2, 3, 5, 7, 4] # Step by step: # 1. Remove root (1), move last (5) to root: # [5, 3, 2, 7, 4] # 2. Compare with children (3, 2): 2 is smallest, swap! # [2, 3, 5, 7, 4] # 3. No more children, stop!

๐Ÿ Python's heapq Module

Python has a built-in min-heap module!

import heapq # Create a heap from a list nums = [5, 3, 8, 1, 4] heapq.heapify(nums) print(nums) # [1, 3, 8, 5, 4] # Push (insert) heapq.heappush(nums, 2) print(nums) # [1, 3, 2, 5, 4, 8] # Pop (extract min) smallest = heapq.heappop(nums) print(smallest) # 1 # Peek at min without removing print(nums[0]) # 2 (just access index 0!) # Push and pop in one step result = heapq.heappushpop(nums, 6) print(result) # 2 (pushed 6, popped smallest)

๐Ÿ”€ Max-Heap Trick

Python only has min-heap. For max-heap, negate the values!

import heapq # Max-heap using negation max_heap = [] values = [3, 1, 5, 2, 4] for val in values: heapq.heappush(max_heap, -val) # Negate! # Get max value largest = -heapq.heappop(max_heap) # Negate back! print(largest) # 5

๐Ÿ”๏ธ Heap Animation

Watch insert, extract, and build heap in action!

Min-Heap: Parent โ‰ค Children

1[0]3[1]5[2]7[3]4[4]8[5]6[6]

๐Ÿ”ข Array Index Formulas

Parent

โŒŠ(i โˆ’ 1) / 2โŒ‹

Left Child

2 ร— i + 1

Right Child

2 ร— i + 2

๐Ÿ‘† Click any node in the tree to see the formulas in action

Array Representation:

1[0]
3[1]
5[2]
7[3]
4[4]
8[5]
6[6]

๐Ÿ”จ Build Heap โ€” Process Order

Start from index โŒŠn/2โŒ‹ โˆ’ 1 = 2 and bubble down each node going left to index 0. Leaves (indices 3โ€“6) are already valid heaps โ€” skip them!

1[0]#3
3[1]#2
5[2]#1
7[3]leaf
4[4]leaf
8[5]leaf
6[6]leaf
Bubble down (#order)Leaf (skipped)
Root (Min)ProcessingComparingSwappingNormal

๐Ÿ“ Time Complexity

OperationTimeSpaceWhy
InsertO(log n)O(1)Bubble up through at most log n levels
Extract MinO(log n)O(1)Bubble down through at most log n levels
Peek (min)O(1)O(1)Min is always at index 0 (root)
Heapify (build)O(n)O(1)Bottom-up โ€” most nodes are leaves and don't move
Insert n itemsO(n log n)O(n)Each of n inserts costs O(log n)
SearchO(n)O(1)Heap is not sorted โ€” must check every element

Why is heapify O(n) and not O(n log n)?

Heapify works bottom-up: it calls bubble_down from the last non-leaf to the root. Most nodes are near the bottom of the tree and move very little:

Leaves (bottom)

n/2 nodes

0 moves

Level above

n/4 nodes

โ‰ค 1 move

Next level

n/8 nodes

โ‰ค 2 moves

Root (top)

1 node

โ‰ค log n moves

Total work: n/4 ยท 1 + n/8 ยท 2 + n/16 ยท 3 + ... โ‰ˆ n (geometric series). Half the nodes do zero work, a quarter do one swap โ€” it adds up to O(n), not O(n log n).

๐Ÿ’ป

Try it yourself

Code: Heap Intro & Concepts

Loading Python runtimeโ€ฆ
Python โ€ฆ
Loading...
Output
Click "Run" to execute your code...
โ„น๏ธ About this Python environment

โœ… Standard library: heapq, collections, itertools, math, random, functools, datetime, bisect

โœ… Functions, classes, recursion, print()

โŒ No file system, subprocess, OS access, or network requests

โŒ No pip install (all supported modules are pre-loaded)

โฑ๏ธ 5 second execution time limit