Estoy escribiendo un progtwig en C que busca el número almacenado en un archivo txt a través de un BST pero hay un error que no puedo encontrar

La primera parte del código es almacenar los enteros en un archivo ans.txt a través de los árboles de búsqueda binarios.

/*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; } 

Esta parte es la búsqueda en la que estoy tratando de encontrar la dirección del número almacenado y luego desde allí puedo aplicar el método del Árbol de búsqueda binaria para encontrar el número, pero hay algunos errores que no puedo encontrar. Por favor, ayúdame.

  /*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; } 

Tienes algunos problemas.
1. while((y=getchar())!=EOF){ : ha descartado un carácter.
2. fwrite(r,sizeof(struct root),1,fp); : Sólo se almacenan los nodos de la raíz. El puntero no se puede reproducir en el momento de la lectura.
3. struct root *srch(struct root *r, int a){ if(r->val>a){ : Primero, debe verificar NULL.

Ejemplos de cambios:

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; }