//-------------------------------------------------------------------- // // Laboratory B showb.cpp // // Array implementation of the showStructure operation for the // Heap ADT // //-------------------------------------------------------------------- template < class HD > void Heap:: showStructure () const // Outputs the priorities of the data in a heap in both array // and tree form. If the heap is empty, outputs "Empty heap". This // operation is intended for testing/debugging purposes only. { int j; // Loop counter cout << endl; if ( size == 0 ) cout << "Empty heap" << endl; else { cout << "size = " << size << endl; // Output array form for ( j = 0 ; j < maxSize ; j++ ) cout << j << "\t"; cout << endl; for ( j = 0 ; j < size ; j++ ) cout << dataItems[j].pty() << "\t"; cout << endl << endl; showSubtree(0,0); // Output tree form } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - template < class DT > void Heap
:: showSubtree ( int index, int level ) const // Recursive partner of the showStructure() function. Outputs the // subtree (subheap) whose root is stored in dataItems[index]. Argument // level is the level of this dataItems within the tree. { int j; // Loop counter if ( index < size ) { showSubtree(2*index+2,level+1); // Output right subtree for ( j = 0 ; j < level ; j++ ) // Tab over to level cout << "\t"; cout << " " << dataItems[index].pty(); // Output dataItems's pty if ( 2*index+2 < size ) // Output "connector" cout << "<"; else if ( 2*index+1 < size ) cout << "\\"; cout << endl; showSubtree(2*index+1,level+1); // Output left subtree } }