1 //BinaryHeap class2 //
3 //CONSTRUCTION: with optional capacity (that defaults to 100)4 //or an array containing initial items5 //
6 //******************PUBLIC OPERATIONS*********************7 //void insert( x ) --> Insert x8 //Comparable deleteMin( )--> Return and remove smallest item9 //Comparable findMin( ) --> Return smallest item10 //boolean isEmpty( ) --> Return true if empty; else false11 //void makeEmpty( ) --> Remove all items12 //******************ERRORS********************************13 //Throws UnderflowException as appropriate
14
15 /**
16 * Implements a binary heap.17 * Note that all "matching" is based on the compareTo method.18 *@authorMark Allen Weiss19 */
20 public class BinaryHeap>
21 {22 /**
23 * Construct the binary heap.24 */
25 publicBinaryHeap( )26 {27 this( DEFAULT_CAPACITY );28 }29
30 /**
31 * Construct the binary heap.32 *@paramcapacity the capacity of the binary heap.33 */
34 public BinaryHeap( intcapacity )35 {36 currentSize = 0;37 array = (AnyType[]) new Comparable[ capacity + 1];38 }39
40 /**
41 * Construct the binary heap given an array of items.42 */
43 publicBinaryHeap( AnyType [ ] items )44 {45 currentSize =items.length;46 array = (AnyType[]) new Comparable[ ( currentSize + 2 ) * 11 / 10];47
48 int i = 1;49 for( AnyType item : items )50 array[ i++ ] =item;51 buildHeap( );52 }53
54 /**
55 * Insert into the priority queue, maintaining heap order.56 * Duplicates are allowed.57 *@paramx the item to insert.58 */
59 public voidinsert( AnyType x )60 {61 if( currentSize == array.length - 1)62 enlargeArray( array.length * 2 + 1);63
64 //Percolate up
65 int hole = ++currentSize;66 for( array[ 0 ] = x; x.compareTo( array[ hole / 2 ] ) < 0; hole /= 2)67 array[ hole ] = array[ hole / 2];68 array[ hole ] =x;69 }70
71
72 private void enlargeArray( intnewSize )73 {74 AnyType [] old =array;75 array = (AnyType []) newComparable[ newSize ];76 for( int i = 0; i < old.length; i++)77 array[ i ] =old[ i ];78 }79
80 /**
81 * Find the smallest item in the priority queue.82 *@returnthe smallest item, or throw an UnderflowException if empty.83 */
84 publicAnyType findMin( )85 {86 if( isEmpty( ) )87 throw newUnderflowException( );88 return array[ 1];89 }90
91 /**
92 * Remove the smallest item from the priority queue.93 *@returnthe smallest item, or throw an UnderflowException if empty.94 */
95 publicAnyType deleteMin( )96 {97 if( isEmpty( ) )98 throw newUnderflowException( );99
100 AnyType minItem =findMin( );101 array[ 1 ] = array[ currentSize--];102 percolateDown( 1);103
104 returnminItem;105 }106
107 /**
108 * Establish heap order property from an arbitrary109 * arrangement of items. Runs in linear time.110 */
111 private voidbuildHeap( )112 {113 for( int i = currentSize / 2; i > 0; i--)114 percolateDown( i );115 }116
117 /**
118 * Test if the priority queue is logically empty.119 *@returntrue if empty, false otherwise.120 */
121 public booleanisEmpty( )122 {123 return currentSize == 0;124 }125
126 /**
127 * Make the priority queue logically empty.128 */
129 public voidmakeEmpty( )130 {131 currentSize = 0;132 }133
134 private static final int DEFAULT_CAPACITY = 10;135
136 private int currentSize; //Number of elements in heap
137 private AnyType [ ] array; //The heap array
138
139 /**
140 * Internal method to percolate down in the heap.141 *@paramhole the index at which the percolate begins.142 */
143 private void percolateDown( inthole )144 {145 intchild;146 AnyType tmp =array[ hole ];147
148 for( ; hole * 2 <= currentSize; hole =child )149 {150 child = hole * 2;151 if( child != currentSize &&
152 array[ child + 1 ].compareTo( array[ child ] ) < 0)153 child++;154 if( array[ child ].compareTo( tmp ) < 0)155 array[ hole ] =array[ child ];156 else
157 break;158 }159 array[ hole ] =tmp;160 }161
162 //Test program
163 public static voidmain( String [ ] args )164 {165 int numItems = 10000;166 BinaryHeap h = new BinaryHeap<>( );167 int i = 37;168
169 for( i = 37; i != 0; i = ( i + 37 ) %numItems )170 h.insert( i );171 for( i = 1; i < numItems; i++)172 if( h.deleteMin( ) !=i )173 System.out.println( "Oops! " +i );174 }175 }