二叉树存储数据过大
我在写一个二叉搜索树 数据内容是这样的:name;movielist\n
input file是由很多行这样的数据组成的csv文件,存储之后 需要读出要搜索的key后将搜索结果打印出来
我的代码用自己的小点的文件运行是没问题的 但是老师的给定文件很大 一运行就崩溃了
找了半天问题实在找不出来 初学者求解答 感谢
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct MovieInfo{
char name[128];
char *movie;
}MovieInfo_t;
struct BstNode{
MovieInfo_t person;
struct BstNode* left;
struct BstNode* right;
};
struct BstNode* GetNewNode(MovieInfo_t* info);
struct BstNode* insert(struct BstNode* root,MovieInfo_t* info);
void search(struct BstNode* root,char key[],FILE *fp,int *compare,int *flag);
struct BstNode* CreateTree(struct BstNode* root,FILE *fp);
struct BstNode* GetNewNode(MovieInfo_t* info) {
struct BstNode* NewNode = (struct BstNode*)malloc(sizeof(struct BstNode));
if (NewNode == NULL) {
exit(EXIT_FAILURE);
}
NewNode->person = *info;
NewNode->left = NewNode->right = NULL;
return NewNode;
}
struct BstNode* insert(struct BstNode* root,MovieInfo_t* info) {
if (root == NULL) {
root = GetNewNode(info);
}
else if ((strcmp(info->name,root->person.name)) <= 0) {
root->left = insert(root->left,info);
}
else {
root->right = insert(root->right,info);
}
return root;
}
void search(struct BstNode* root,char key[],FILE *fp,int *compare,int *flag) {
/* flag check if any same element in tree and compare count the
times that to be compare */
if (root == NULL) {
if (*flag == 0) {
fprintf(fp,"%s --> NOT FOUND\n",key);
}
printf("%s --> %d\n",key,*compare);
return;
}
else if (strcmp(key,root->person.name) == 0) {
*compare = *compare+1;
*flag = *flag+1;
fprintf(fp,"%s --> %s\n",key,root->person.movie);
return search(root->left,key,fp,compare,flag);
}
else if (strcmp(key,root->person.name) < 0) {
*compare = *compare+1;
return search(root->left,key,fp,compare,flag);
}
else {
*compare = *compare+1;
return search(root->right,key,fp,compare,flag);
}
}
struct BstNode* CreateTree(struct BstNode* root,FILE *fp) {
char buffer[422498];
while ((fgets(buffer,422498,fp)) != NULL) {
MovieInfo_t* info = (MovieInfo_t*)malloc(sizeof(MovieInfo_t));
if (info == NULL) {
printf("fail to apply memory.\n");
exit(EXIT_FAILURE);
}
info->movie = (char*)malloc(sizeof(char)*strlen(buffer));
if (info->movie == NULL) {
printf("fail to apply memory.\n");
exit(EXIT_FAILURE);
}
strcpy(info->name,strtok(buffer,";"));
strcpy(info->movie,strtok(NULL,";"));
root = insert(root,info);
free(info);
}
return root;
}
int main(int argc,char *argv[]) {
FILE *fp1 = NULL,*fp2 = NULL,*KeyFile = NULL;
int compare;
int flag;
char KeyBuffer[128],key[128];
struct BstNode* root = NULL;
if (argc == 1) {
printf("No Files exist.\n");
exit(EXIT_FAILURE);
}
if ((fp1 = fopen(argv[1],"r")) == NULL){
printf("Cannot open datafile.\n");
exit(EXIT_FAILURE);
}
else {
root = CreateTree(root,fp1);
}
fclose(fp1);
if ((KeyFile = fopen(argv[3],"r")) == NULL) {
printf("Cannot open keyfile.\n");
exit(EXIT_FAILURE);
}
fp2 = fopen(argv[2],"w");
while ((fgets(KeyBuffer,128,KeyFile)) != NULL) {
compare = 0,flag = 0;
strncpy(key,KeyBuffer,strlen(KeyBuffer)-1);
/* read in key from keyfile and take away '\n'at the end*/
key[strlen(KeyBuffer)-1] = '\0';
search(root,key,fp2,&compare,&flag);
}
free(root);
fclose(KeyFile);
fclose(fp2);
return 0;
}