vendredi 22 mars 2019

How to completely delete a tree that has a variable number of children?

I have to delete a tree completely with variable number of children. After deleting I have to create a new object, to which my code stops working. Here's what I'm doing-

struct node {
    string data;
    vector<node*> child;
};

class lin
{
    vector<node*> pwd;
    node* root;

    public:

    lin()
    {   
        root = newNode(m);
        pwd.push_back(root);
    }

//other functions..
}

int main(){
    lin* obj = new lin();

    while(condition){
        delete obj;
        lin* obj = new lin(); 
    }
}

I even tried deleting nodes separately using destructor, but to no gain.

    ~lin(){
        cout<<"GONE"<<endl;
        int l = (root->child).size();
        for(int i=0;i<l;i++)
        {
            delete (root->child)[i];    
        }   
        delete root;
    }

Can someone please point out my mistake and suggest a better way to do it?

Aucun commentaire:

Enregistrer un commentaire