Search code examples
c++data-structurestrie

need help in implementation of trie in c++


class node {

     public:
     node(){
       value  = 0;
       arr = new node*[26];
       for(int i =0 ; i<26;i++)
         arr[i]= NULL;
       }
      int  value;
      node ** arr ;
};
class trie {
     public:
        trie(){
           root = NULL;
        }
     node * root;
     int count;
};
int main(){
        string A[5] = {"apple, ball , bat , cat, car"};
        trie* too;
        node *p = new node();
        too->root = p; // **here**
}

Here :i am having a runtime error.. to me its seems to be correct ..I have no idea whats wrong with it. Any help would be of Great Use to me :) Thank you


Solution

  • too isn't pointing to anything.

    The simplest fix is to remove the * to make it a local object instead of a pointer.

    int main(){
        string A[5] = {"apple, ball , bat , cat, car"};
        trie too; //<------ Fix here
        node *p = new node();
        too->root = p; // **here**
    }
    

    Alternatively, you can new a trie object. But for your purposes that is unnecessary.