max heap - Pascal Max_Heapify

I did these in pascal.Proceure Max_Heapify(a:Table;i:longint);var tmp,l,r,k:longint;begin l:=2*i;r:=2*i+1;if (heapsize>=l)and(a[i]<a[l]) then k:=l else k:=i;if (heapsize>=r)and(a[i]<a[r]) then k:=r;if k<>i then begin swap(a[i],a[k]); Max_Heapify(a,k); end;end;I wrote this procedure in pascal, but it seems not working and I can't figure out what's wrong can somebody help me ? Thanks...Read more

Merge 2 Max heaps

I need to find the most efficient algorithm to merge 2 max-heaps.Some important facts: The heaps are represented as a binary trees, that means that each node has 3 fields - value (key), pointer to right child, and pointer to left child.My idea: to take the last leaf of the second heap and to place him as the root of the new heap. So we get a new heap, when the left child is a legal max heap, and the right child is a legal max heap. The problem (to my opinion) is only the fact that the root is not the maximal element - So we can run the function...Read more