Write in pseudocode the algorithm to insert an element into

Write in pseudocode the algorithm to insert an element into a heap.

If value > root
set root to value
pull down root
repeat until forming heap again
if value < root
pull down until it finds its position

Solution

void insert(double x){

a[nodes+1]=x;

nodes++;

upheap(nodes);

}

upheap(k){

while(k has parent node){

parent=k/2; a[parent] is the parent of a[k]

if(a[k]<a[parent]){

swap a[k] <--> a[parent];

k=parent;

}

else

break; // correct position of a[k]

}

Write in pseudocode the algorithm to insert an element into a heap. If value > root set root to value pull down root repeat until forming heap again if value

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site