next up previous
Next: Up: Previous:

Pseudocode

Heapify(A,i)
1
$\;\;\;\;\;$l = left(i)
2
$\;\;\;\;\;$r = right(i)
3
$\;\;\;\;\;$if l $\leq$ HeapSize(A) and A[l] > A[i]
4
$\;\;\;\;\;$then largest = l
5
$\;\;\;\;\;$else largest = i
6
$\;\;\;\;\;$if r $\leq$ HeapSize(A) and A[r] > A[largest]
7
$\;\;\;\;\;$then largest = r
8
$\;\;\;\;\;$if largest $\neq$ i
9
$\;\;\;\;\;$then swap(A[largest], A[i])
10
$\;\;\;\;\;$ $\;\;\;\;\;$Heapify(A, largest)

If the parent is smaller than either of its children, does it matter whether we swap the parent with the larger or the smaller of the children? Yes - consider case where parent=3, child1=6, child2=7


next up previous
Next: Up: Previous: