用C语言编:构造一棵二叉树的二叉链表,并输出此二叉树的中序遍历序列和后序遍历序列。数据元素为字符型

2024-11-25 04:20:19
推荐回答(2个)
回答1:

你确定你的序列没写错?

回答2:

先序创建一颗二叉树遍历;
#include
#include
#include
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef int status;
typedef char TElemtype;
typedef struct BiTNode{
TElemtype data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
status Visit(TElemtype e)
{
printf(" %c ",e);
return OK;
}
void InoRtraverse(BiTree T,status(* Visit)(TElemtype)){
if(T){
InoRtraverse(T->lchild,Visit);
Visit(T->data);
InoRtraverse(T->rchild,Visit);

}
}
void PosTtraverse(BiTree T,status(* Visit)(TElemtype)){
if(T){
PosTtraverse(T->lchild,Visit);
PosTtraverse(T->rchild,Visit);
Visit(T->data);

}
}
status creatBiTree(BiTree *T){
char c;
scanf("%c",&c);
if(c==' ')
(*T)=NULL;
else{
if(!((*T)=(BiTNode *)malloc(sizeof(BiTNode))))
exit(OVERFLOW);
(*T)->data=c;
creatBiTree(&((*T)->lchild));
creatBiTree(&((*T)->rchild));
}
return OK;
}
void main(){
BiTree T;
creatBiTree(&T);
printf("\n按照中序排列输出\n");
InoRtraverse(T,Visit);
printf("\n按照后序排列输出\n");
PosTtraverse(T,Visit);
}