Three Definition of the Trie class
Trie.h:
#include <map>
#include <string>
#include "TrieNode.h"
Class Trie {
Private
Trienode __emptyroot;
int __pace;
trienode* Insert (trienode* parent, std::string& keyword);
Trienode* Insertbranch (trienode* parent, std::string& keyword);
Trienode* Find (trienode* parent, std::string& keyword);
Std::string toString (trienode* parent);
std::string getkeywords (const std::string& character, const trienode* parent);
Public
int __size;
Trie (trienode::encoding Encoding);
~trie ();
trienode* Insert (std::string& keyword);
trienode* Insert (const char* keyword);
trienode* find (std::string& keyword);
std::string toString ();
Std::string getkeywords ();
};
Inline Trie::~trie () {
}
Description
1.__emptyroot is the root node of the Trienode tree established above
The 2.trienode* trie::find (std::string& keyword) function is to find a branch that is constructed by keyword in turn, such as a branch built with HelloWorld as a unit h->e- >l->l->o->w->o->r->l->d, you can determine if Hello is a sensitive word when looking for hello
Four Implementation of the Trie class:
Trie.cpp:
#include "Trie.h"
Trie::trie (trienode::encoding Encoding): __emptyroot (Trienode (Encoding)), __size (0) {
Switch (encoding) {
Case TRIENODE::UTF8:
__pace = 1;
Break
Case TRIENODE::UTF16:
__pace = 2;
Break
Default
Logger::error ("Unrecognized encoding type.");
Break
}
}
trienode*
Trie::insert (std::string& keyword) {
Return insert (&__emptyroot, keyword);
}
trienode*
Trie::insert (const char* keyword) {
std::string ___keyword (keyword);
return insert (___keyword);
}
trienode*
Trie::insert (trienode* parent, std::string& keyword) {
if (keyword.size () = = 0) {
return NULL;
}
std::string ___firstcharacter = keyword.substr (0, __pace);
trienode* ___firstnode = Parent->findchild (___firstcharacter);
if (___firstnode = = NULL) {
Return Insertbranch (parent, keyword);
}
std::string ___reststring = Keyword.substr (__pace, Keyword.size ());
Return Insert (___firstnode, ___reststring);
}
trienode*
Trie::insertbranch (trienode* parent, std::string& keyword) {
std::string ___firstcharacter = keyword.substr (0, __pace);
trienode* ___firstnode = Parent->insertchild (___firstcharacter);
if (___firstnode!= NULL) {
std::string ___reststring = Keyword.substr (__pace, Keyword.size ());
if (!___reststring.empty ())
Return Insertbranch (___firstnode, ___reststring);
}
return NULL;
}
trienode*
Trie::find (std::string& keyword) {
Return find (&__emptyroot, keyword);
}
trienode*
Trie::find (trienode* parent, std::string& keyword) {
std::string ___firstchild = keyword.substr (0, __pace);
trienode* ___firstnode = Parent->findchild (___firstchild);
if (___firstnode = = NULL) {
return NULL;
}
Last character, then stop searching
if (keyword.size () = = (unsigned int) __pace) {
return ___firstnode;
}
More than one character
std::string ___reststring = Keyword.substr (__pace, Keyword.size ());
if (___firstnode->__map.empty ()) {
return ___firstnode;
}
Return find (___firstnode, ___reststring);
}
std::string
Trie::tostring () {
std::string ___result ("[");
bool ___isfirstchild = true;
for (Trienode::_triemapiterator ___it = __emptyroot.__map.begin ();
___it!= __emptyroot.__map.end (); ++___IT) {
if (___isfirstchild) {
___result.append (ToString (const_cast<trienode*> (___it->second)));
___isfirstchild = false;
} else {
___result.append (",");
___result.append (ToString (const_cast<trienode*> (___it->second)));
}
}
___result.append ("]");
return ___result;
}
std::string
Trie::tostring (trienode* parent) {
std::string ___result ("[");
___result + + parent->getcharacter () + ":";
bool ___isfirstchild = true;
for (Trienode::_triemapiterator ___it = Parent->__map.begin ();
___it!= parent->__map.end (); ++___IT) {
if (___isfirstchild) {
___result.append (ToString (const_cast<trienode*> (___it->second)));
___isfirstchild = false;
} else {
___result.append (",");
___result.append (ToString (const_cast<trienode*> (___it->second)));
}
}
___result.append ("]");
return ___result;
}
std::string
Trie::getkeywords () {
Std::string ___result;
for (Trienode::_triemapiterator ___it = __emptyroot.__map.begin ();
___it!= __emptyroot.__map.end (); ++___IT) {
___result.append (GetKeywords (___it->first, & (___it->second)));
}
return ___result;
}
std::string
Trie::getkeywords (const std::string& character, const trienode* parent) {
Std::string ___result;
for (Trienode::_triemapiterator ___it = Parent->__map.begin ();
___it!= parent->__map.end (); ++___IT) {
___result.append (Logger::tohex (character, true));
___result.append (GetKeywords (___it->first, & (___it->second)));
}
Last character
if (parent->__map.size () = = 0) {
__size++;
___result.append (Logger::tohex (character, true));
___result.append ("\ n");
}
return ___result;
}
Description
1.trienode* Trie::insert (trienode* parent, std::string& keyword) is established with parent as the root node when the Trienode tree is established, at the beginning the parent is __ Emptyroot, and then add keyword to the tree, assuming that __emptyroot is empty at the beginning, and that keyword is Hello, it will be built as a branch of "hello" as a twig h->e->l->l->o, After that, if you want to add hero again, because hero is the same as the first two characters of Hello, it will be based on H->e->l->l->o, starting from the letter e to grow a branch, that is, H->e->r->o, The two branches share h->e
2.__pace is related to the encoding method, UTF16 encoding, then __pace for 2,UTF8 is variable length encoding, no longer detailed.
This article refers to the article written from Michael, specific reference: Http://my.csdn.net/Poechant