| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 603 人关注过本帖
标题:二叉树存储数据过大
只看楼主 加入收藏
ym950209
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-9-1
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
二叉树存储数据过大
我在写一个二叉搜索树  数据内容是这样的:
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;
}
搜索更多相关主题的帖子: 搜索结果 include person 二叉树 file 
2015-09-01 18:33
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct MovieInfo{
    char name[128];
    char *movie;
}MovieInfo_t;

struct BstNode{            //为啥不和上面一样用typedef?
    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;
}

剑栈风樯各苦辛,别时冰雪到时春
2015-09-01 22:47
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:20 
代码量还是很大的,近视眼表示累了

剑栈风樯各苦辛,别时冰雪到时春
2015-09-01 22:48
ym950209
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-9-1
收藏
得分:0 
回复 3楼 林月儿
谢谢谢谢
2015-09-02 16:27
w2009w
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:190
专家分:542
注 册:2015-4-20
收藏
得分:0 
2015-09-02 16:34
快速回复:二叉树存储数据过大
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018795 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved