Binary Heap and HeapSort Review


A short summary of Chapter 6 Heapsort from Introduction to Algorithms
 

1. What is a binary heap?
A binary heap is a nearly complete binary tree that has the max(min) heap property: each node is greater than or equal to any of its children. As a result, the maximum(minimum) element in a max(min) heap is its root.

2. Representation and Access to parent, left and right child.
Binary heap can be represented as an array. If we denote the root as A[1], then for each node indexed by i, the indices of its parent, left child and right child are i/2, 2*i and 2*i +1, correspondingly.
When representing binary heap as an array, we need to hold two values. One is length of A and one is heap_size of A, in which heap_size is less or equal to the length, i.e. it is not necessary that all elements of array A are in the heap.

3. Key procedures

<1> Max-Heapify: the key procedure to maintain heap property. O(lg n).
<2> Build-Max-Heap: build a max binary heap in O(n lg n) time.
<3> Heap sort: runs in O(n lg n) time, in place sort.
<4> Other procedures that allow binary heap to be used as a priority queue: Max-Heap-Insert; Heap-Extract-Max; Heap-Increase-Key. All run in O(lg n) time.

4. Max-Heapify(A, i)

Assumption: binary trees rooted at Left(i) and Right(i) are max-heaps, but A[i] might be smaller than its children, which violates the max-heap property.
Key idea: let A[i] "float down" in the max heap. At each iteration, exchange A[i] with max(Left(i), Right(i)).
Code:

MAX-HEAPIFY(A, i)
1 l ← LEFT(i)
2 r ← RIGHT(i)
3 if l ≤ heap-size[A] and A[l] > A[i]
4   then largest ← l
5   else largest ← i
6 if r ≤ heap-size[A] and A[r] > A[largest]
7   then largest ← r
8 if largest ≠ i
9   then exchange A[i] ↔ A[largest]
10 MAX-HEAPIFY(A, largest)


5. Build-Max-Heap(A)
Idea: if we view a random array as a nearly complete binary tree, the leaves are elements A[n/2 + 1] up to A[n]. (NOTE that index starts from 1 rather than 0). We can use Max-Heapify to build a max heap in a bottom-up manner to convert an array to a max heap.
Code:

BUILD-MAX-HEAP(A)
1 heap-size[A] ← length[A]
2 for i ← length[A]/2 to 1
3   do MAX-HEAPIFY(A, i)


6. Heap-Extract-Max(A)
The Idea is very straightforward.
Code:

HEAP-EXTRACT-MAX(A)
1 if heap-size[A] < 1
2   then error "heap underflow"
3 max ← A[1]
4 A[1] ← A[heap-size[A]]
5 heap-size[A] ← heap-size[A] - 1
6 MAX-HEAPIFY(A, 1)
7 return max

 

7. Heap-Sort(A)
Idea: we first build a max heap and then extract the max element(the root) and decrease the size of heap-size.
Code:

HEAPSORT(A)
1 BUILD-MAX-HEAP(A)
2 for i ← length[A] downto 2
3   do exchange A[1] ↔ A[i]
4     heap-size[A] ← heap-size[A] - 1
5     MAX-HEAPIFY(A, 1)

posted on 2011-03-18 02:54  chihits  阅读(321)  评论(0编辑  收藏  举报

导航