我正在用C语言编写一个程序,它通过BST搜索存储在txt文件中的数字,但是有一个我找不到的错误

代码的第一部分是通过二进制搜索树将整数存储在ans.txt文件中。

/*code for storin the numbers in a particulr txt file */ #include #include #include struct root{ int val ; struct root *left ; struct root *right ; }; void write(struct root *,int); // to write the numbers in a file struct root *insrt(struct root *r,int val){ struct root *p,*q; int ch,i=0; FILE *fp; if(r==NULL){ r=(struct root *)malloc(sizeof(struct root )); p = r; r->val=val; r->left=r->right=NULL; i++; } else{ if(valval) r->left=insrt(r->left,val); else if(val>r->val) r->right=insrt(r->right,val); } write(r,i); return r; } void write(struct root *r,int i){ FILE *fp; fp=fopen("texts/bst.txt","w"); fwrite(r,sizeof(struct root),1,fp); fclose(fp); } void infile(struct root *r){ //storing the address of the root in ans.txt FILE *fp; fp=fopen("texts/ans.txt","a"); fprintf(fp,"%d\n",r->val); } void show(struct root *r){ if(r){ show(r->left); printf("%d ",r->val); infile(r); show(r->right); } } int main() { int val,i=0,n; char y; struct root *r; printf("Enter the number: \n"); while((y=getchar())!=EOF){ if(y!=EOF){ scanf("%d",&val); r=insrt(r,val); i++; } } show(r); return 0; } 

这部分是我试图找到存储号码的地址的搜索,然后从那里我可以应用二进制搜索树方法来查找数字,但是有些错误我找不到。 请帮我。

  /*code for search */ #include #include struct root{ int val; struct root *left; struct root *right; }; struct root *srch(struct root *r, int a){ if(r->val>a){ return srch(r->left,a); } else if (r->valright,a); } else return r; } int main() { int num; struct root *r,*f; r=(struct root *)malloc(sizeof(struct root )); FILE *fp; fp=fopen("texts/bst.txt","rb"); fread(r,sizeof(struct root),1,fp); fclose(fp); printf("Enter the Number: "); scanf("%d",&num); f=srch(r,num); if(f) printf("Found"); else printf("Not Found"); return 0; } 

你有一些问题。
1. while((y=getchar())!=EOF){ :丢弃一个字符。
2. fwrite(r,sizeof(struct root),1,fp); :仅存储根节点。 在阅读时无法再现指针。
3. struct root *srch(struct root *r, int a){ if(r->val>a){ :首先,你需要进行NULL检查。

变化的例子:

bst.h:

 #ifndef BST_H #define BST_H #define BST_TEXT_FILE "texts/ans.txt" #define BST_BIN_FILE "texts/bst.dat" struct root{ int val; struct root *left; struct root *right; }; #endif 

input_store.h:

 #include  #include  #include "bst.h" typedef enum ftype { Bin, Text } Ftype; int fprint_bst(FILE *fp, struct root *r, Ftype type ){ if(r){ int count = 0; count += fprint_bst(fp, r->left, type); if(type == Text) fprintf(fp, "%d\n", r->val);//write text else fwrite(&r->val, sizeof(r->val), 1, fp);write bin count += fprint_bst(fp, r->right, type); return count + 1;//count elements then it return } return 0; } void fwrite_bst(struct root *r){ FILE *fp; int n; fp = fopen(BST_TEXT_FILE, "wt"); n = fprint_bst(fp, r, Text); fclose(fp); fp = fopen(BST_BIN_FILE, "wb"); fwrite(&n, sizeof(n), 1, fp);//store number of elements fprint_bst(fp, r, Bin); fclose(fp); } struct root *insert(struct root *r, int val){ if(r == NULL){ r = malloc(sizeof(*r)); r->val = val; r->left = r->right = NULL; } else { if(val < r->val) r->left = insert(r->left, val); else if(val > r->val) r->right = insert(r->right, val); } return r; } void free_bst(struct root *r){ if(r){ free_bst(r->left); free_bst(r->right); free(r); } } int main(void){ int val; struct root *r = NULL; printf("Enter the number: \n"); while(1 == scanf("%d", &val)){ r = insert(r, val); } fprint_bst(stdout, r, Text); fwrite_bst(r); free_bst(r); return 0; } 

SEARCH.C:

 #include  #include  #include "bst.h" struct root *search(struct root *r, int a){ if(r == NULL)//need NULL check return NULL;//and return NULL if(r->val > a) return search(r->left, a); else if(r->val < a) return search(r->right,a); else return r; } struct root *new_node(int val){ struct root *node = malloc(sizeof(*node)); if(!node){ fprintf(stderr, "fail malloc\n"); exit(EXIT_FAILURE); } node->val = val; node->left = node->right = NULL; return node; } struct root *create_bst(int n, int data[n]){ if(n == 0) return NULL; else if(n == 1) return new_node(data[0]); int mid = n / 2;//split [left part] mid [right part] struct root *r = new_node(data[mid]); r->left = create_bst(mid, data); r->right = create_bst(n - mid -1, data+mid+1); return r; } struct root *load(void){ int n; FILE *fp = fopen(BST_BIN_FILE, "rb"); fread(&n, sizeof(n), 1, fp);//read number of elements int *data = malloc(n * sizeof(*data)); fread(data, sizeof(*data), n, fp); fclose(fp); struct root *r = create_bst(n, data); free(data); return r; } int main(void){ int num; struct root *f, *r = load();//Constitute the tree from the file printf("Enter the Number: "); scanf("%d", &num); f = search(r, num); if(f) puts("Found"); else puts("Not Found"); //free_bst(r);//Omitted because it is the same return 0; }