RandomWits

life is too short for a diary




Cribsheet for Heap data structure

Tags: java python algorithm heap

Author
Written by: Tushar Sharma
Featured image for Cribsheet for Heap data structure

A heap is a specialized tree-based data structure that satisfies the heap property: for every node \(i\) other than the root, \(A[\text{parent}(i)] \ge A[i]\) (Max-Heap) or \(A[\text{parent}(i)] \le A[i]\) (Min-Heap).

Core Properties

Key Algorithms

1. Max-Heapify (\(O(\log n)\))

Assumes the left and right subtrees of node \(i\) are already max-heaps, but \(A[i]\) might be smaller than its children. It "bubbles down" \(A[i]\) to its correct position.

2. Build-Max-Heap (\(O(n)\))

Converts an unordered array into a max-heap by calling Max-Heapify on all non-leaf nodes in bottom-up order (from \(n/2\) down to \(1\)).

3. Heapsort (\(O(n \log n)\))

  1. Build a max-heap from the array.
  2. Swap the root (max element) with the last element of the heap.
  3. Reduce heapsize and call Max-Heapify on the new root.
  4. Repeat until the heap is empty.

Priority Queue API

A Max-Priority Queue maintains a set \(S\) and supports:

Using Standard Libraries

// Min Heap (Default)
PriorityQueue<Integer> minHeap = new PriorityQueue<>();

// Max Heap
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());

// Operations
minHeap.offer(10);    // Insert - O(log n)
minHeap.peek();       // Maximum - O(1)
minHeap.poll();       // Extract-Min - O(log n)
import heapq

# Min Heap (Default)
min_heap = []
heapq.heapify(min_heap) # O(n)

# Operations
heapq.heappush(min_heap, 10)  # Insert - O(log n)
heapq.heappop(min_heap)       # Extract-Min - O(log n)
min_heap[0]                    # Minimum - O(1)

# Max Heap: Use negative values

Common Patterns

  1. Top K Elements: Use a Min-Heap of size \(K\).
  2. Median from Stream: Use a Max-Heap (lower half) and a Min-Heap (upper half).
  3. Greedy with Cooldown: Use a Max-Heap for the next best item and a Queue for items in cooldown.

comments powered by Disqus