gpt4 book ai didi

c++ - 如何为 Trie 树编写析构函数

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:45 26 4
gpt4 key购买 nike

我一直在做一个作业,它让我们使用 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::vectorshared_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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com