实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。
示例:
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // 返回 true
trie.search("app"); // 返回 false
trie.startsWith("app"); // 返回 true
trie.insert("app");
trie.search("app"); // 返回 true
说明:
你可以假设所有的输入都是由小写字母 a-z 构成的。
保证所有输入均为非空字符串。
前缀树的示意图
简单点说就是app与ake有共同的前缀a,为了减少空间,将这些单词的公共前缀合并,尽可能减少空间。
按照本题的要求,还需要设定一个标志,用于记录是公共子串还是字符串。
class Trie {
private:
struct TrieNode {
bool isWord;//当前节点为结尾是否是字符串
vector<TrieNode*> children;
TrieNode() : isWord(false), children(26, nullptr) {}
~TrieNode() {
for (TrieNode* child : children)
if (child) delete child;
}
};
TrieNode * trieRoot;//树的根节点
TrieNode* myFind(string str) {
TrieNode *ptr = trieRoot;//扫描这棵树
//将word的字符逐个插入
for (auto ch : str) {
if (ptr->children[ch - 'a'] == NULL) {//如果没有这个节点,说明str不在此树中
return NULL;
}
ptr = ptr->children[ch - 'a'];//将ptr转移到相应的下一层中
}
return ptr;
}
public:
/** Initialize your data structure here. */
Trie() {
trieRoot = new TrieNode();
}
/** Inserts a word into the trie. */
void insert(string word) {
TrieNode *ptr = trieRoot;//扫描这棵树,将word插入
//将word的字符逐个插入
for (auto ch : word) {
if (ptr->children[ch - 'a'] == NULL) {
ptr->children[ch - 'a'] = new TrieNode();
}
ptr = ptr->children[ch - 'a'];
}
ptr->isWord = true;//标记为单词
}
/** Returns if the word is in the trie. */
bool search(string word) {
return myFind(word) && myFind(word)->isWord;//首先寻找word是否在此树中,然后还要判断是否是字符串(有可能是公共前缀)
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
return myFind(prefix);//只需要判断这个字符串是否在树中即可,无需判断是否为前缀
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* bool param_2 = obj.search(word);
* bool param_3 = obj.startsWith(prefix);
*/