我上软件课程设计,要一个二叉树的设计流程图和源程序,求各路高手帮帮忙啊,我只有100分了,全给你们了,

2024-12-25 20:23:21
推荐回答(1个)
回答1:

#include "stdio.h"
#include "string.h"
#include "malloc.h"

typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTree Create(BiTree T){
char ch;
ch=getchar();
if(ch=='#')
T=NULL;
else{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
printf("Error!");
T->data=ch;
T->lchild=Create(T->lchild);
T->rchild=Create(T->rchild);
}
return T;
}
void Preorder(BiTree T){
if(T){
printf("%c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}

void zhongxu(BiTree T){
if(T){
zhongxu(T->lchild);
printf("%c",T->data);
zhongxu(T->rchild);
}
}
void houxu(BiTree T){
if(T){
houxu(T->lchild);
houxu(T->rchild);
printf("%c",T->data);
}
}

main(){
BiTree T;

printf("建立树: ");
T=Create(T);
printf("\n先序遍历: ");
Preorder(T);
printf("\n中序遍历: ");
zhongxu(T);
printf("\n后序遍历: ");
houxu(T);

getchar();
}