Binary Heap
- A binary heap is a nearly complete binary tree stored in an array that satisfies:
- Structure property: the tree is a complete binary tree (every level is filled except possibly the last; filled from left to right)
- Order property:
- Max-heap: for every node other than the root,
- Min-heap; for every node other than the root,
- In a max-heap, the maximum is at , in a min-heap, the minimum is at (the root).
- Using 1-based indexing:
- The height of a heap with nodes is
Key functions
MAX-HEAPIFY
Goal: to fix a single possible violation at node , assuming its children’s subtrees already satisfy the max heap property.
MAX-HEAPIFY(A, i):
l ← left(i)
r ← right(i)
largest ← i
if l ≤ A.heap_size and A[l] > A[largest]:
largest ← l
if r ≤ A.heap_size and A[r] > A[largest]:
largest ← r
if largest ≠ i:
swap A[i], A[largest]
MAX-HEAPIFY(A, largest)
def max_heapify(A, i, heap_size):
left = 2 * i + 1
right = 2 * i + 2
largest = i
if left < heap_size and A[left] > A[largest]:
largest = left
if right < heap_size and A[right] > A[largest]:
largest = right
if largest != i:
A[i], A[largest] = A[largest], A[i]
max_heapify(A, largest, heap_size)
- Running time. At most the height of the node . A common bound uses the recurrence since the violating element moves into a subtree of size at most , giving
BUILD-MAX-HEAP
Converts an array into a max-heap by calling MAX-HEAPIFY in a bottom-up manner.
BUILD-MAX-HEAP(A):
A.heap_size ← length(A)
for i ← ⌊length(A)/2⌋ downto 1:
MAX-HEAPIFY(A, i)
def build_max_heap(A):
heap_size = len(A)
# Start from the last non-leaf node and go upwards
for i in range((heap_size // 2) - 1, -1, -1):
max_heapify(A, i, heap_size)It is actually ).
HEAPSORT
Use the heap as a selection data structure:
- Build a max-heap on the array
- Repeatedly swap the maximum at the root with the last element in the heap, shrink the heap and restore the heap with
MAX-HEAPIFY
HEAPSORT(A):
BUILD-MAX-HEAP(A)
for i ← length(A) downto 2:
swap A[1], A[i]
A.heap_size ← A.heap_size - 1
MAX-HEAPIFY(A, 1)