#include <math.h>
#include <curses.h>
void print_tree(struct node * root){
initscr() ;
clear();
recur_print(root, 0, COLS/2);
move(LINES-1, COLS-1) ;
refresh() ;
getchar() ;
endwin() ;
}
static void recur_print( struct node * node, int line, int col ){
if( node == NULL ){
move( line, col ) ;
addstr("*");//此处为加*号的代码,用来占位,可去掉。
return ;
}
int t = COLS/pow( 2, line+2 );
char buf[9];
sprintf(buf,"%-d", node->data ) ;
move( line , col ) ;
addstr( buf ) ;
if( node->left != NULL || node->right != NULL ){
move(line+1, col-t-1 ) ;
addstr("[");
move(line+1, col+t+3) ;
addstr("]");
}
recur_print( node->left, line+1, col-t ) ;
recur_print( node->right, line+1, col+t ) ;
}
|