Main Page   Class Hierarchy   Compound List   File List   Compound Members  

MaxHeap.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 
00030 // I can't believe that the huge Java API doesn't already include such
00031 // a basic data structure
00032 
00038 public class MaxHeap extends Heap
00039 {
00040     public MaxHeap()
00041     {
00042         super();
00043     }
00044 
00050     public MaxHeap(Comparator c)
00051     {
00052         super(c);
00053     }
00054 
00055     public MaxHeap(int capacity)
00056     {
00057         super(capacity);
00058     }
00059 
00060     public MaxHeap(Collection c)
00061     {
00062         super(c);
00063     }
00064 
00065     public Object deleteMax()
00066     {
00067         return remove(0);
00068     }
00069 
00074     protected int cmp(int node1, int node2)
00075     {
00076         return -super.cmp(node1, node2);
00077     }
00078 
00079     // Simple test program
00080     public static void main(String args[])
00081     {
00082         MaxHeap h = new MaxHeap();
00083         Vector v = new Vector();
00084 
00085         //int[] numbers = {10, 1, 6, -10, 2, 7, 6};
00086         int[] numbers = {10, 1, 6, -10, 2, 7, 4};
00087 
00088         try
00089         {
00090             System.out.print("Adding ");
00091             Double d = null;
00092             for(int x = 0; x < numbers.length; x++)
00093             {
00094                 System.out.print(numbers[x] + " ");
00095                 d = new Double(numbers[x]);
00096                 h.add(d);
00097                 v.add(d);
00098             }
00099 
00100             System.out.print("\nCalling deleteMax: ");
00101             for(int x = 0; x < numbers.length; x++)
00102                 System.out.print(h.deleteMax() + " ");
00103 
00104             System.out.print("\nRemoving an element (this should obey the heap property): ");
00105             h.addAll(v);
00106             h.remove(d);
00107             Iterator i = h.iterator();
00108             while(i.hasNext())
00109                 System.out.print(i.next() + " ");
00110 
00111             System.out.print("\nRunning heapSort: ");
00112             //h.addAll(v);
00113             h.sort();
00114             i = h.iterator();
00115             while(i.hasNext())
00116                 System.out.print(i.next() + " ");
00117             System.out.print("\n");
00118         }
00119         catch(Exception e)
00120         {
00121             e.printStackTrace();
00122         }
00123     }
00124 }

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