用c实现如下的二叉树
![](https://img-my.csdn.net/uploads/201210/24/1351079750_7455.png)
#include "stdio.h"
#include "malloc.h"
struct btree
{
char data;
struct btree *lp;
struct btree *rp;
};
struct btree * init();
void pretraverse(struct btree *);
void intraverse(struct btree *);
void postraverse(struct btree *);
int main()
{
struct btree * p=init();
printf("前序遍历的结果为:\n");
pretraverse(p);
printf("中序遍历的结果为:\n");
intraverse(p);
printf("后序遍历的结果为:\n");
postraverse(p);
return 0;
}
struct btree * init()
{
struct btree *a=(struct btree *)malloc(sizeof(struct btree));
struct btree *b=(struct btree *)malloc(sizeof(struct btree));
struct btree *c=(struct btree *)malloc(sizeof(struct btree));
struct btree *d=(struct btree *)malloc(sizeof(struct btree));
struct btree *e=(struct btree *)malloc(sizeof(struct btree));
a->data='a';
b->data='b';
c->data='c';
d->data='d';
e->data='e';
a->lp=b;
b->lp=NULL;
b->rp=NULL;
a->rp=c;
c->lp=d;
d->lp=NULL;
d->rp=NULL;
c->rp=e;
e->lp=NULL;
e->rp=NULL;
return a;
}
void pretraverse(struct btree *p)
{
printf("%c\t",p->data);
if(NULL!=p->lp)
{
pretraverse(p->lp);
}
if(NULL!=p->rp)
{
pretraverse(p->rp);
}
}
void intraverse(struct btree *p)
{
if(NULL!=p->lp)
{
intraverse(p->lp);
}
printf("%c\t",p->data);
if(NULL!=p->rp)
{
intraverse(p->rp);
}
}
void postraverse(struct btree *p)
{
if(NULL!=p->lp)
{
postraverse(p->lp);
}
if(NULL!=p->rp)
{
postraverse(p->rp);
}
printf("%c\t",p->data);
}