Data Structures19 sections · 729 units
Open in Course

Two Heaps Visualization

See the split

Elements: [1,2,3,4,5][1, 2, 3, 4, 5]

Max-heap (smaller half): [1,2,3][1, 2, 3], root = 33

Min-heap (larger half): [4,5][4, 5], root = 44

Median: with 55 elements, max-heap is larger. Its root (33) is the median.

If you add 66: Max-heap: [3,2,1][3, 2, 1], root = 33

Min-heap: [4,5,6][4, 5, 6], root = 44

Median: average of roots = (3+4)/2=3.5(3 + 4) / 2 = 3.5.