// sanityCheck() // // This method makes sure that the HeapItems in H[] are heap ordered. // It also checks some other heap properties. // It does not check all heap properties (e.g., that items are filled // level by level). // public void sanityCheck() { boolean bad = false ; int left, right ; for (int i = 1 ; i <= maxSize ; i++) { left = 2 * i ; right = 2 * i + 1 ; if ( H[i].tag == EMPTY ) { // an empty node should not have non-empty children if ( left <= maxSize && H[left].tag != EMPTY ) { bad = true ; System.out.println("*** Non-empty left child of empty node at index: " + i) ; } if ( right <= maxSize && H[right].tag != EMPTY ) { bad = true ; System.out.println("*** Non-empty right child of empty node at index: " + i) ; } } else { // check that left child is smaller than parent if ( left <= maxSize && (H[left].tag != EMPTY) && (H[left].priority > H[i].priority) ) { bad = true ; System.out.println("*** Left child bigger at index: " + i) ; } // check that right child is smaller than parent if ( right <= maxSize && (H[right].tag != EMPTY) && (H[right].priority > H[i].priority) ) { bad = true ; System.out.println("*** Right child bigger at index: " + i) ; } // if there's no left child, there shouldn't be a right child if ( left <= maxSize && right <= maxSize && H[left].tag == EMPTY && H[right].tag != EMPTY ) { bad = true ; System.out.println("*** Left child empty but right child non-empty at index: " + i) ; } } } // end of for loop if (!bad) { System.out.println("Heap passes sanity check!!!") ; } }