Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
9Activity
0 of .
Results for:
No results containing your search query
P. 1
Heap java

Heap java

Ratings:

4.86

(7)
|Views: 3,339 |Likes:
Published by aravind.arjun
A rough implementation of priority queue in Java.
A rough implementation of priority queue in Java.

More info:

Published by: aravind.arjun on Oct 19, 2007
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

11/07/2012

pdf

text

original

 
/*Implementation of priority Queue.Methods include :add()Top()removeTop()isEmpty()printHeap();Author : Aravind Ragipindi.*/import java.util.ArrayList;import java.util.Random;class Heap{private ArrayList myHeap;public Heap(){myHeap = new ArrayList();}/*adds the given element to the priority queue.*/public void add(Comparable obj){myHeap.add(obj);Comparable tmpObj,lastObj = obj;int heapLastIndex = myHeap.size()-1;int lastParent = getParentIndex(heapLastIndex);int i,j = heapLastIndex;for(i = lastParent ; i>=0 && i != j ; i = getParentIndex(i)){if(lastObj.compareTo(myHeap.get(i))>0){tmpObj = (Comparable)myHeap.get(i);myHeap.set(i,lastObj);myHeap.set(j,tmpObj);j=i;}else{break;}}}/*returns the parent node's index for any given index.We could check for negative values here before rerturning.*/private int getParentIndex(int index){return (index%2 == 0) ? ( index-2)/2 : (index-1)/2;}/*printHeap()This is for debugging purposes.*/
 
public void printHeap(){System.out.println(" Elements in Queue are : ");for(int i=0;i<myHeap.size();i++){System.out.print(" "+myHeap.get(i));}System.out.println();}/*removeTop();removes and returns the top Node.*/public Comparable removeTop(){Comparable retObj = null;if(!myHeap.isEmpty()){retObj = (Comparable)myHeap.get(0);Comparable lastObj = (Comparable)myHeap.remove(myHeap.size()-1);if(!myHeap.isEmpty()){myHeap.set(0,lastObj);}int i = 0;while(i< myHeap.size()){int maxIndex = getMaxIndex(2*i+1 ,2*i+2);if( maxIndex > 0 && lastObj.compareTo(myHeap.get(maxIndex)) < 0){Comparable tmpObj = (Comparable)myHeap.get(maxIndex);myHeap.set(maxIndex,lastObj);myHeap.set(i,tmpObj);i = maxIndex;continue;}break;}}return retObj;}/*just returns the root value or the highest valued nodein the priority queue.*/public Comparable Top(){if(!myHeap.isEmpty())return (Comparable)myHeap.get(0);return null;}public boolean isEmpty(){return myHeap.isEmpty();}/*private :returns the index value for which the array is holding a highervalue among the two given indices.*/

Activity (9)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Luís Ribeiro liked this
Iftikhar Ansari liked this
nirmalrajj liked this
Hafsi50 liked this
Roger Makram liked this
macromohamed liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->