// Returns if the word is in the trie. bool Trie::search(string key) { TrieNode* curr = root; for(int i=0; i<key.length(); i++){ if(!curr->inChildren(key[i])) return false; curr = curr->getChild(key[i]); } return curr->inChildren('#'); }
// Returns if there is any word in the trie // that starts with the given prefix. bool Trie::startsWith(string prefix) { TrieNode* curr = root; for(int i=0; i<prefix.length(); i++){ if(!curr->inChildren(prefix[i])) return false; curr = curr->getChild(prefix[i]); } return true; }
void Trie::insert(string s) { TrieNode* curr = root; for(int i=0; i<s.length(); i++){ if(curr->inChildren(s[i])) curr = curr->getChild(s[i]); else curr=curr->addChild(s[i]); } curr->addChild('#'); }