LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

字典树(Trie树相关)

208. Implement Trie (Prefix Tree)

Implement a trie with insertsearch, and startsWith methods. (Medium)

Note:
You may assume that all inputs are consist of lowercase letters a-z.

分析:

字典树即前缀匹配树,在空间不是很影响的情况下一般采用如下数据结构存储Trie节点

 class TrieNode {
public:
// Initialize your data structure here.
bool isWord;
TrieNode* child[];
TrieNode(): isWord(false){
memset(child, NULL, sizeof(child));
}
};

所以插入、查找等操作比较直观,直接见程序。

代码:

 class TrieNode {
public:
// Initialize your data structure here.
bool isWord;
TrieNode* child[];
TrieNode(): isWord(false){
memset(child, NULL, sizeof(child));
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode* r = root;
for (int i = ; i < word.size(); ++i) {
if (r -> child[word[i] - 'a'] == NULL) {
r -> child[word[i] - 'a'] = new TrieNode(); }
r = r -> child[word[i] - 'a'];
}
r -> isWord = true;
} // Returns if the word is in the trie.
bool search(string word) {
TrieNode* r = root;
for (int i = ; i < word.size(); ++i) {
if (r -> child[word[i] - 'a'] == NULL) {
return false; }
r = r -> child[word[i] - 'a'];
}
if (r -> isWord) {
return true;
}
return false;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode* r = root;
for (int i = ; i < prefix.size(); ++i) {
if (r -> child[prefix[i] - 'a'] == NULL) {
return false; }
r = r -> child[prefix[i] - 'a'];
}
return true;
} private:
TrieNode* root;
}; // Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");

211. Add and Search Word - Data structure design

Design a data structure that supports the following two operations:

void addWord(word)
bool search(word)

search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter. (Medium)

For example:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

Note:
You may assume that all words are consist of lowercase letters a-z.

分析:

继续沿用字典树的思路,只不过加入了“.”通配符,所以在查找过程中加入DFS即可,用辅助函数helper

代码:

 class TrieNode {
public:
// Initialize your data structure here.
bool isWord;
TrieNode* child[];
TrieNode(): isWord(false){
memset(child, NULL, sizeof(child));
}
}; class Trie {
public:
Trie() {
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string word) {
TrieNode* r = root;
for (int i = ; i < word.size(); ++i) {
if (r -> child[word[i] - 'a'] == NULL) {
r -> child[word[i] - 'a'] = new TrieNode(); }
r = r -> child[word[i] - 'a'];
}
r -> isWord = true;
} //helper function for search, use to solve "." problem
bool helper(string word, int pos, TrieNode* curRoot) {
if (pos == word.size() && curRoot -> isWord) {
return true;
}
if (word[pos] != '.') {
if (curRoot -> child[word[pos] - 'a'] == NULL) {
return false;
}
else {
return helper(word, pos + , curRoot -> child[word[pos] - 'a']);
}
}
else {
bool flag = false;
for (int i = ; i < ; ++i) {
if (curRoot -> child[i] != NULL && helper(word, pos + , curRoot -> child[i]) ) {
flag = true;
}
}
return flag;
}
return true;
}
// Returns if the word is in the trie.
bool search(string word) {
return helper(word, , root);
} private:
TrieNode* root;
}; class WordDictionary {
public:
Trie dic;
// Adds a word into the data structure.
void addWord(string word) {
dic.insert(word);
} // Returns if the word is in the data structure. A word could
// contain the dot character '.' to represent any one letter.
bool search(string word) {
return dic.search(word);
}
}; // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
 
上一篇:通用的增删(分别根据id,id数组,entity删除)改查(查一个,查所有,查所有带分页)【dry-parent】


下一篇:QGraphicsItem的paint函数的一些相关问题