Java Min Heap Priority Queue Implementation -
i'm trying implement min heap pq, i'm having issues regarding correctness of implementation , can't seem figure out i'm doing wrong - doesn't output lowest priority or sort them properly.
public void additem(vertex item) { if (queuesize == queuearray.length) { vertex[] queuearray2 = new vertex[queuesize*2]; system.arraycopy(queuearray, 0, queuearray2, 0, queuesize); queuearray = queuearray2; } if (queuesize == 0) { queuearray[queuesize] = item; // insert @ 0 queuesize++; } else { int index=queuesize; //vertex newnode = new vertex(item, priority); queuearray[index] = item; queuesize++; int parent=(index-1)/2; while (index!=0 && queuearray[index].getf()<queuearray[parent].getf()) { // swap parent , index items vertex temp = queuearray[parent]; queuearray[parent] = queuearray[index]; queuearray[index] = temp; index=parent; parent=(index-1)/2; } } } public vertex getnextitem() { if (queuesize == 0) { return null; } vertex temp = queuearray[0]; --queuesize; if (queuesize > 0) { queuearray[0] = queuearray[queuesize]; swapnodes(0); } queuearray[queuesize] = null; return temp; } public void swapnodes(int root) { int child; if ((2*root+1) >= queuesize) { child = root; //no children } else if ((2*root)+2 == queuesize) { child = (2*root)+1; } else if (queuearray[(2*root)+1].getf()< queuearray[(2*root)+2].getf()) { child = (2*root)+1; //left child } else { child = (2*root)+2; //right child } //swap nodes around if (queuearray[root].getf() < queuearray[child].getf()) { vertex temp = queuearray[root]; queuearray[root] = queuearray[child]; queuearray[child] = temp; swapnodes(child); } }
using following test data:
data1.setf(71); data2.setf(19); data3.setf(65); data4.setf(16); data5.setf(14); data6.setf(8); data7.setf(10); data8.setf(36); data9.setf(543); test.additem(data1); test.additem(data2); test.additem(data3); test.additem(data4); test.additem(data5); test.additem(data6); test.additem(data7); test.additem(data8); test.additem(data9);
i following results:
array data: 8.0 array data: 16.0 array data: 10.0 array data: 36.0 array data: 19.0 array data: 65.0 array data: 14.0 array data: 71.0 array data: 543.0 pq data: 8.0 pq data: 543.0 pq data: 71.0 pq data: 14.0 pq data: 65.0 pq data: 19.0 pq data: 36.0 pq data: 16.0 pq data: 10.0
i'm expecting results in ascending order - @ first thought may due wrong children being swapped last output greatest priority didn't make sense. i've spent few hours trying research heap priority queues can't find help.
edit:
here better output of code asked cmps (i think asked for)
array data: 8.0 array data: 16.0 array data: 10.0 array data: 36.0 array data: 19.0 array data: 65.0 array data: 14.0 array data: 71.0 array data: 543.0 pq getnextitem: 8.0 array data: 543.0 array data: 16.0 array data: 10.0 array data: 36.0 array data: 19.0 array data: 65.0 array data: 14.0 array data: 71.0 pq getnextitem: 543.0 array data: 71.0 array data: 16.0 array data: 10.0 array data: 36.0 array data: 19.0 array data: 65.0 array data: 14.0 pq getnextitem: 71.0 array data: 14.0 array data: 16.0 array data: 10.0 array data: 36.0 array data: 19.0 array data: 65.0 pq getnextitem: 14.0 array data: 65.0 array data: 16.0 array data: 10.0 array data: 36.0 array data: 19.0 pq getnextitem: 65.0 array data: 19.0 array data: 16.0 array data: 10.0 array data: 36.0 pq getnextitem: 19.0 array data: 36.0 array data: 16.0 array data: 10.0 pq getnextitem: 36.0 array data: 16.0 array data: 10.0 pq getnextitem: 16.0 array data: 10.0 pq getnextitem: 10.0
when bubble down in heap after removing node, need have minimum @ top, in following code, swapping if minimum in top, should opposite way.
change:
if (queuearray[root].getf() < queuearray[child].getf()) { vertex temp = queuearray[root]; queuearray[root] = queuearray[child]; queuearray[child] = temp; swapnodes(child); }
to:
if (queuearray[root].getf() > queuearray[child].getf()) { vertex temp = queuearray[root]; queuearray[root] = queuearray[child]; queuearray[child] = temp; swapnodes(child); }
Comments
Post a Comment