二叉树的树形打印输出问题

bingxingshengge 2011-12-13 10:08:36
用c语言编写,将二叉树以树形打印输出,横向的。我看到了一段竖向输出的代码,没怎么看懂,请高手指教。
int PrintTree(BiTree T,int nLayer)
{
//竖向输出二叉树
int i ;
if(T == NULL)
return false;
PrintTree(T->rchild,nLayer+3);
for(i = 0;i < nLayer; i++)
{
printf(" ");
}
printf("%c\n",T->data );
PrintTree(T->lchild,nLayer+3 );
return true;
}
如果横向树形打印输出二叉树的话,代码怎么写,求帮助。
...全文
787 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
AndyZhang 2011-12-13
  • 打赏
  • 举报
回复
这个网上一大堆哈
李少1991 2011-12-13
  • 打赏
  • 举报
回复
这个是横向的

#include<stdio.h>
#include<stdlib.h>
typedef struct node //二叉树结点定义
{
int data;
struct node *lchild,*rchild;
}bitnode,*bittree;

int creattree(bittree &t)//创建二叉树
{
char ch ;
scanf("%c",&ch);
if(ch=='#')
t=NULL;
else
{
t=(bittree)malloc(sizeof(node));
t->data=ch;
creattree(t->lchild);
creattree(t->rchild);
}
return 1;
}

void printbittree(bittree t,int n)// 二叉树的显示输出
{
int i;
char ch=' ';
if(t)
{
printbittree(t->lchild,n+1);
for(i=1;i<=n;++i)
{
printf("%5c",ch);
}
printf("%c\n",t->data);
printbittree(t->rchild,n+1);

}
}
void inorder(bittree t)//中序遍历二叉树
{
if(t!=NULL)
{
inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
}

void main()
{
bittree t;
printf("建立二叉树:\n");
creattree(t);
inorder(t);
printf("\n");
printf("\n");
printf("二叉树的形状显示输出:\n");
printbittree(t,0);
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧