二叉树创建
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
char data;
struct node* left;
struct node* right;
}*btree;
btree creat()
{
char element;
printf("请输入:");
scanf(" %c",&element);
if(element=='#')
{
return NULL;
}
btree t=(btree)malloc(sizeof(struct node));
t->data=element;
t->left=creat();
t->right=creat();
return t;
}
void first_output(btree t)
{
if(NULL==t)
{
return;
}
printf("%c",t->data);
first_output(t->left);
first_output(t->right);
}
void mid_output(btree t)
{
if(NULL==t)
{
return;
}
mid_output(t->left);
printf("%c",t->data);
mid_output(t->right);
}
void last_output(btree t)
{
if(NULL==t)
{
return;
}
last_output(t->left);
last_output(t->right);
printf("%c",t->data);
}
void free_btree(btree t)
{
if(NULL==t)
{
return;
}
free_btree(t->left);
free_btree(t->right);
free(t);
t=NULL;
}
int main(int argc, const char *argv[])
{
btree t=creat();
first_output(t);
puts("");
mid_output(t);
puts("");
last_output(t);
puts("");
return 0;
}