typedef char datatype;
typedef struct Node
{
datatype data;//数据域
struct Node *left;//左
struct Node *right;//右
}Node,*tree;
//先序创建
tree tree_create();
//先序遍历
void pri_order(tree T);
//中序遍历
void in_order(tree T);
//后序遍历
void after_order(tree T);
#endif
#include<stdio.h>
#include<stdlib.h>
#include"tree.h"
//先序创建
tree tree_create()
{
tree T=(tree)malloc(sizeof(Node));
if(T==NULL)
{
return NULL;
}
datatype val;
scanf(" %c",&val);
if(val=='#')
{
return NULL;
}
T->data=val;
T->left=tree_create();
T->right=tree_create();
return T;
}
//先序遍历
void pri_order(tree T)
{
if(NULL==T)
{
return ;
}
printf("%c\t",T->data);
pri_order(T->left);
pri_order(T->right);
}
//中序遍历
void in_order(tree T)
{
if(NULL==T)
{
return ;
}
in_order(T->left);
printf("%c\t",T->data);
in_order(T->right);
}
//后序遍历
void after_order(tree T)
{
if(NULL==T)
{
return ;
}
after_order(T->left);
after_order(T->right);
printf("%c\t",T->data);
}
#include<stdio.h>
#include<stdlib.h>
#include"tree.h"
int main(int argc, const char *argv[])
{
tree T = tree_create();
if(T==NULL)
{
printf("创建失败\n");
return -1;
}
printf("创建成功\n");
pri_order(T);
putchar(10);
printf("遍历成功\n");
in_order(T);
putchar(10);
printf("遍历成功\n");
after_order(T);
putchar(10);
printf("遍历成功\n");
return 0;
}