- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在做一个作业,它让我们使用 trie 树将字典中的单词添加到树中,然后在其中进行搜索。我坚持的部分是类析构函数。这是我第一次不得不处理析构函数/内存管理,下面是我搜索目前拥有的资源后的最佳猜测。
我走在正确的轨道上吗?每个节点都有 27 个子节点(字母表和一个分隔符),所以我希望它能从叶节点到根节点全部删除它们。
class Trie
{
private:
TrieNode *_root = nullptr;
TrieNode *_current = nullptr;
TrieNode *_child = nullptr;
string current_word;
bool setRoot = false;
protected:
public:
Trie()
{
_root = new TrieNode{};
}
virtual ~Trie()
{
//TODO: clean up memory
DestroyRecursive(_root);
}
void Trie::DestroyRecursive(TrieNode* node)
{
if (node != nullptr)
{
for (TrieNode* child : node->getChildren())
{
delete(child);
}
}
}
如何检查析构函数是否正常工作?我正在使用 Visual Studio。
最佳答案
你的 DestroyRecursive
实际上不是递归的
您需要在叶节点上调用delete
,并在有子节点的节点上递归。
void Trie::DestroyRecursive(TrieNode* node)
{
if (node != nullptr)
{
if (node->getChildren().size() == 0)
{
// delete the leaf node
delete(node);
return;
}
for (TrieNode* child : node->getChildren())
{
DestroyRecursive(child);
}
}
}
这可能会出错,具体取决于对您的TrieNode
结构的依赖性。例如,它是否有一个非平凡的析构函数?
通过将原始指针替换为 std::shared_ptr 可以避免很多这样的情况。
std::shared_ptr<TrieNode> _root = nullptr;
vector<shared_ptr<TrieNode>> _child = nullptr;
Trie()
{
_root = std::make_shared<TrieNode>();
}
然后在大多数情况下你不需要析构函数。 std::vector
和 shared_ptr
将负责在超出范围时在适当的内存上调用 delete
。请注意,所有权没有循环依赖性。如果以后添加父指针,它必须是原始指针或 std::weak_ptr。
How can I check if a destructor is working properly? I'm using Visual Studio.
你可以放一个breakpoint检查代码是否被命中。
关于c++ - 如何为 Trie 树编写析构函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49164346/
我正在尝试做的事情: 构建一个移动网络应用程序,用户可以在玩拼字游戏时获得帮助找到要玩的单词 用户通过输入任意数量的字母和 0 个或多个通配符来获得单词建议 我是如何尝试做到这一点的: 使用 MySQ
假设我有一个 trie 包含多个字符串的数据结构。要在 trie 中查找字符串,我会从根开始,然后按顺序跟随标有字符串适当字符的指针,直到到达给定节点。 现在假设我想为同一组字符串构建一个“反向 tr
我正在阅读 Ingersoll、Morton 和 Farris 撰写的 Taming Text,但我不明白 solr 的数字 trie 实现如何帮助搜索文本?我对 solr.TrieField fie
我正在阅读 Ingersoll、Morton 和 Farris 的 Taming Text,但我不明白 solr 的数字 trie 实现如何帮助搜索文本?我对 solr 的 solr.TrieFiel
我正在开发一个 Trie 数据结构,其中每个节点代表一个词。所以词 st, stack, stackoverflow 和 overflow 将被排列为 root --st ---stack -----
trie 和radix trie 数据结构是一回事吗? 如果它们不相同,那么 radix trie (AKA Patricia trie) 是什么意思? 最佳答案 基数树是 trie 的压缩版本。在
我用过 video理解前缀特里树(虽然最终我试图最终得到后缀特里树)但是示例代码的链接被破坏所以我从视频中想出了这个,有两个功能,即插入和搜索如下 void insert(string word)
本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,208,Python, C++, Java 题目地址:https://leetcode.com/problems/implement
这是我使用 trie.c 和 trie.h 制作的主文件。该程序的目的是存储字典文件中的单词。 node* x = (node*)malloc(sizeof(node)); x = insert("b
因此,我必须搜索缺少字母(用于填字游戏)的单词,并且还必须维护剩余空格的可能单词列表。 现在我的问题是,我已经用谷歌搜索了 burst-trie 是否是最快的搜索算法。但是,如果我在 trie 中编写
我正在尝试将这个 trie 实现用于 ocaml:http://www.lri.fr/~filliatr/ftp/ocaml/ds/trie.ml.html 这是我对模块“M”的实现: module
我试图将 trie 中的所有单词放入字符串中,单词由 eow 字段表示,对于 trie 数据结构中的某个字符为 true,因此 trie 可以有字母但没有单词,例如“abc”在 trie 中,但“c”
我不太了解尝试字符串匹配中使用的实际算法。 我想知道为什么似乎更关注字符串匹配的后缀尝试而不是前缀尝试。我们可以不使用前缀尝试来进行子字符串匹配吗?换句话说,后缀尝试相对于前缀尝试有什么优势? 最佳答
当你构建一个特里树时,你是否将字符串/句子存储在其分支的末尾,以便在分支的末尾轻松访问它?有些人这样做,我有时也这样做,但我应该这样做吗? 有时(尤其是使用 LeetCode),我会收到此错误: Li
鉴于以下情况... (def inTree '((1 2) (1 2 3) (1 2 4 5 9) (1 2 4 10 15) (1 2 4 20 25))) 你如何将它转换
我想为高棉语(一种单词之间没有空格的语言)添加一个开源 Java 单词分割程序。开发人员已经很长时间没有开发它了,我无法联系他们了解详细信息(http://sourceforge.net/projec
我有一个字典文件(仅包含小写字母和撇号的单词),它作为特里树加载。 我有一个检查函数,它检查文件中的单词是否存在于特里树中树,无论字母大小写。 一切正常,除了撇号的单词总是拼写错误。 这是我的函数 b
我一直在练习 trie 数据结构(与类(class)作业无关)。此类用于存储字符串的子字符串。对于长度为 n 的字符串,总共有 n(n+1)/2 个子字符串。特别是 trie 的这种实现保留了自然顺序
所以我创建了一个包含大量数据的 trie,我的搜索算法非常快,但我想看看是否有人知道我如何才能让它更快。 bool search (string word) { int wordLen
我正在尝试用 C++ 实现 Trie,但出现运行时错误... 这是我的代码: #include using namespace std; struct trie{ bool word = f
我是一名优秀的程序员,十分优秀!