/** * HeapPrinter prints out the contents of a heap in a rudimentary fashion. * @author Erik Steinmetz */ public class HeapPrinter { /** * Prints out the contents of the given integer heap. * * If invoked from outside of this class, call using the code * HeapPrinter.printHeap( someArray, sizeOfHeap); * * @param heapArray The heap to display. * @param heapSize The size of the heap. */ public static void printHeap( int[] heapArray, int heapSize) { int numberInThisRow = 1; int printedSoFarInThisRow = 0; for( int index = 1; index <= heapSize; index++) { System.out.print( heapArray[ index] + " "); if( heapArray[ index] < 100) System.out.print( " "); if( heapArray[ index] < 10) System.out.print( " "); printedSoFarInThisRow++; if( printedSoFarInThisRow == numberInThisRow) { // End of row. Print a carriage return, reset row numbers System.out.println(); numberInThisRow *= 2; printedSoFarInThisRow = 0; } } } /** * Prints a test heap. * @param args Main method arguments. */ public static void main( String[] args) { int[] test = { 0, 10, 3, 7, 6, 4, 9, 8}; int size = 7; printHeap( test, size); } }