主题:二叉树的建立及遍历!!!!!
#define NULL 0;
typedef char elemtype;
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
void creatree(bitree *s)
{
char xt;
bitree t;
scanf("%c",&xt);
if(xt==' ') *s=NULL
else
{
t=(bitnode*)malloc(sizeof(bitnode));
if (!t) printf("\n超过空间\n");
t->data=xt;
*s=t;
creatree(&((*s)->lchild));
creatree(&((*s)->rchild));
}
return;
}
void xian(bitree s)
{
if(s)
{
printf("%c->",s->data);
xian(s->lchild);
xian(s->rchild);
}
return;
}
void zhong(bitree s)
{
if(s)
{
zhong(s->lchild);
printf("%c->",s->data);
zhong(s->rchild);
}
return;
}
void hou(bitree s)
{
if(s)
{
hou(s->lchild);
hou(s->rchild);
printf("%c->",s->data);
}
return;
}
void main()
{
bitree s;
clrscr();
printf("\n请按先序方式输入数据!\n");
creatree(&s);
printf("\n按先序方式输出:\n");
xian(s);
printf("\b\n");
printf("\n按中序方式输出:\n");
zhong(s);
printf("\b\n");
printf("\n按后序方式输出:\n");
hou(s);
printf("\b\n");
clrscr();
getch();
return;
}
typedef char elemtype;
typedef struct bitnode
{
char data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
void creatree(bitree *s)
{
char xt;
bitree t;
scanf("%c",&xt);
if(xt==' ') *s=NULL
else
{
t=(bitnode*)malloc(sizeof(bitnode));
if (!t) printf("\n超过空间\n");
t->data=xt;
*s=t;
creatree(&((*s)->lchild));
creatree(&((*s)->rchild));
}
return;
}
void xian(bitree s)
{
if(s)
{
printf("%c->",s->data);
xian(s->lchild);
xian(s->rchild);
}
return;
}
void zhong(bitree s)
{
if(s)
{
zhong(s->lchild);
printf("%c->",s->data);
zhong(s->rchild);
}
return;
}
void hou(bitree s)
{
if(s)
{
hou(s->lchild);
hou(s->rchild);
printf("%c->",s->data);
}
return;
}
void main()
{
bitree s;
clrscr();
printf("\n请按先序方式输入数据!\n");
creatree(&s);
printf("\n按先序方式输出:\n");
xian(s);
printf("\b\n");
printf("\n按中序方式输出:\n");
zhong(s);
printf("\b\n");
printf("\n按后序方式输出:\n");
hou(s);
printf("\b\n");
clrscr();
getch();
return;
}