二进制搜索树析构函数c++
Binary Search Tree destructor c++
我正试图递归地创建一个二进制搜索树,但我在析构函数方面遇到了一些问题。我的BST基于一个使用的类命名为BSNode
private:
int _count;
string _data;
BSNode* _left;
BSNode* _right;
这是我当前的析构函数:
BSNode::~BSNode()
{
if (this == NULL)
return;
else if ((this->_left == NULL) && (this->_right == NULL)){
delete (this);
return;
}
else if (this->_left == NULL)
{
this->_right->~BSNode();
delete (this);
return;
}
else if (this->_right == NULL)
{
this->_left->~BSNode();
delete (this);
return;
}
else
{
this->_left->~BSNode();
this->_right->~BSNode();
delete (this);
return;
}
}
我有一个问题,过了一段时间(破坏类的"节点"),程序停止了,当我开始调试程序时,我发现当函数到达树的末尾时,它不会破坏节点,并不断获得相同的节点,就好像用相同的节点递归调用函数一样。我该怎么修?
这是我每次程序进入析构函数时得到的错误
我想你正在寻找更像这个的东西
BSNode::~BSNode()
{
delete(_left);
delete(_right);
}
~tree()
{
remove(root);
}
void remove(node* temp)
{
if (temp == NULL)
return;
remove(temp->left);
remove(temp->right);
delete temp;
}
相关文章:
- 什么时候调用组成单元对象的析构函数
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 内联映射初始化的动态atexit析构函数崩溃
- 什么时候调用析构函数
- 优先顺序:智能指针和类析构函数
- C++-明确何时以及如何调用析构函数
- 使用基类指针创建对象时,缺少派生类析构函数
- 在c++中使用向量时,如何调用构造函数和析构函数
- 重载运算符new[]的行为取决于析构函数
- 我需要知道编译器如何在cpp中使用析构函数
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 析构函数调用
- 通过引用传递-为什么要调用这个析构函数
- 对具有动态分配的内存和析构函数的类对象的引用
- 重载 -> shared_ptr 个实例中的箭头运算符<interface>,接口中没有纯虚拟析构函数
- C++成员的析构函数顺序与shared_ptr
- C++ 防止在映射中放置()时调用析构函数
- 在这种情况下显式调用时,std::cout 如何更改析构函数的行为?
- 调用析构函数以释放动态分配的内存
- 不命名构造函数和析构函数上的类型错误