Wednesday, June 18, 2008

Heap Insert

Insert (H, k)

The above procedure inserts an element with key value ‘k’ in a given maximum heap. The heap-size in incremented by 1 after the insertion of the element with key ‘k’.
Step 1 Incrementing the array size, assuming size does not exceed the maximum array size.
set heap-size [H]  heap-size [H] + 1
Step 2 initialization
set i  heap-size [H]
Step 3 loop, obtaining proper position
while (i > 1 and H [Parent (i)] < k
set H [i]  H [Parent (i)]
set i  Parent (i)
Step 4 Insertion
set H [i]  k
Step 5 return at the point of call
return

No comments: