试图删除二进制树C
Trying to delete binary tree C++
我正在尝试在C 中删除二进制树,但是我有一个问题,因为树的大小似乎没有变化。这是我正在使用的尺寸函数:
int BST::size(Node *& cur_root)
{
if (cur_root == NULL) {
return 0;
} else {
return(size(cur_root->m_left) + 1 + size(cur_root->m_right));
}
}
这是我试图在:
中使用它的功能void BST::deletetree(Node *& cur_root)
{
cout << "tree size: " << size() << endl;
if (cur_root!=NULL)
{
deletetree(cur_root->m_left);
deletetree(cur_root->m_right);
delete cur_root;
if(cur_root->m_left != NULL) {
cur_root->m_left = NULL;
}
if(cur_root->m_right != NULL) {
cur_root->m_right = NULL;
}
cur_root=NULL;
}
}
对于三(1、2、3(的树大小,我的输出是:
tree size: 3
tree size: 3
tree size: 3
tree size: 3
tree size: 3
tree size: 3
tree size: 3
有人知道为什么每个删除的节点都不会下降我的大小?
编辑:我删除了if语句,但是问题仍然存在
void BST::deletetree(Node *& cur_root)
{
cout << "tree size: " << size() << endl;
if (cur_root!=NULL)
{
deletetree(cur_root->m_left);
deletetree(cur_root->m_right);
delete cur_root;
cur_root=NULL;
}
}
编辑2:这是我的完整代码。BST.H:
#ifndef BST_H
#define BST_H
#include <iostream>
using namespace std;
class BST
{
public:
BST();
bool insert(string str) {return insert(str, m_root);}
int size() {return size(m_root);}
void deletetree() {return deletetree(m_root);}
private:
class Node
{
public:
Node(string value, Node *left = NULL, Node *right = NULL)
{m_value = value; m_left = left; m_right = right;}
string m_value;
Node *m_left;
Node *m_right;
};
Node *m_root;
bool insert(string str, Node *& cur_root);
int size(Node *& cur_root);
void deletetree(Node *& cur_root);
};
#endif
bst.cpp:
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
#include <queue>
#include <math.h>
#include "bst.h"
BST::BST()
{
m_root = NULL;
}
bool BST::insert(string str, Node *& cur_root)
{
/*if (find(str) == true) {
return false;
}*/
if (cur_root == NULL) {
cur_root = new Node(str);
return true;
} else {
if (cur_root->m_value < str) {
return insert(str, cur_root->m_right);
} else {
return insert(str, cur_root->m_left);
}
}
}
int BST::size(Node *& cur_root)
{
if (cur_root == NULL) {
return 0;
} else {
return(size(cur_root->m_left) + 1 + size(cur_root->m_right));
}
}
void BST::deletetree(Node *& cur_root)
{
cout << "tree size: " << size() << endl;
if (cur_root!=NULL)
{
deletetree(cur_root->m_left);
deletetree(cur_root->m_right);
delete cur_root;
cur_root=NULL;
}
}
main.cpp:
#include <iostream>
using namespace std;
#include "bst.h"
int main()
{
BST tree;
tree.insert("1");
tree.insert("2");
tree.insert("3");
tree.deletetree();
}
忽略有关插入的评论部分。这是我稍后将要实现的问题。
让我们从您的deletetree
函数中查看这些行:
delete cur_root;
if(cur_root->m_left != NULL) {
cur_root->m_left = NULL;
}
if(cur_root->m_right != NULL) {
cur_root->m_right = NULL;
}
第一行破坏了对象由cur_root
指向的对象,其余行放证了访问现在破坏对象的指针。
删除指向破坏对象的指针导致不确定的行为,这使所有关于行为的猜测无用。
简单的解决方案是不做if
检查,因为根本不需要它们。您需要的是delete cur_root
,其次是cur_root = nullptr
。
现在,当我们可以看到您的完整代码时,我们可以看到您的插入功能不会创建树。它创建 list (用您显示的顺序插入数据(。
插入"1"
时,它将成为树的根。然后,当您插入"2"
时,它将变为m_root->m_right
。然后,当您插入"3"
时,它将变为m_root->m_right->m_right
。我还没有踏上您的代码,以了解它是否导致deletetree
的size
行为不对,但其中之一是这样做的。您应该使用调试器找出哪些功能会导致您的明显问题。
然后,您应该考虑将节点插入树上的顺序。或者,如果您的树应变得自相平衡并在插入上重新排序节点。
相关文章:
- 试图删除二进制树C
- C 试图删除二进制树并将其移至矢量
- 在二进制文件中删除记录
- 在二进制搜索树中删除节点
- 如何从二进制搜索树中删除节点
- AVL二进制树旋转和删除树的C 问题
- 从C++二进制搜索树中删除一个节点(类而非结构)
- 无法从二进制搜索树中删除叶子
- 二进制树搜索删除节点复杂性
- 无法从我创建的 c 二进制程序中删除记录
- 原始二进制树删除与复制问题(改进解释)
- C++:使用std::ifstream读取二进制文件后删除缓冲区/指针时发生访问冲突
- 二进制搜索树删除而无需复制
- fstream从二进制文件的末尾删除n个字节
- 很难从我的二进制搜索树中删除一个节点
- 二进制搜索树 - 删除节点,没有指向前任的指针
- 从二进制搜索树中删除只有一个孩子的节点
- 二进制搜索树C删除节点一些错误
- 将二进制文件加载到eigen矩阵中时,请删除潜在的冗余模板参数
- C++二进制搜索树删除问题