回复 5楼 wp231957
#include <stdio.h>
#include <stdlib.h>
typedef struct tree
{
int date;
struct tree *lchild,*rchild;
}Tree;
FILE *fp;
Tree *creat_tree()
{
int c;Tree *t;
fscanf(fp,"%d",&c);// c的值只取0,1,2
fgetc(fp);
if(c==0)
t=NULL;
else
{
t=(Tree*)malloc(sizeof(Tree));
fscanf(fp,"%d",&t->date);
fgetc(fp);
t->lchild=creat_tree();
t->rchild=creat_tree();
}
return t;
}
/*
//....................................错误程序
不知错那
Tree *creat_tree(Tree *t)
{
int c;
fscanf(fp,"%d",&c);// c的值只取0,1,2
fgetc(fp);
if(c==0)
t=NULL;
else
{
t=(Tree*)malloc(sizeof(Tree));
fscanf(fp,"%d",&t->date);
fgetc(fp);
creat_tree(t->lchild);
creat_tree(t->rchild);
}
return t;
}
*/
Tree* file(Tree *t)
{
fp=fopen("E:\\1.txt","r");
if(fp==NULL)
{
printf("
cannt open the file\n");
exit(0);
}
else
t=creat_tree();
fclose(fp);
return t;
}
/*
这个是排序树
Tree *tree_sort(Tree *t,int a)
{
Tree *t_node,*t1,*t2;
t_node=(Tree*)malloc(sizeof(Tree));
t_node->date=a;
t_node->lchild=NULL;
t_node->rchild=NULL;
if(t==NULL)
t=t_node;
else
{
t1=t;
while(t1)
{
if(t_node->date<t1->date)
{
t2=t1;t1=t1->lchild;
}
else
{
t2=t1;t1=t1->rchild;
}
}
if(t2->date>t_node->date)
t2->lchild=t_node;
else
t2->rchild=t_node;
}
return t;
}
*/
void traverse_tree(Tree *t)
{
if(t)
{
traverse_tree(t->lchild);
printf("
%d
",t->date);
traverse_tree(t->rchild);
}
}
void main()
{
int i, a[6]={7,56,26,45,8,9};
Tree *t;
t=NULL;
/*
for(i=0;i<6;i++)
t=tree_sort(t,a[i]);
traverse_tree(t);
*/
t=file(t);
traverse_tree(t);
}