finished trie.find

This commit is contained in:
wyy 2013-06-24 13:48:09 +08:00
parent d28383948b
commit 121b58cb9d
2 changed files with 24 additions and 2 deletions

View File

@ -64,6 +64,24 @@ namespace CppJieba
_display(_root, 0); _display(_root, 0);
} }
bool Trie::find(const ChUnicode* chUniStr, size_t len)
{
TrieNode* p = _root;
for(size_t i = 0; i < len; i++)
{
ChUnicode chUni = chUniStr[i];
if(p->hmap.find(chUni) == p->hmap.end())
{
return false;
}
else
{
p = p->hmap[chUni];
}
}
return p->isLeaf;
}
void Trie::_display(TrieNode* node, int level) void Trie::_display(TrieNode* node, int level)
{ {
if(NULL == node) if(NULL == node)
@ -112,7 +130,6 @@ namespace CppJieba
p->isLeaf = true; p->isLeaf = true;
return true; return true;
} }
} }
#ifdef TRIE_UT #ifdef TRIE_UT
@ -122,6 +139,10 @@ int main()
Trie trie; Trie trie;
trie.init("test/dict.txt"); trie.init("test/dict.txt");
trie.display(); trie.display();
char * utf = "B";
ChUnicode chUniStr[16];
int uniLen = utf8ToUnicode(utf, sizeof(utf), chUniStr);
cout<<trie.find(chUniStr, uniLen)<<endl;
//hash_map<ChUnicode, int> hmap; //hash_map<ChUnicode, int> hmap;
//hmap[136]=1; //hmap[136]=1;
return 0; return 0;

3
Trie.h
View File

@ -33,8 +33,9 @@ namespace CppJieba
bool init(const char* const filepath = DICT_FILE_PATH); bool init(const char* const filepath = DICT_FILE_PATH);
bool destroy(); bool destroy();
void display(); void display();
void _display(TrieNode* node, int level); bool find(const ChUnicode* chUniStr, size_t len);
private: private:
void _display(TrieNode* node, int level);
bool _insert(const ChUnicode* chUniBuf, size_t len); bool _insert(const ChUnicode* chUniBuf, size_t len);
}; };
} }