正在尝试删除二叉树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)); } }

正在尝试删除二叉树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)); } },c++,binary-tree,C++,Binary Tree,这就是我试图使用它的函数: 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

这就是我试图使用它的函数:

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;
    }
}
有人知道为什么我的尺寸不会随着每个删除的节点而下降吗

编辑:我删除了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;
    }
}
void BST::deletetree(节点*&cur\u根)
{
cout m_值m\u right);
}否则{
返回插入(str,cur_root->m_left);
}
}
}
int BST::size(节点*&cur\u根)
{
if(cur_root==NULL){
返回0;
}否则{
返回(大小(cur_root->m_left)+1+大小(cur_root->m_right));
}
}
void BST::deletetree(节点*&cur_根)
{

cout让我们看看
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\u root
,后跟
cur\u root=nullptr


现在,当我们看到完整的代码时,我们可以看到插入函数并没有创建树,而是创建了一个列表(按照显示的顺序插入数据)

当您插入
“1”
时,它将成为树的根。然后当您插入
“2”
时,它将成为
m_root->m_right
。然后当您插入
“3”
它变成了
m_root->m_right->m_right
。我还没有仔细检查您的代码,以了解它是否导致
deleteteTree
大小
出现错误,但其中一个出现了错误。您应该使用调试器找出导致您出现明显问题的函数


然后,您应该考虑将节点插入到树中的顺序。或者您的树是否应该成为自平衡的,并在插入时对节点重新排序。

您正在打印返回值的size()方法不能与您发布实现的方法相同——您发布的方法使用一个参数,但您正在调用size()没有参数。在delete函数中,您在删除
cur\u root
后立即取消对它的引用。
delete cur\u root;
之后的所有内容都是完全不必要的。@TanveerBadar除了
curr\u root=NULL
。另外,在您的问题中提供一些信息。我尝试过这样做,但大小一直保持在3。@ErikIngvoldsen我们有足够的代码来复制您的问题。@ErikIngvoldsen然后是您创建一个并向我们展示的时候了。您真的应该从一开始就做一些事情。我…不知道我还可以包括什么。有一个基本的节点对象,它包含字符串和左/右以及树的结构(root=NULL),但这基本上就是全部代码。@ErikIngvoldsen请告诉我们如何创建树,以及如何调用
deletetree
函数。最好是我们自己复制代码并复制行为。
#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
#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;
    }
}
#include <iostream>
using namespace std;
#include "bst.h"

int main()
{
    BST tree;
    tree.insert("1");
    tree.insert("2");
    tree.insert("3");
    tree.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;
}