left);
rh = get_tree_height(pRoot->right);
return (lhdepth+1;
if(qNode->depth > current_depth)
{
current_depth = qNode->depth;
printf("%s\n",buf_branch);
sprintf(buf_branch,"\n"); //reset the buffer after print
}
// ***************0**************** pad_between = 31 ; pad_front = 15 (depth == 1)
// *******0***************0******** pad_between = 15 ; pad_front = 7 (depth == 2)
// ***0*******0*******0*******0**** pad_between = 7 ; pad_front = 3 (depth == 3)
// *0***0***0***0***0***0***0***0** pad_between = 3 ; pad_front = 1 (depth == 4)
// 0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0* pad_between = 1 ; pad_front = 0; (depth == 5)
// Tree height = 5
// pad_num = 1
// padding between node = (1+2*pad_front)*pad_num = (1+ (1<>1;
int pad_under = (pad_front+1)>>1;
//add the parent node's padding:2
if(pad_front == 0) printf("%.*s%*s",pad_num,leaf,pad_num," ");
else
{
printf("%*s%.*s%*s",pad_front*pad_num," ",pad_num,leaf,(1+pad_front)*pad_num," ");
char *new_buf=(char*)malloc(100);
char *old_buf = buf_branch;
sprintf(buf_branch,"%s%s",old_buf,new_buf);
}
if(child_depth<=height)
{
//enter two NULL sub-tree node.
//every time you enter NULL TNode,there's corresponding blank QNode.
enQueue(pQueue,NULL,child_depth);
enQueue(pQueue,NULL,child_depth);
}
}
else
{
if(pad_front == 0)
{
printf("%*d%*s",pad_num,qNode->t_node->value,pad_num," ");
}
else
{
char *new_buf=(char*)malloc(100);
char *old_buf=buf_branch;
printf("%*s%*d%*s",pad_front*pad_num," ",pad_num,qNode->t_node->value,(1+pad_front)*pad_num," ");
sprintf(buf_branch,"%s%s",old_buf,new_buf);
}
if(child_depth <=height)
{
enQueue(pQueue,qNode->t_node->left,child_depth);
enQueue(pQueue,qNode->t_node->right,child_depth);
}
}
} //while end
printf("\n-----------\nbreath end!\n-----------\n");
return 1;
}
int main(int argc,char **argv)
{
Queue *queue=init_queue();
int i;
ifEmpty(queue);
printf("insert node to queue\n");
int num = NUM; //default
if(argc == 2)
{
num = atoi(argv[1]);
}
Tree root = creatTree(num);
if(!root)
{
printf("create Tree failed!\n");
return 0;
}
breath_travel(root,queue);
return 0;
}