【发布时间】:2017-06-06 09:15:26
【问题描述】:
我想插入一个字符串并使用 trie 数据结构进行搜索操作。这是我第一次使用指针实现,所以我真的很困惑我在代码中做错了什么,它给出了编译错误。请帮助调试请告诉我我的指针逻辑有什么问题。
typedef struct trie {
unordered_multimap<char, struct trie> child;
bool isEnd;
} trie;
trie* newtrienode()
{
trie* newnode = (trie*)malloc(sizeof(trie));
newnode->isEnd = false;
return newnode;
}
trie* root = newtrienode();
void insert(string word)
{
trie* current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word[i];
trie* node = current->child[ch];
if (node == NULL) {
trie* node = newtrienode();
current->child.insert(pair<char, trie>(ch, node));
}
current = node;
}
current->isEnd = true;
}
bool search(string word)
{
trie* current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word[i];
trie* node = current->child[ch];
if (node == NULL) {
return false;
}
current = node;
}
return true;
}
【问题讨论】:
标签: c++ algorithm data-structures hashmap trie