1.递归方式创建和遍历二叉树:采用先序输入
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char data;
struct node * lchild;
struct node * rchild;
}*btree;
btree Create_Bittree(void)
{
char str;
btree new;
scanf("%c",&str);
if('#' == str)
return NULL;
else
{
if((new = (btree)malloc(sizeof(struct node))) == NULL)
{
perror("malloc");
exit(-1);
}
new->data = str;
new->lchild = Create_Bittree();
new->rchild = Create_Bittree();
}
return new;
}
void Pre_Print(btree t)
{
if(t != NULL)
{
printf("%c\t",t->data);
Pre_Print(t->lchild);
Pre_Print(t->rchild);
}
return ;
}
void Mid_Print(btree t)
{
if(t != NULL)
{
Mid_Print(t->lchild);
printf("%c\t",t->data);
Mid_Print(t->rchild);
}
return ;
}
void Beh_Print(btree t)
{
if(t != NULL)
{
Beh_Print(t->lchild);
Beh_Print(t->rchild);
printf("%c\t",t->data);
}
return ;
}
int main(void)
{
btree root;
root = Create_Bittree();
Pre_Print(root);
puts("");
Mid_Print(root);
puts("");
Beh_Print(root);
puts("");
return 0;
}