首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >前缀树实现

前缀树实现
EN

Stack Overflow用户
提问于 2017-02-16 06:23:31
回答 1查看 208关注 0票数 1

我正在存储一个具有相应纬度和经度值的城市列表(从文件中读取)。在每个城市的尽头,我都试图附加经度和纬度值。

例如,特里尔的弗里蒙特看起来就像

F->R->E->M->O->N->T->(latitude和经度)

我能够成功地将这些值插入trie,但是当我试图搜索特定的城市时,经度和纬度值返回为(null)。

这是我的实现

代码语言:javascript
复制
void readFile(){

              //the functions that deal with the trie
                struct trieNode *node = initializeTrie();
                trieInsert(node, place, longitude, latitude);
                getTrie(node, place);
                trieFree(node);
}

struct trieNode{
        char *longi;
        char *lat;
        struct trieNode *children[27];
        char value;
};

struct trieNode *initializeTrie(){
        struct trieNode *pNode = NULL;

        pNode = (struct trieNode *)malloc(sizeof(struct trieNode));
        if(pNode){
                pNode->longi = '\0';
                pNode->lat = '\0';
                pNode->value = '\0';
                memset(pNode->children, 0, sizeof(pNode->children));
        }

        return pNode;
}


void trieFree(struct trieNode *root){
        int i;
        if(root){
                for(i = 0; i<= 26; i++){
                        trieFree(root->children[i]);
                }
        }
        free(root);
}

int trieInsert(struct trieNode *node, char *key, char *longitude, char *latitude){
        struct trieNode *parent = node;
        //printf("Longi: %s", longitude);
        //printf(" ");
        //printf("Latitude: %s \n", latitude);
        if(key){
                int index = 0;
                int i = 0;

                if(node){
                        while(key[i] != '\0'){
                                int indexVal = convertLetterToIndex(key[i]);
                                if(!parent->children[indexVal]){
                                        parent->children[indexVal] = initializeTrie();
                                        parent->children[indexVal]->value = key[i];
                                }
                                parent = parent->children[indexVal];
                                i++;
                        }

                        int longitudeLen = strlen(longitude);
                        int latitudeLen = strlen(latitude);

                        node->longi = malloc(longitudeLen + 1);
                        strncpy(node->longi, longitude, longitudeLen + 1);
                        node->longi[longitudeLen] = '\0';
                        //printf("Longi: %s", node->longi);
                        node->lat = malloc(latitudeLen + 1);
                        strncpy(node->lat, latitude, latitudeLen + 1);
                        node->lat[latitudeLen] = '\0';
                        //printf("Lati: %s \n", node->lat);
                        //free(node->longi);
                        //free(node->lat);
                }
        }


}

 //function to print the long and lat values based on the city
void getTrie(struct trieNode *root, char *key){
        struct trieNode *pNode = root;
        //bool flag = false;
        if(!key){
            printf("Not found \n");
        }

        if(!root){
                printf("Not found \n");
        }
        int i = 0;
        while(key[i] != '\0'){
                int indexVal = convertLetterToIndex(key[i]);
                if(!pNode->children[indexVal]){
                        printf("Not found \n");
                }

                pNode = pNode->children[indexVal];
                i++;
        }

        printf("Longitude: %s", pNode->longi);
        printf(" ");
        printf("Latitude: %s \n", pNode->lat);


}
EN

回答 1

Stack Overflow用户

发布于 2017-02-16 07:17:25

首先,longilatchar *类型的,而不是char类型的value,因此初始化

代码语言:javascript
复制
   pNode->longi = '\0';
   pNode->lat = '\0';
   pNode->value = '\0';

看上去不太对。

它应该是

代码语言:javascript
复制
   pNode->longi = NULL;
   pNode->lat = NULL;
   pNode->value = '\0';

(我不想问为什么value只是一个字符--它是数据表示的特殊方式)

接下来要注意的是使用strncpystrlen函数。

当函数trieInsert接收到char的指针时,在像strlen(longitude)strncpy(node->longi, longitude, longitudeLen + 1)这样的表达式中使用之前,您应该检查它们为if(longitude != NULL)。当然,带有指针的逻辑必须如下:

  • 定义指针并用NULL插入它;
  • 使用malloc或任何其他函数分配内存(C标准动态内存分配函数在分配失败时返回空指针);
  • 检查指针的值,并在if( p != NULL)if( p )之后使用它。

这是一种好的做法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42266419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档