10.3:二叉树创建
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *lchild;
struct Node *rchild;
}*Tree;
Tree create()
{
int element ;
printf("请输入元素:");
scanf(" %d",&element);
if(element == 0)
{
return NULL;
}
Tree t = (Tree)malloc(sizeof(struct Node));
t->data = element ;
puts("左");
t->lchild = create();
puts("右");
t->rchild = create();
}
void first_output(Tree t)
{
if(NULL == t)
return;
printf("%d ",t->data);
first_output(t->lchild);
first_output(t->rchild);
}
int main(int argc, const char *argv[])
{
Tree t = create();
printf("先序遍历:");
first_output(t);
puts("");
return 0;
}
运行结果: