DSA Lab
Heap and priority queue
A binary heap is a complete binary tree kept in a plain array, with one rule: every parent comes before its children (smaller in a min-heap, larger in a max-heap). So the best item is always at index 0.
Heap and priority queue: how it works
In this tool: sift up, sift down, min or max. Put in your own values and watch it change. Each operation runs one step at a time and says in plain words what happened, with its calculation worked out. The code is shown in Python, C, C++ or Java with the line that just ran marked, and an Understand it panel gives the idea, why it works, the cost of each operation, common mistakes and a short quiz.
Lessons that use it
- Heaps and priority queues (Data Structures)A complete binary tree stored in an array: push with sift-up, pop the minimum with sift-down, and build a heap in O(n).
- Heap sort and counting sort (Algorithms)Heap sort: O(n log n) in place. Counting sort: O(n + k) without comparing, and why comparison sorts cannot beat n log n.
- Heaps and intervals (Interview Prep)Top-k problems with a heap (heapq), merging overlapping intervals after sorting, and counting meeting rooms by sweeping start and end times.