Implement a trie with Example: Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // returns true trie.search("app"); // returns false trie.startsWith("app"); // returns true trie.insert("app"); trie.search("app"); // returns true Note:
|
实现一个 Trie (前缀树),包含 示例: 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 说明:
|
前缀树:Trie树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种。
典型应用是
1. 用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。
2. 用于前缀匹配,比如我们在搜索引擎中输入待搜索的字词时,搜索引擎会给予提示有哪些前缀。
它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。缺点就是空间开销大。
例如,一个保存了8个键的trie结构,"A", "to", "tea", "ted", "ten", "i", "in", and "inn".如下图所示:
前缀树有如下特点:
1. 根节点不包含字符,除根节点外每一个节点都只包含一个字符。
2. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
3. 每个节点的所有子节点包含的字符都不相同。
4. 如果字符的种数为n,则每个结点的出度为n,这也是空间换时间的体现,浪费了很多的空间。
5. 插入查找的复杂度为O(n),n为字符串长度。
class TrieNode{ //前缀树节点
public:
bool isword; //根节点到当前节点的字符串是否是单词
TrieNode *child[26]; //每个节点有26个子节点
TrieNode():isword(false){
for(auto &a:child)
a=NULL;
}
};
class Trie {
public:
/** Initialize your data structure here. */
TrieNode *root; //定义个根节点
public:
Trie() {
root = new TrieNode();
}
/** Inserts a word into the trie. */
void insert(string word) {
TrieNode *cur = root;
for(int i=0;i<word.size();++i)
{
if(cur->child[word[i]-'a']==NULL) //没有第i个节点
cur->child[word[i]-'a']= new TrieNode(); //新建
cur = cur->child[word[i]-'a']; //遍历
}
cur->isword=true; //插入之后标记是个单词
}
/** Returns if the word is in the trie. */
bool search(string word) {
TrieNode *cur = root;
for(int i=0;i<word.size();++i)
{
if(cur->child[word[i]-'a']==NULL)
return false;
cur = cur->child[word[i]-'a'];
}
return cur->isword; //返回是否是一个单词
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
TrieNode *cur = root;
for(int i=0;i<prefix.size();++i)
{
if(cur->child[prefix[i]-'a']==NULL)
return false;
cur = cur->child[prefix[i]-'a'];
}
return true; //查找到前缀了就返回true
}
};
/**
* 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);
*/