0

im close to finish this problem set , but i cannot figure out the working recursive function, especially if backtracking may work in this form of implementation:

void rec_unload(node *trv)

{

 for(int m =0 ; m<27 ; m++)
{
    if(trv ->children[m] == NULL)
    {
        free(trv ->children[m]);
    }
    else
    {
         rec_unload(trv->children[m]) ;
    }
}

}

I appreciate any effort to look into my issue.

2

Think carefully about what the code is doing. It checks if a pointer is null, and if so, it will free that node. In other words, it will free a nonexistent node - a do nothing operation. OR, if the node is not null, it will go on to process it's children, doing the same thing. BUT, on return, the node isn't freed. In other words, nothing is ever freed.

The code needs to free everything. So, it needs to check if a child is not null. If it isn't, then it needs to do the recursive call. On return, the node itself then needs to be freed. Remember, all the children have to be freed first, and then the parent has to be freed.

If this answers your question, please click on the check mark. Let's keep up on forum maintenance. ;-)

1
  • Really thanks for advice, it sparked right idea in matter of minutes, function frees now all the nodes except root, which is easily freed by other unique call.
    – Jakub
    May 16 '18 at 8:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .