test.h
#ifndef __TEST_H__
#define __TEST_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char DataType;
typedef struct node
{
DataType data;
struct node *L;
struct node *R;
}Tree,*treePtr;
treePtr create();
void frist_show(treePtr T);
void mid_show(treePtr T);
void last_show(treePtr T);
void rmTree(treePtr T);
#endif
test.c
#include "test.h"
treePtr create()
{
DataType e;
scanf("%c",&e);
if(e == '#')
{
return NULL;
}
treePtr T = (treePtr)malloc(sizeof(Tree));
if(T == NULL)
{
printf("创建失败\n");
return NULL;
}
T->data = e;
T->L = create();
T->R = create();
return T;
}
void frist_show(treePtr T)
{
if(T == NULL)
{
return;
}
printf("%c",T->data);
frist_show(T->L);
frist_show(T->R);
}
void mid_show(treePtr T)
{
if(T == NULL)
{
return;
}
mid_show(T->L);
printf("%c",T->data);
mid_show(T->R);
}
void last_show(treePtr T)
{
if(T == NULL)
{
return;
}
last_show(T->L);
last_show(T->R);
printf("%c",T->data);
}
void rmTree(treePtr T)
{
if(T == NULL)
{
return;
}
rmTree(T->L);
rmTree(T->R);
free(T);
}
main.c
#include "test.h"
int main(int argc, char const *argv[])
{
treePtr T = create();
frist_show(T);
mid_show(T);
last_show(T);
rmTree(T);
return 0;
}