/** Inserts a word into the trie. */ voidinsert(string word){ Trie* node = this; for (char c : word) { if (node->next[c - 'a'] == nullptr) { node->next[c - 'a'] = newTrie(); } node = node->next[c - 'a']; }
node->isEnd = true; }
/** Returns if the word is in the trie. */ boolsearch(string word){ Trie* node = this; for (char c : word) { if (node->next[c - 'a'] == nullptr) { returnfalse; } node = node->next[c - 'a']; } return node->isEnd; }
/** Returns if there is any word in the trie that starts with the given prefix. */ boolstartsWith(string prefix){ Trie* node = this; for (char c : prefix) { if (node->next[c - 'a'] == nullptr) { returnfalse; } node = node->next[c - 'a']; } returntrue; } };