二叉树如图
输入:AB#D##CE###
前序遍历结果:ABDCE
中序遍历结果:BDAEC
后序遍历结果:DBECA
下面根据,创建函数的不同,分为三种类型
#include <stdio.h>
#include <stdlib.h>
/********************创建方法1:main中创建树指针,create_tree传入指针***************************/
#if 0
typedef struct bitnode{
int data;
struct bitnode *lchild;
struct bitnode *rchild;
}BITNODE, *BITREE;
BITREE create_tree(BITREE t)
{
char ch;
scanf("%c", &ch);
if(ch == '#')
t = NULL;
else{
if(!(t=(BITREE)malloc(sizeof(BITNODE))))
printf("error\r");
t->data = ch;
t->lchild = create_tree(t->lchild);
t->rchild = create_tree(t->rchild);
}
return t;
}
//前序遍历
void pre_order_traverse(BITREE t)
{
if(t)
{
printf("%c ", t->data);
pre_order_traverse(t->lchild);
pre_order_traverse(t->rchild);
}
}
//中序遍历
void in_order_traverse(BITREE t)
{
if(t)
{
in_order_traverse(t->lchild);
printf("%c ", t->data);
in_order_traverse(t->rchild);
}
}
//后序遍历
void post_order_traverse(BITREE t)
{
if(t)
{
post_order_traverse(t->lchild);
post_order_traverse(t->rchild);
printf("%c ", t->data);
}
}
int main()
{
BITREE t;
t = create_tree(t);
printf("\n前序遍历为\n");
pre_order_traverse(t);
printf("\n中序遍历为\n");
in_order_traverse(t);
printf("\n后序遍历为\n");
post_order_traverse(t);
printf("\n");
}
#endif
/********************创建方法2:main中创建树指针,create_tree传入指针的地址***************************/
#if 0
typedef struct bitnode{
int data;
struct bitnode *lchild;
struct bitnode *rchild;
}BITNODE, *BITREE;
void create_tree(BITREE *t)
{
char ch;
scanf("%c", &ch);
if(ch == '#')
return ;
else{
if(!(*t=(BITREE)malloc(sizeof(BITNODE))))
printf("error\r");
(*t)->data = ch;
create_tree(&((*t)->lchild));
create_tree(&((*t)->rchild));
}
return ;
}
//前序遍历
void pre_order_traverse(BITREE t)
{
if(t)
{
printf("%c ", t->data);
pre_order_traverse(t->lchild);
pre_order_traverse(t->rchild);
}
}
//中序遍历
void in_order_traverse(BITREE t)
{
if(t)
{
in_order_traverse(t->lchild);
printf("%c ", t->data);
in_order_traverse(t->rchild);
}
}
//后序遍历
void post_order_traverse(BITREE t)
{
if(t)
{
post_order_traverse(t->lchild);
post_order_traverse(t->rchild);
printf("%c ", t->data);
}
}
int main()
{
BITREE t;
create_tree(&t);
printf("\n前序遍历为\n");
pre_order_traverse(t);
printf("\n中序遍历为\n");
in_order_traverse(t);
printf("\n后序遍历为\n");
post_order_traverse(t);
printf("\n");
}
#endif
/********************创建方法3:main中创建树指针,create_tree不传参***************************/
typedef struct bitnode{
int data;
struct bitnode *lchild;
struct bitnode *rchild;
}BITNODE, *BITREE;
BITREE create_tree()
{
char ch;
BITREE t;
scanf("%c", &ch);
if(ch == '#')
return NULL;
else{
if(!(t=(BITREE)malloc(sizeof(BITNODE))))
printf("error\r");
t->data = ch;
t->lchild = create_tree();
t->rchild = create_tree();
}
return t;
}
//前序遍历
void pre_order_traverse(BITREE t)
{
if(t)
{
printf("%c ", t->data);
pre_order_traverse(t->lchild);
pre_order_traverse(t->rchild);
}
}
//中序遍历
void in_order_traverse(BITREE t)
{
if(t)
{
in_order_traverse(t->lchild);
printf("%c ", t->data);
in_order_traverse(t->rchild);
}
}
//后序遍历
void post_order_traverse(BITREE t)
{
if(t)
{
post_order_traverse(t->lchild);
post_order_traverse(t->rchild);
printf("%c ", t->data);
}
}
int main()
{
BITREE t;
t = create_tree();
printf("\n前序遍历为\n");
pre_order_traverse(t);
printf("\n中序遍历为\n");
in_order_traverse(t);
printf("\n后序遍历为\n");
post_order_traverse(t);
printf("\n");
}