0

I am really struggling with the unload function in pset5 :( I have implemented a trie to load the dictionary, and my load and check functions work fine.

But somehow, the recursive structure needed to free all nodes in the dictionary is just beyond my grasp. Extremely frustrating. I have looked at other advice on these forums but I just can't seem to write a successful unload.

My code is below, please could someone help me figure out where I'm going wrong? Note here that destroy is a function I have called inside of unload, in order to be able to pass it the traversal pointer.

void destroy(node *trav)

{
   int i = 0;

    // if the path is NULL
    if (trav -> path[i] == NULL)
    {
        free(trav);
    }

   else
   {
        // starting with the first path
        for (i = 0; i < 27; i++)
        {
            // so long as a path exists
            while (trav -> path[i] != NULL)
            {
                // go down it
                trav = trav -> path[i];
                // then destroy the current node and restart loop
                destroy(trav);
            }
        }
    }
1

You free the node even if it has children. You should first destroy any non-NULL children, and then free the current node. No while loop required, just a single loop over all the children, calling destroy on the non-NULL ones.

You must log in to answer this question.

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