0

Hi I'm a beginner in C++ trying to implement basic trie structure .Someone please help. Any feedback is welcome.I'm getting a runtime error of

Line 15: Char 25: error: no member named 'children' in 'Trie'
           if(present->children[word[i]-'a']==NULL)

I have tried to solve this for past 2 days. Still no idea.

class Trie {
    public:
        /** Initialize your data structure here. */
        Trie() {
            string val="";
            Trie* children[26];
            bool flag=false;
        }
        Trie* root=new Trie();
        /** Inserts a word into the trie. */
        void insert(string word) {
            Trie* present=root;
            for(int i=0;i<word.size();i++)
            {
                if(present->children[word[i]-'a']==NULL)
                {
                    Trie* p=new Trie();
                    p->val=present->val+word[i];
                    present->children[word[i]-'a']=p;
                    present=p;
                }
                else
                    present=present->children[word[i]-'a'];
            w}
            present->flag=true;
        }
        
        /** Returns if the word is in the trie. */
        bool search(string word) {
            Trie* present=root;
            for(int i=0;i<word.size();i++)
            {
                if(present->children[word[i]-'a']==NULL)
                    return false;
                else
                    present=present->children[word[i]-'a'];
            }
            return true;
        }
        
        /** Returns if there is any word in the trie that starts with the given prefix. */
        bool startsWith(string prefix) {
            Trie* present=root;
            for(int i=0;i<word.size();i++)
            {
                if(present->children[word[i]-'a']==NULL)
                    return false;
                else
                    present=present->children[word[i]-'a'];
            }
            for(int i=0;i<26;i++)
            {
                if(present->children[i]!=NULL)
                    return true;
            }
            return false;
        }
    };
    
    /**
     * Your Trie object will be instantiated and called as such:
     * Trie* obj = new Trie();
     * obj->insert(word);
     * bool param_2 = obj->search(word);
     * bool param_3 = obj->startsWith(prefix);
     */
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
gaurav1903
  • 41
  • 1
  • 7
  • 4
    That's not a runtime error. `children` is a local variable in the constructor body. Other functions cannot accesas it. I'd recommend, you rather start with a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), before starting to create more complex stuff. – πάντα ῥεῖ May 15 '21 at 09:18

1 Answers1

1

In this constructor

   Trie() {
        string val="";
        Trie* children[26];
        bool flag=false;
    }

you created a local array children that will not be alive and visible outside the constructor.

The class has only one data member

Trie* root=new Trie();

that can be accessed inside member functions of the class.

What you need is to design a data structure that will represent a node of the trie and then the data structure trie itself that will contain a pointer or pointers to the data structure that represents a node.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335