简体   繁体   中英

Destructor for Binary Search Tree

I am trying to write the destructor for my Binary Search Tree and I know how to recursively loop through the tree, but I do not know how to do that in the destructor so that every node is deleted.

My Header is:

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;
};

My constructor is:

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

Is there a way to call the destructor recursively? If not, how do I traverse through every node to delete it.

void Tree::DestroyRecursive(TreePtr node)
{
    if (node)
    {
        DestroyRecursive(node->left);
        DestroyRecursive(node->right);
        delete node;
    }
}

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

You need two destructors:

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

and

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

But you don't really need two classes here. Every Node is a tree.

When you call delete or your Tree goes to end of lifetime (exit from a block, like the example at the end), you have to delete the Tree children Node s and the delete operator will call the destructor, see example at the end.

This will make the Tree disappear entirely from memory when the Tree destructor is called.

Just try this:

#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))));
}

Here is my implementation. A Tree equals a TreeNode.

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

Simply delete the root node of the tree, then the whole tree will be deleted recursively.

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

You may already know what a delete do.

When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor).

So, in a treeNode's destructor, you only need to destroy the left and right pointers that are manually allocated by you. You don't need to worry about the deallocation of the node itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM