二叉搜索树析构函数问题
Binary Search Tree Destructor issue
我目前正在将代码作为二叉搜索树的类代码,但是我的BST类的析构函数中出现错误。这是我的代码的相关部分:
节点结构:
struct Node{
int key;
struct Node* left;
struct Node* right;
};
创建新节点的函数:
Node* BST::CreateNode(int key){
Node* temp_node = new Node();
temp_node->key = key;
temp_node->left = nullptr;
temp_node->right = nullptr;
return temp_node;
}
赋值运算符:
BST& BST::operator=(const BST& cpy_bst){
if (this != &cpy_bst){
Node* cpy_root = cpy_bst.root;
this->root=assgRec(cpy_root, this->root);
}
return *this;
}
Node* BST::assgRec(Node* src_root, Node* dest_root){
if (src_root != nullptr){
dest_root = CreateNode(src_root->key);
dest_root->left=assgRec(src_root->left, dest_root->left);
dest_root->right=assgRec(src_root->right, dest_root->right);
}
return src_root;
}
破坏者:
BST::~BST(){
DestroyNode(root);
}
void BST::DestroyNode(Node* r){
if (r != nullptr){
DestroyNode(r->left);
DestroyNode(r->right);
delete r;
}
}
问题是在我在 main 函数中使用赋值之后,例如:
BST bin_tree2=bin_tree1;
析构函数被调用,但在它删除bin_tree1中的数据后,放置在bin_tree2中的所有值都包含一些垃圾值,我收到该部分的错误。任何帮助将不胜感激。谢谢
这
看起来像您正在复制指针并在解除分配内存后访问它们。
问题似乎不在于我之前所说的键,而在于 BST::assgRec 函数中似乎构造不正确的节点。
相关文章:
- 类中C++析构函数的 Xcode 问题
- 为什么我以前调用析构函数的方式会导致模因问题
- 二叉搜索树析构函数问题
- 与C++析构函数相比,C#析构函数和GC并没有真正解决问题
- C++二进制树递归析构函数问题
- 我不明白析构函数有什么问题?
- 创建'new'实例可以解决析构函数崩溃问题?
- c++析构函数解分配问题
- 为什么我的析构函数导致我的链表出现问题
- 析构函数问题 + 列表显示
- 崩溃,可能是由于构造函数或析构函数中的某些问题
- 通用二进制树节点析构函数问题
- 析构函数的 AVL 树内存问题
- 标量删除析构函数问题
- 在我的程序中"无符号"之前的预期主表达式(析构函数和构造函数问题)
- 未调用析构函数,程序异常退出时出现问题
- C++和析构函数(嵌套类和一般问题)
- 提升::p ython 受保护的析构函数问题
- c++的析构函数有问题
- c++问题析构函数