00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00031
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
00080 public static void main(String args[])
00081 {
00082 MaxHeap h = new MaxHeap();
00083 Vector v = new Vector();
00084
00085
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
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 }