Heap
A tree where each element is smaller (or larger) than its children, so the smallest (or largest) is always on top.
Example
In a heap of 1 million tasks, seeing the most urgent is 1 step and removing it takes about 20.
Test yourself: Heap
Heaps
In a min-heap, the smallest value is at the ___.
Each parent is smaller than its children, so nothing is smaller than the root. With 1 million values, the minimum is read in 1 step.
Heaps
In a heap stored in an array, the highest-priority element is at index ___.
The root comes first in level order. Peeking at the maximum of a 10,000-element max-heap is reading index 0.
Heaps
Adding an element to a heap of n elements costs O(___).
The new one moves up at most to the root: as many steps as the height. With 1,000 elements, about 10.
0/3
â–¶ PlayLessons that use it
Measuring efficiency
Fast and light on memory
Types of structures
How they relate and where they live
Arrays
Adjacent cells
Trees in an array
No references, just math
Heaps
The smallest, always on top
Building a heap
Bottom-up is cheaper
The k largest
Keep the best without sorting everything
Heap sort
Take the maximum and leave it at the end
Bucket sort
Split into buckets and sort each one
Divide and conquer
Split, solve and combine
