Prefix tree implementation

拈花ヽ惹草 提交于 2019-12-24 06:07:25

问题


I am working on storing a list of cities(read from a file) with their corresponding latitude and longitude values. At the end of each city, I am trying to append the longitude and latitude values.

So for example, Fremont in the trie would look like

F->R->E->M->O->N->T->(latitude and longitude)

I am able to successfully insert the values into the trie, but when i try to search for a specific city, the longitude and latitude values return as (null)

Here is my implementation

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


}

回答1:


First of all longi and lat are of type char *, not char as value, so initialization

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

looks not so right.

It rather should be

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

(I do not want to ask why value is only one character - it is special way of data representation)

The next point for attention is using strncpy and strlen functions.

When you function trieInsert receive pointers to char you should check them as if(longitude != NULL) before using in expressions like strlen(longitude) and strncpy(node->longi, longitude, longitudeLen + 1). And of course, the logic with pointer must be like:

  • define pointer and init it with NULL;

  • allocate memory with malloc or any other function for allocation (C standard dynamic memory allocation function return a null pointer if fail to allocate);

  • check the value of pointer and use it after if( p != NULL) or if( p ).

That is some kind of good practice.



来源:https://stackoverflow.com/questions/42266419/prefix-tree-implementation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!