二叉树-链式存储的遍历

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define Status void
#define Teletype char
#define NULL 0
typedef struct bitnode
{
 Teletype data;
 struct bitnode *rchild;
 struct bitnode *lchild;
}bitnode,*bitree;
bitree jj;
bitree *bin=&jj;


/*构造二叉树链表表示的二叉树的函数-以先根序方式构造*/
Status createbitree(bitree *b)
{
  char ch;
  fflush(stdin); /*清除键盘缓冲区*/
  scanf("%c",&ch);
  if(ch==' '){*b=NULL;return;}
  else{
   *b=(bitree)malloc(sizeof(bitnode));
   if(!*b)exit(1);
   (*b)->data=ch;
   createbitree(&((*b)->lchild));
   createbitree(&((*b)->rchild));
  }
}

/*二叉树的先根序遍历函数*/
Status pretra(bitree *b)
{
 if(*b==NULL)return;
 else{
  printf("%c ",(*b)->data);
  pretra(&((*b)->lchild));
  pretra(&((*b)->rchild));
 }
}

/*二叉树的中根序遍历函数*/
Status intra(bitree *b)
{
 if(*b==NULL)return;
 else{
  intra(&((*b)->lchild));
  printf("%c ",(*b)->data);
  intra(&((*b)->rchild));
 }
}

/*二叉树的后根序遍历函数*/
Status posttra(bitree *b)
{
 if(*b==NULL)return;
 else{
  posttra(&((*b)->lchild));
  posttra(&((*b)->rchild));
  printf("%c ",(*b)->data);
 }
}


void main()
{
printf("按先根序输入二叉树(空格表示空树):/n");
createbitree(bin);
printf("/n按先根序输出二叉树:/n");
pretra(bin);
printf("/n按中根序输出二叉树:/n");
intra(bin);
printf("/n按后根序输出二叉树:/n");
posttra(bin);
printf("/n");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值