简体   繁体   English

二叉树析构函数C ++

[英]Binary tree destructor c++

I am pretty new to c++ and I am doing some exercises to learn it better. 我是C ++的新手,我正在做一些练习以更好地学习它。 However, I do not understand what is going on in the destructor of the proposed solution of a given task. 但是,我不了解给定任务的建议解决方案的析构函数中发生了什么。 I have tried to look online for an explanation, but not found one yet.. 我试图在网上寻找一种解释,但还没有找到。

This is the binary tree given in the exercise: 这是练习中给出的二叉树:

class Tree {
Tree *_left;
Tree *_right;
string _name;

public:
Tree(string name) : _left(NULL), _right(NULL), _name(name) {}
void insert(string name);
void insert(const Tree &tree);
friend ostream& operator<<(ostream&, const Tree&);
};

void Tree::insert(string name) {
Tree **destination = &_right;
if (name < _name) {
    destination = &_left;
}
if (*destination == NULL)
    (*destination) = new Tree(name);
else
    (*destination)->insert(name);
}

The exercise is to make a destructor for this class. 练习是为此类提供一个析构函数。 In every other example of binary trees I have found online, the destructor is implemented recursively in some way. 在网上找到的其他所有二叉树示例中,析构函数都以某种方式递归实现。 However the solution manual suggests this: 但是,解决方案手册建议这样做:

Tree::~Tree() {
delete _left;
delete _right;
}

How will this work? 这将如何运作? To me it looks like this destructor will only delete the left and right pointer of one node? 在我看来,这个析构函数只会删除一个节点的左右指针? Or is this destructor also in some way recursive? 还是这个析构函数也以某种方式递归?

delete -ing the left and right node calls their destructors. delete -ing左右节点调用其析构函数。 So all the resources are freed recursively. 因此,所有资源都以递归方式释放。

cppreference tells cppreference告诉

If expression is not a null pointer [...] the delete expression invokes the destructor 如果表达式不是空指针,则删除表达式将调用析构函数

Or from the current draft expr.delete#6 或者从当前草案expr.delete#6

If the value of the operand of the delete-expression is not a null pointer value and the selected deallocation function (see below) is not a destroying operator delete, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted. 如果delete-expression的操作数的值不是空指针值,并且所选的释放函数(参见下文)不是破坏运算符delete,则delete-expression将为对象或对象调用析构函数(如果有)。数组中要删除的元素。

When you call the delete operator on a pointer, the destructor of that object will be called. 当您在指针上调用delete运算符时,将调用该对象的析构函数。 So, in your case, the call will propagate down the binary tree. 因此,在您的情况下,该调用将沿二进制树传播。

Besides that, I would like to point out that your code is considered old C++ . 除此之外,我想指出您的代码被认为是旧的C ++ I understand you are still learning and this is only an exercise. 我了解您仍在学习,这只是一种练习。 But ideally, you should be using smart pointers like std::unique_ptr or std::shared_ptr to manage pointer ownership. 但理想情况下,您应该使用诸如std::unique_ptrstd::shared_ptr类的智能指针来管理指针所有权。 Those classes simplify the code because they destroy the pointed objects automatically when you don't need them anymore (eg, your object is destroyed and no other object points to those objects). 这些类简化了代码,因为它们在您不再需要指向的对象时会自动销毁它们(例如,您的对象被销毁,并且没有其他对象指向这些对象)。 There are some rules you have to understand when you work with those types of pointers (eg, circular dependency could be a problem), but they are really worth learning and using. 使用这些类型的指针时,您必须了解一些规则(例如,循环依赖可能会成为问题),但确实值得学习和使用。

Another tip I can give you is to stop using NULL and start using nullptr . 我可以给您的另一个提示是停止使用NULL并开始使用nullptr

If you want to learn more about these issues, there is a book called Effective Modern C++ by Scott Meyers that explains those points and much more. 如果您想了解更多有关这些问题的信息,请参阅Scott Meyers撰写的一本名为《 有效的现代C ++》的书,其中介绍了这些内容以及更多内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM