C语言打印二叉树图形,c语言打印二叉树树形结构(转载自:http://blog.csdn.net/xzongyuan)...

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值