你自己看看吧
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
#define max 30
/*二叉树中做多的结点数*/
typedef struct btnode
/*定义二叉树的结点类型*/
{
char data;
/*结点的数据为字符型数据*/
struct btnode *lchild,*rchild; /*左右指针*/
}bttree;
bttree *cre_tree(char *str,int i,int m)
/*将字符串中的第i个字符到第m个字符作为数据生成对应的满二叉树*/
{
bttree *p;
if(i>=m)
/*无效结点*/
return NULL;
else
{
p=(bttree *)malloc(sizeof(bttree));
p->data=str[i];
p->lchild=cre_tree(str,2*i+1,m);
/*创建新结点的左子树*/
p->rchild=cre_tree(str,2*i+2,m);
/*创建结点的右子树*/
return p;
}
}
void preorder(bttree *t)
/*先序遍历二叉树*/
{
if(t!=NULL)
{
printf("%c",t->data);
if(t->lchild)
{
printf("->");
preorder(t->lchild);
}
if(t->rchild)
{
printf("->");
preorder(t->rchild);
}
}
}
void inorder(bttree *t)
/*中序遍历二叉树*/
{
if(t!=NULL)
{
inorder(t->lchild);
printf("%c",t->data);
printf("->");
inorder(t->rchild);
}
}
void postorder(bttree *t) /*后续遍历二叉树*/
{
if(t!=NULL)
{
postorder(t->lchild);
printf("%c",t->data);
printf("->");
inorder(t->rchild);
}
}
void main()
{
int i,n;
char str[max];
bttree *root;
/* *root为指向根结点的指针*/
printf("please input a bbtree node num:\n");
scanf("%d",&n);
getchar(); /*输入数字*/
printf("please input a string which length is %d:",n);
for(i=0;i<n;i++)
/*输入字符串*/
str[i]=getchar();
printf("\n");
root=cre_tree(str,0,n);
/*生成二叉树*/
printf("the tree is already created\n");
printf("\n");
printf("the result sfter preorder processing :");
/*先序遍历后输出结果*/
preorder(root);
printf("\n");
printf("the result sfter inorder processing :"); /*中序遍历后输出结果*/
inorder(root);
printf("\n");
printf("the result sfter postorder processing :");/*后续遍历后输出结果*/
postorder(root);
getch();
}