next up previous
Next: Up: Previous:

Insert



While loop iterates through height of heap and is thus O(lgn)

Insert(A, key)
1
$\;\;\;\;\;$HeapSize(A) = HeapSize(A) + 1
2
$\;\;\;\;\;$i = HeapSize(A)
3
$\;\;\;\;\;$while i > 1 and A[Parent(i)] < key
4
$\;\;\;\;\;$ $\;\;\;\;\;$A[i] = A[Parent(i)]
5
$\;\;\;\;\;$ $\;\;\;\;\;$i = Parent(i)
6
$\;\;\;\;\;$A[i] = key



\psfig{figure=figures/f3-9.ps,width=4in,height=1.5in}

Priority Queue Example


next up previous
Next: Up: Previous: