本文整理汇总了C++中TrieNode::AddChild方法的典型用法代码示例。如果您正苦于以下问题:C++ TrieNode::AddChild方法的具体用法?C++ TrieNode::AddChild怎么用?C++ TrieNode::AddChild使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TrieNode
的用法示例。
在下文中一共展示了TrieNode::AddChild方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: AddWord
void Trie::AddWord(string s) {
TrieNode* current = root;
if(s.length() == 0)
{
current->Mark(); // recognize empty word
return;
}
unsigned long len = s.length();
for (int i=0; i<len-1; i++) {
TrieNode *child = current->FindChild(s[i]);
if(child != NULL)
{
current = child;
}
else {
TrieNode* tmp = new TrieNode(s[i], false);
tmp->Set(s[i]);
current->AddChild(tmp);
current = tmp;
}
if (s[i+1]=='/') {
current->Mark();
break;
}
if ( i == len - 2 )
current->Mark();
}
}
示例2: insert
// Inserts a word into the trie.
void insert(string s)
{
TrieNode *prev = nullptr;
TrieNode *curr = m_root;
// We check the characters of s one by one. Note that
// we also add the NULL terminator to the Trie which
// indicates the end of a word.
for (int i = 0; i <= s.length(); i++)
{
prev = curr;
curr = prev->FindChild(s[i]);
// Can't find a TrieNode with the character s[i],
// so create a new one and add it to the children
// list of prev.
if (curr == nullptr)
{
curr = new TrieNode(s[i]);
prev->AddChild(curr);
}
}
}