学号:E30814013
姓名:张芸;
专业:网络工程
题目::实现二叉树的前序,中序,后序遍历
#include
#include
#include
typedef struct Btnode{
char data;
struct Btnode *lchild;
struct Btnode *rchild;
}Btnode;
void main()
{
void xianxu(Btnode *BT);
void zhongxu(Btnode *BT);
void houxu(Btnode *BT);
Btnode *CreatBiTree(Btnode *BT);
Btnode *BT;
Btnode *B;
BT=(Btnode*)malloc(sizeof(Btnode));
printf("先序输入二叉树中节点的值(一个字符,先序输入),空格表示空树\n");
B=BT=CreatBiTree(BT);
printf("输出二叉树(二叉树的遍历)\n");
printf("先序\n");
xianxu(BT);
printf("\n");
printf("中序\n");
zhongxu(BT);
printf("\n");
printf("后序\n");
houxu(BT);
printf("\n");
}
Btnode *CreatBiTree(Btnode *BT)
{char ch;
scanf("%c",&ch);
if(ch==' ')BT=NULL;
else{
BT=(Btnode*)malloc(sizeof(Btnode));
BT->data=ch;
BT->lchild=CreatBiTree(BT->lchild);
BT->rchild=CreatBiTree(BT->rchild);
}
return BT;
}
void xianxu(Btnode *BT)
{
if(BT){
printf("%c",BT->data);
xianxu(BT->lchild);
xianxu(BT->rchild);
}
}
void zhongxu(Btnode *BT)
{
if(BT){
zhongxu(BT->lchild);
printf("%c",BT->data);
zhongxu(BT->rchild);
}
}
void houxu(Btnode *BT)
{
if(BT){
houxu(BT->lchild);
houxu(BT->rchild);
printf("%c",BT->data);
}
}
本文来源:https://www.2haoxitong.net/k/doc/25ff40284b73f242336c5f87.html
文档为doc格式