next up previous
Up: CSE 2320: Algorithms and Previous: Application: Job Scheduling

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=7in,height=3in}

Priority Queue Example



Copyright © 1998 The University of Texas at Arlington