{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Binary heap(priority queue)

Binary heap(priority queue) - for;hole*2<=currentSize...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
public void insert(Anytype x ) { if(currentSize==array.length-1) enlargeArray(array.length*2-1); int hole = ++currentSize; for(;hole>1 & x.compareTo( array[hole/2] < 0 ); hole/=2) array[hole]=array[hole/2]; array[hole]=2 } public AnyType deleteMin(){ if( isEmpty() ) throw new UnderflowException(); AnyType minItem=array[0]; array[1]= array[currentSize--]; percolateDown(1); return minItem; } private void percolateDown(int hole) { int child; anyType tmp= array[hole];
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: for(;hole*2<=currentSize; hole = child) { child =hole*2; if( child!=currentSize && array[child+1].compareTo(array[child])<0 ) child++; if( array[child].compareTo[tmp]<0) array[hole]=array[child]; else break; } array[hole]=tmp; } // insert:o(1) delete worse case: log(n) //left child position 2i //right child position(2i+1) //parent position floor(i/2) //disadvantage: size should be defined first...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online