二进制搜索树的析构函数

问题描述 投票:2回答:4

我正在尝试为二进制搜索树编写析构函数,我知道如何递归遍历树,但我不知道如何在析构函数中执行此操作,以便删除每个节点。

我的标题是:

struct Node;
typedef string TreeType;
typedef Node * TreePtr;

//Defines a Node
struct Node
{
    TreeType Info;
    int countDuplicates = 1;
    TreePtr Left, Right;
};

class Tree
{
public:
    //Constructor
    Tree();

    //Destructor
    ~Tree();

    //Retruns true if the tree is Empty
    bool Empty();

    //Inserts a Node into the tree
    bool Insert(TreeType);

    //Delete decides what type of delection needs to occur, then calls the correct Delete function
    bool Delete(Node * , Node * );

    //Deletes a leaf node from the tree
    bool DeleteLeaf(TreePtr, TreePtr);

    //Deletes a two child node from the tree
    bool DeleteTwoChild(TreePtr);

    //Deletes a one child node from the tree
    bool DeleteOneChild(TreePtr, TreePtr);

    //Finds a certain node in the tree
    bool Find(TreeType);

    //Calculates the height of the tree
    int Height(TreePtr);

    //Keeps a count of the nodes currently in the tree;
    void Counter();

private:

    //Prints the nodes to the output text file in order alphabetically
    void InOrder(ofstream &,TreePtr);

    //Defines a TreePtr called Root
    TreePtr Root;

    //Defines a TreePtr called Current
    TreePtr Current;

    //Defines a TreePtr called Parent
    TreePtr Parent;
};

我的构造函数是:

Tree::Tree()
{
    Root = NULL;
    Current = NULL;
    Parent = NULL;
}

有没有办法递归调用析构函数?如果没有,我如何遍历每个节点以删除它。

c++ destructor
4个回答
5
投票
void Tree::DestroyRecursive(TreePtr node)
{
    if (node)
    {
        DestroyRecursive(node->left);
        DestroyRecursive(node->right);
        delete node;
    }
}

Tree::~Tree()
{
    DestroyRecursive(Root);
}

3
投票

你需要两个析构函数:

Tree::~Tree()
{
    delete Root;
}

Node::~Node()
{
    delete Left;
    delete Right;
}

但是你真的不需要两节课。每个Node都是一棵树。


1
投票

当你调用delete或你的Tree进入生命周期结束时(从一个块退出,如结尾的例子),你必须delete Tree孩子Node s和delete操作符将调用析构函数,最后看示例。

当调用Tree析构函数时,这将使Tree完全从内存中消失。

试试这个:

#include <iostream>
using namespace std;

class Node {
    Node *left, *right;
public:
    Node(Node *l, Node *r);
    ~Node();
};

class Tree {
    Node *root;
public:
    Tree(Node *rt);
    ~Tree();
};

Tree::Tree(Node *rt):root(rt) {
    cout << "new Tree with root node at " << rt << endl;
}
Tree::~Tree() {
    cout << "Destructor of Tree" << endl;
    if (root) delete root;
}
Node::Node(Node *l, Node *r):left(l), right(r) {
    cout << "Node@"
        << this
        << "(left:" << l
        << ", right:" << r << ")"
        << endl;
}   
Node::~Node() {
    cout << "~Node@" << this 
        << endl;
    if (left) delete left;
    if (right) delete right;
}

int main() {
    Tree t(
        new Node(
            new Node(
                new Node(
                    new Node(0, 0), 
                    0),
                0),
            new Node(0, new Node(0, 0))));
}

0
投票

这是我的实施。树等于TreeNode。

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    ~TreeNode() {
        delete left;
        delete right;
    }
    ...
};

只需删除树的根节点,然后递归删除整个树。

TreeNode* root = new TreeNode(2);
delete root;

您可能已经知道删除操作的内容。

当delete用于为C ++类对象释放内存时,在释放对象的内存之前调用对象的析构函数(如果对象具有析构函数)。

因此,在treeNode的析构函数中,您只需要销毁由您手动分配的左右指针。您无需担心节点本身的重新分配。

热门问题
推荐问题
最新问题