添加时一样,搜索不一样。深度搜索Trie树
node为正在搜索的节点,word指向正在搜索的单词起始,查找成功返回真;否则返回假;
当遍历到单词结束时(word指向’\0’):
如果node指向的结点标记为单词的结尾(is_end为真)
返回真
否则返回假
如果word指向 ’.’:
遍历node的全部孩子指针:
如果孩子指针为真,继续递归深搜该孩子子树,单词指针向前移动一个位置,
如果递归深搜为真,返回真
否则(‘a’ - ‘z’)小写字母
计算孩子位置pos = 当前字符 - ‘a’;
如果pos指向的孩子指针为真,继续递归深搜该孩子子树,单词指针向前移动一个位置,
如果递归深搜结果为真,则返回真
最终返回假
#include<vector>
#define TRIE_MAX_CHAR_NUM 26
struct TrieNode{
TrieNode* child[TRIE_MAX_CHAR_NUM];
bool is_end;
TrieNode() :is_end(false){
for(int i = 0; i < TRIE_MAX_CHAR_NUM; ++i)
child[i] = 0;
}
};
class TrieTree{
public:
TrieTree(){}
~TrieTree(){
for(int i = 0; i < _node_vec.size(); ++i)
delete _node_vec[i];
}
//插入字符串
void insert(const char* word){
TrieNode* ptr = &_root;
while(*word)
{
int pos = *word - 'a';
if(!ptr->child[pos])
ptr->child[pos] = new_node();
ptr = ptr->child[pos];
word++;
}
ptr->is_end = true;
}
//搜索trie树里有没有某个字符串
bool search(const char* word){
TrieNode* ptr = &_root;
while(*word)
{
int pos = *word - 'a';
if(!ptr->child[pos])
return false;
ptr = ptr->child[pos];
word++;
}
return ptr->is_end;
}
//判断trie树中有没有以某个字符串开头的字符串,有就返回true
bool startWith(const char* prefix){
TrieNode* ptr = &_root;
while(*prefix)
{
int pos = *prefix - 'a';
if(!ptr->child[pos])
return false;
ptr = ptr->child[pos];
prefix++;
}
return true;
}
TrieNode* root()
{
return &_root;
}
private:
TrieNode* new_node(){
TrieNode* node = new TrieNode();
_node_vec.push_back(node);
return node;
}
std::vector<TrieNode*> _node_vec;
TrieNode _root;
};
class WordDictionary {
public:
/** Initialize your data structure here. */
WordDictionary() {
}
/** Adds a word into the data structure. */
void addWord(string word) {
_trie_tree.insert(word.c_str());
}
/** 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 search_trie(_trie_tree.root(),word.c_str());
}
bool search_trie(TrieNode* node,const char* word)
{
if(*word == '\0')
{
if(node->is_end )
return true;
return false;
}
if(*word == '.')
{
for(int i = 0;i < TRIE_MAX_CHAR_NUM; ++i)
{
if(node->child[i] && search_trie(node->child[i], word+1))
return true;
}
}
else
{
int pos = *word - 'a';
if(node->child[pos] && search_trie(node->child[pos] ,word+1))
return true;
}
return false;
}
private:
TrieTree _trie_tree;
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* bool param_2 = obj.search(word);
*/