0

I've decided to implement the trie data structure and got everything working except the unloading part. I've written another function for it and it isn't working. I think it only frees the first few nodes but I'm not sure. Here's the main unload function:

    bool unload(void)
    {
         // Traversal pointer
         trie *liberator = root;

         if(root == NULL)
            return true;
         else
         {
            liberation(liberator);
         }

         return true;
    }

And here's the liberation function:

    void liberation(trie *liberator)
    {
        bool isLast = isEnd(liberator);

        if(isLast)
        {
            free(liberator);
        }
        else
        {
            for(int i = 0; i < 27; i++)
            {
                if(liberator->letters[i] != NULL)
                {
                   liberator = liberator->letters[i];
                   break;
                }
           }
           //Go further down the node
           liberation(liberator);
        }
    }

And finally the isLast function:

     bool isEnd(trie *theEnd)
     {
         for(int i = 0; i < 27; i++)
             if(theEnd->letters[i] != NULL)
                 return false;

         return true;
     }

Also, I any tips in making this a little more efficient would be helpful (I'm just lost as of now). Thanks

0

Your recursive call would need to go into the loop. Also, there's no need for an isEnd. Just free the current node after calling liberation for all child nodes that are not NULL.

Also, too much code. Your unload is essentially the same as

bool unload(void)
{
     if(root != NULL)
        liberation(root);
     return true;
}

You must log in to answer this question.

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