Main Page   Class Hierarchy   Compound List   File List   Compound Members  

MinHeap.java

00001 /*
00002  *  Copyright 2006 Columbia University.
00003  *
00004  *  This file is part of MEAPsoft.
00005  *
00006  *  MEAPsoft is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License version 2 as
00008  *  published by the Free Software Foundation.
00009  *
00010  *  MEAPsoft is distributed in the hope that it will be useful, but
00011  *  WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with MEAPsoft; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00018  *  02110-1301 USA
00019  *
00020  *  See the file "COPYING" for the text of the license.
00021  */
00022 
00023 package com.meapsoft;
00024 
00025 import java.util.Collection;
00026 import java.util.Comparator;
00027 import java.util.Iterator;
00028 import java.util.Vector;
00029 
00035 public class MinHeap extends Heap
00036 {
00037     public MinHeap()
00038     {
00039         super();
00040     }
00041 
00047     public MinHeap(Comparator c)
00048     {
00049         super(c);
00050     }
00051 
00052     public MinHeap(int capacity)
00053     {
00054         super(capacity);
00055     }
00056 
00057     public MinHeap(Collection c)
00058     {
00059         super(c);
00060     }
00061 
00062     public Object deleteMin()
00063     {
00064         return remove(0);
00065     }
00066 
00072     public void sort()
00073     {
00074         super.sort();
00075 
00076         // reverse the vector since we want to sort in ascending order
00077         for(int x = 0; x <= (size()-1)/2; x++)
00078         {
00079             Object tmp = get(x);
00080             set(x, get(size()-1-x));
00081             set(size()-1-x, tmp);
00082         }
00083         
00084         // this just so happens to maintain the min-heap property
00085         isHeap = true;
00086     }
00087 
00088     // Simple test program
00089     public static void main(String args[])
00090     {
00091         MinHeap h = new MinHeap();
00092         Vector v = new Vector();
00093 
00094         //int[] numbers = {10, 1, 6, -10, 2, 7, 6};
00095         int[] numbers = {10, 1, 6, -10, 2, 7, 4};
00096 
00097         try
00098         {
00099             System.out.print("Adding ");
00100             Double d = null;
00101             for(int x = 0; x < numbers.length; x++)
00102             {
00103                 System.out.print(numbers[x] + " ");
00104                 d = new Double(numbers[x]);
00105                 h.add(d);
00106                 v.add(d);
00107             }
00108 
00109             System.out.print("\nCalling deleteMin: ");
00110             for(int x = 0; x < numbers.length; x++)
00111                 System.out.print(h.deleteMin() + " ");
00112 
00113             System.out.print("\nRemoving an element (this should obey the heap property): ");
00114             h.addAll(v);
00115             h.remove(d);
00116             Iterator i = h.iterator();
00117             while(i.hasNext())
00118                 System.out.print(i.next() + " ");
00119 
00120             System.out.print("\nRunning heapSort: ");
00121             //h.addAll(v);
00122             h.sort();
00123             i = h.iterator();
00124             while(i.hasNext())
00125                 System.out.print(i.next() + " ");
00126             System.out.print("\n");
00127         }
00128         catch(Exception e)
00129         {
00130             e.printStackTrace();
00131         }
00132     }
00133 }

Generated on Thu May 11 15:04:11 2006 for MEAPsoft by doxygen1.2.18