mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
finished destrop in trie
This commit is contained in:
parent
121b58cb9d
commit
812adcc20b
33
Trie.cpp
33
Trie.cpp
@ -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
1
Trie.h
@ -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);
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user