bool search(std::string word) { TrieNode* current = root, *node = nullptr; for (int i = 0; i < word.size(); ++i) { node = current->getSon(word[i]);//查找当前字符对应的子节点是否存在 if (node == nullptr) return false; current = node; } return node->isEnd(); //注意这里,就算所有字符都存在,也要看是不是一个字符串的结尾 }
bool startsWith(std::string prefix) { TrieNode* current = root, *node = nullptr; for (int i = 0; i < prefix.size(); ++i) { node = current->getSon(prefix[i]); if (node == nullptr) return false; current = node; } return true; }
void insert(std::string word) { TrieNode* current = root, *node = nullptr; for (int i = 0; i < word.size(); ++i) { node = current->getSon(word[i]); if (node == nullptr)//该字符对应的子节点不存在,就创建一个子节点,连接到该字符 { node = new TrieNode(); current->setSon(word[i], node); } current = node; //current指向子节点 } node->setEnd(); //插入完一个字符串之后,setEnd表示字符串结束 }