本文整理汇总了C++中TrieNode::subNode方法的典型用法代码示例。如果您正苦于以下问题:C++ TrieNode::subNode方法的具体用法?C++ TrieNode::subNode怎么用?C++ TrieNode::subNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TrieNode
的用法示例。
在下文中一共展示了TrieNode::subNode方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: insert
// Inserts a word into the trie.
void insert(string word) {
if(search(word)) return;
TrieNode* curr = root;
for(auto c:word) {
TrieNode* node = curr->subNode(c);
if(node != nullptr) {
curr = node;
}
else {
curr->children.push_back(new TrieNode(c));
curr->numChildren++;
curr = curr->subNode(c);
}
}
curr->isend = true;
}
示例2: startsWith
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
TrieNode* node = root;
for(auto c:prefix) {
node = node->subNode(c);
if(node == nullptr) return false;
}
return true;
}
示例3: search
// Returns if the word is in the trie.
bool search(string word) {
TrieNode* node = root;
for(auto c:word) {
node = node->subNode(c);
if(node == nullptr) return false;
}
return node->isend;
}
示例4: startsWith
bool Trie::startsWith(string prefix)
{
TrieNode* curr = root;
for (auto ch : prefix) {
curr = curr->subNode(ch);
if (curr == nullptr)
return false;
}
return true;
}
示例5: search
bool Trie::search(string key)
{
TrieNode* curr = root;
for (auto ch : key) {
curr = curr->subNode(ch);
if (curr == nullptr)
return false;
}
return curr->isend == true;
}
示例6: insert
void Trie::insert(string s)
{
if (search(s)) return;
TrieNode* curr = root;
for (auto ch : s) {
TrieNode* child = curr->subNode(ch);
if (child != nullptr) {
curr = child;
} else {
TrieNode *newNode = new TrieNode(ch);
curr->children.push_back(newNode);
curr = newNode;
}
++curr->shared;
}
curr->isend = true;
}