c - 前缀树实现

标签 c prefix-tree

我正在存储一个城市列表(从文件中读取)及其相应的纬度和经度值。在每个城市的末尾,我试图附加经度和纬度值。

例如,trie 中的 Fremont 看起来像

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

我能够成功地将值插入到 trie 中,但是当我尝试搜索特定城市时,经度和纬度值返回为(空)

这是我的实现

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


}

最佳答案

首先longilat类型为 char * , 不是 char作为value , 所以初始化

   pNode->longi = '\0';
   pNode->lat = '\0';
   pNode->value = '\0';

看起来不太对。

应该是

   pNode->longi = NULL;
   pNode->lat = NULL;
   pNode->value = '\0';

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

下一个注意点是使用strncpystrlen功能。

当你运行 trieInsert 时收到指向 char 的指针,您应该将它们检查为 if(longitude != NULL)在像 strlen(longitude) 这样的表达式中使用之前和 strncpy(node->longi, longitude, longitudeLen + 1) .当然,指针的逻辑必须是这样的:

  • 定义指针并用 NULL 初始化它;

  • malloc分配内存或任何其他分配函数(如果分配失败,C 标准动态内存分配函数返回空指针);

  • 检查指针的值并在if( p != NULL)之后使用它或 if( p ) .

这是一种很好的做法。

关于c - 前缀树实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42266419/

相关文章:

java - Kotlin/Native 安装报错: 'jni.h' file not found

c - C中文件的最大和最小数量

SSE中的比较操作

c - Madgwick 2010 AHRS 在 C 语言中的实现

C 编程 - float 条件的 while 循环的异常行为

string - 选择合适的数据结构(哈希表与后缀树)来索引非常大的相似字符串集

c++ - 将前缀表达式树 vector 转换为 ORF/Karva 表示法表达式树 vector

algorithm - 我可以使用在每个节点上都有一个完整单词的 trie 吗?