Binary heap(priority queue)

Binary heap(priority queue) -...

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&lt;=currentSize; hole = child) { child =hole*2; if( child!=currentSize &amp;&amp; array[child+1].compareTo(array[child])&lt;0 ) child++; if( array[child].compareTo[tmp]&lt;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

This note was uploaded on 02/18/2011 for the course 22C 031 taught by Professor Kasturi during the Spring '11 term at University of Iowa.

Ask a homework question - tutors are online