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

1
Trie.h
View File

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