Why do I need to define a new Trienode in the following code as new TrieNode()? When I do it as new TrieNode, it gives a segmentation fault. Is there a default constructor for a struct datatype that's important to call here, if so what does it do?
struct TrieNode
{
struct TrieNode *children[ALPHABET_SIZE];
bool isLeaf;
};
void insert(struct TrieNode *root, string key)
{
for(int i=0; i<key.size(); i++)
{
if(root->children[key[i]-'a'] ==NULL) root->children[key[i]-'a'] = new TrieNode();
root = root->children[key[i]-'a'];
}
root->isLeaf = true;
}
childrenhold after you constructTrieNode?TrieNode * children[ALPHABET_SIZE] = {};, life suddenly gets easier.struct. C++ learned a lot from C, and one of the things it learned was, "I know darn well thatTrieNodeis astruct, so why repeat it everywhere?"NULL? C++ hasnullptr, which is better in every way.