finished destrop in trie

This commit is contained in:
wyy 2013-06-24 14:47:38 +08:00
parent 121b58cb9d
commit 812adcc20b
2 changed files with 30 additions and 4 deletions

View File

@ -56,7 +56,16 @@ namespace CppJieba
bool Trie::destroy()
{
return true;
if(NULL == _root)
{
return false;
}
else
{
bool ret = _destroyNode(_root);
_root = NULL;
return ret;
}
}
void Trie::display()
@ -82,6 +91,18 @@ namespace CppJieba
return p->isLeaf;
}
bool Trie::_destroyNode(TrieNode* node)
{
for(TrieNodeHashMap::iterator it = node->hmap.begin(); it != node->hmap.end(); it++)
{
TrieNode* next = it->second;
_destroyNode(next);
}
delete node;
return true;
}
void Trie::_display(TrieNode* node, int level)
{
if(NULL == node)
@ -137,12 +158,16 @@ using namespace CppJieba;
int main()
{
Trie trie;
trie.init("test/dict.txt");
trie.display();
char * utf = "B";
//trie.init("test/dict.txt");
trie.init("dict.txt");
//trie.display();
const char * utf = "B";
ChUnicode chUniStr[16];
int uniLen = utf8ToUnicode(utf, sizeof(utf), chUniStr);
cout<<trie.find(chUniStr, uniLen)<<endl;
getchar();
trie.destroy();
getchar();
//hash_map<ChUnicode, int> hmap;
//hmap[136]=1;
return 0;

1
Trie.h
View File

@ -35,6 +35,7 @@ namespace CppJieba
void display();
bool find(const ChUnicode* chUniStr, size_t len);
private:
bool _destroyNode(TrieNode* node);
void _display(TrieNode* node, int level);
bool _insert(const ChUnicode* chUniBuf, size_t len);
};