A binary heap is a complete binary tree stored compactly in an array: parent i has children at 2i+1 and 2i+2. In a max-heap every parent is ≥ its children, so the maximum always sits at the root. Inserting places the new value at the end and 'bubbles' it up; extracting the max removes the root, moves the last element up, and 'sinks' it down.
Max-heap insert and extractArray layout: [50, 30, 70, 20, 40, 60, 80]
child(2i+1), child(2i+2), parent((i-1)/2)
Max-heap invariant: value[i] ≥ value[2i+1], value[2i+2]
Insert → append + bubble up (≤ log n swaps)
Extract max → swap root & last, remove last, sink down
Heap-sort connection
Repeatedly extracting the root of a max-heap yields the elements in descending order — that is exactly how heap sort works, in O(n log n) with O(1) extra space.