Insert and Search implementation in Prefix Tree

97 Views Asked by At

I am working on an implementation with prefix, where i am trying to construct the following

F->R->E->T->(latitude + longitude)

I have implemented the insert function and it seems to be working. I verify this by printing out the respective latitude and longitude values.

The issue I am running into is in my search function where the latitude and longitude values return (null). Also the search function does return true for a word.

At the moment I am not able to understand where the underlying issue is

Insert Function

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

        }
    }
}

My Search function

bool getTrie(struct trieNode *root, char *key){
    struct trieNode *pNode = root;
    bool flag = true;
    if(!key){
        printf("Word is empty \n");
        return false;
    }

    if(!root){
        printf("Trie is empty \n");
        return false;
    }
    int i = 0;
    while(key[i] != '\0'){
        int indexVal = convertLetterToIndex(key[i]);
        if(!pNode->children[indexVal]){
            printf("Character not found in trie \n");
            flag = false;
            break;
        }

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

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

    return flag;
}

In my insert function, are the latitude and longitude values being added correctly?

EDIT

Definition of my struct

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

There are 1 best solutions below

0
On

So I found out the issue i was running into

Instead of

node->longi = malloc(longitudeLen + 1);
node->lat = malloc(latitudeLen + 1);

it should have been

parent->longi = malloc(longitudeLen + 1);
parent->lat = malloc(latitudeLen + 1);