// Returns if the word is in the trie. bool search(string key) { int len = key.size(); TrieNode *node = root; for(int i=0;i<len;i++){ node = node->get_childen(key[i]); if(node==NULL) return false; } return node->iswords(); }
// Returns if there is any word in the trie // that starts with the given prefix. bool startsWith(string prefix) { int len = prefix.length(); TrieNode* node = root; for(int i=0; i<len; i++){ node = node->get_childen(prefix[i]); if(node==NULL){ return false; } } return true; }
// Inserts a word into the trie. void insert(string s) { int len = s.size(); TrieNode* parent = root, *node; for(int i=0;i<len; i++){ node = parent->get_childen(s[i]); if(node == NULL){ node=new TrieNode(); parent->set_childen(s[i] - 'a', node); } parent = node; } parent->markAsWord(); }