0001 #define ROOT 0 0002 #define L_CHILD 1 0003 #define R_CHILD -1*L_CHILD 0004 0005 /****************************************************************************************** 0006 * 打印输出PQ_ComplHeap 0007 ******************************************************************************************/ 0008 template <typename T> //元素类型 0009 void UniPrint::p ( PQ_ComplHeap<T> & pq ) { //引用 0010 printf ( "%s[%d]*%d:\n", typeid ( pq ).name(), &pq, pq.size() ); //基本信息 0011 int branchType[256]; //最深256层 <= 2^256 = 1.16*10^77 0012 printComplHeap ( ( Vector<T> & ) pq, pq.size(), 0, 0, ROOT, branchType ); //树状结构 0013 printf ( "\n" ); 0014 } 0015 0016 /****************************************************************************************** 0017 * 递归打印输出 0018 ******************************************************************************************/ 0019 template <typename T> //元素类型 0020 static void printComplHeap ( Vector<T>& elem, int n, int k, int depth, int type, int* bType ) { 0021 if ( k >= n ) return; //递归基 0022 bType[depth] = type; 0023 printComplHeap ( elem, n, RChild ( k ), depth + 1, R_CHILD, bType ); //右子树(在上) 0024 print ( elem[k] ); ParentValid ( k ) && ( elem[Parent ( k ) ] < elem[k] ) ? printf ( "X" ) : printf ( " " ); printf ( "*" ); 0025 for ( int i = 0; i < depth; i++ ) //根据相邻各层 0026 if ( bType[i] + bType[i+1] ) //的拐向是否一致,即可确定 0027 printf ( " " ); //是否应该 0028 else printf ( "│ " ); //打印横线 0029 switch ( type ) { 0030 case R_CHILD : printf ( "┌─" ); break; 0031 case L_CHILD : printf ( "└─" ); break; 0032 default : printf ( "──" ); break; //root 0033 } 0034 print ( elem[k] ); ParentValid ( k ) && ( elem[Parent ( k ) ] < elem[k] ) ? printf ( "X" ) : printf ( " " ); printf ( "\n" ); 0035 printComplHeap ( elem, n, LChild ( k ), depth + 1, L_CHILD, bType ); //左子树(在下) 0036 }