first commit
This commit is contained in:
175
CS1322/t5/Heap.java
Normal file
175
CS1322/t5/Heap.java
Normal file
@@ -0,0 +1,175 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Heap.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 27, 2002
|
||||
* Created the Heap class
|
||||
* 1.1 Oct. 27, 2002
|
||||
* Compiled, Finished
|
||||
* 1.2 Oct. 27, 2002
|
||||
* Commented
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.2, Oct. 27, 2002
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Heap {
|
||||
|
||||
private Vector data;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Default Constructor for Heap
|
||||
*@done sets the first element to null
|
||||
*/
|
||||
public Heap(){
|
||||
data = new Vector();
|
||||
data.add(null);
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Swaps the elements at the given locations
|
||||
*@param a and b, the elements to be swapped
|
||||
*/
|
||||
private void swap(int a,int b){
|
||||
Comparable temp = (Comparable)data.elementAt(a);
|
||||
data.setElementAt(data.elementAt(b),a);
|
||||
data.setElementAt(temp,b);
|
||||
}
|
||||
|
||||
/**
|
||||
*Insert a piece of data into the Heap
|
||||
*@param toAdd, the data to be added
|
||||
*/
|
||||
public void insert(Comparable toAdd){//i changed this to toAdd because
|
||||
//the bright guy who decided to make us
|
||||
//use this.data.INSERTHERE() is a sadist
|
||||
int i = data.size();
|
||||
data.setElementAt(new Integer(i),0);
|
||||
data.add(toAdd);
|
||||
|
||||
while(i>1 &&
|
||||
((Comparable)data.elementAt(i)).compareTo(data.elementAt(i/2))>0){
|
||||
swap(i,(i/2));
|
||||
i /= 2;
|
||||
}
|
||||
}//end insert(Comparable)
|
||||
|
||||
/**
|
||||
*Arranges heap to be a heap
|
||||
*@param node, the node to be tested for heapitude
|
||||
*/
|
||||
private void heapify(int node){
|
||||
int iLeft = (node*2),iRight = ((node*2)+1),iMin = iLeft;
|
||||
int iSize = data.size()-1;
|
||||
|
||||
if(iLeft <= iSize &&
|
||||
((Comparable)data.elementAt(iLeft)).compareTo(data.elementAt(node))<0){
|
||||
iMin = iLeft;
|
||||
}
|
||||
else{
|
||||
iMin = node;
|
||||
}
|
||||
|
||||
if(iRight <= iSize &&
|
||||
((Comparable)data.elementAt(iRight)).compareTo(data.elementAt(iMin))<0){
|
||||
iMin = iRight;
|
||||
}
|
||||
|
||||
if(iMin != node){
|
||||
swap(iMin,node);
|
||||
heapify(iMin);
|
||||
}
|
||||
}//end heapify(int)
|
||||
|
||||
/**
|
||||
*Removes the largest piece of data from the heap
|
||||
*/
|
||||
public Comparable remove(){
|
||||
Comparable removed=null;
|
||||
|
||||
if(data.size()-1 > 0) {
|
||||
removed = (Comparable)data.elementAt(1);
|
||||
data.setElementAt(data.elementAt(data.size()-1), 1);
|
||||
|
||||
//reduce size
|
||||
Integer temp = (Integer)data.elementAt(0);
|
||||
temp = new Integer(temp.intValue()-1);
|
||||
data.setElementAt(temp,0);
|
||||
data.remove(temp.intValue()+1);
|
||||
heapify(1);
|
||||
}
|
||||
|
||||
|
||||
return removed;
|
||||
}//end remove()
|
||||
|
||||
/**
|
||||
*@return true if the heap is empty, false otherwise
|
||||
*/
|
||||
public boolean isEmpty(){
|
||||
if(data.size()>1){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*String representation of the Heap
|
||||
*/
|
||||
public String toString(){
|
||||
return data.toString();
|
||||
}
|
||||
|
||||
/***************************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class Heap.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Heap heap = new Heap();
|
||||
System.out.println(heap.isEmpty());
|
||||
|
||||
heap.insert(new Integer(5));
|
||||
System.out.println(heap.isEmpty());
|
||||
heap.insert(new Integer(21));
|
||||
heap.insert(new Integer(1));
|
||||
heap.insert(new Integer(322));
|
||||
heap.insert(new Integer(12));
|
||||
heap.insert(new Integer(3252));
|
||||
heap.insert(new Integer(0));
|
||||
heap.insert(new Integer(223));
|
||||
System.out.println(heap.data);
|
||||
|
||||
heap.remove();
|
||||
System.out.println(heap.data);
|
||||
heap.remove();
|
||||
System.out.println(heap.data);
|
||||
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Heap
|
||||
Reference in New Issue
Block a user