试图删除二进制树C

Trying to delete binary tree C++

本文关键字:二进制 删除      更新时间:2023-10-16

我正在尝试在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。我还没有踏上您的代码,以了解它是否导致deletetreesize行为不对,但其中之一是这样做的。您应该使用调试器找出哪些功能会导致您的明显问题。

然后,您应该考虑将节点插入树上的顺序。或者,如果您的树应变得自相平衡并在插入上重新排序节点。