A Heap (Priority Queue) is a special tree where the smallest (or largest) element is ALWAYS at the top. Perfect when you repeatedly need the min/max! 🏔️
💡 Think of it like this: Imagine an emergency room. Patients aren't served in arrival order — the most critical patient is always treated first. That's a priority queue!
Heap operations:
heappush(heap, val) — Insert: O(log n)heappop(heap) — Remove smallest: O(log n)heap[0] — Peek at smallest: O(1)heapify(list) — Convert list to heap: O(n)-val, negate after popClassic heap interview problems:
result
[1, 3, 4, 5, 7, 8]