0

I could not unload the dictionary, and still don't understand why. It seems that it is not freeing what I think. I would really appreciate some help, here is my code:

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // we re initialize cursor
    cursor = root;

    // base case
    if (root == NULL)
    {
        return true;
    }

    // iterate over each node
    seek_last();


    // and call again the funciotn
    unload();

    return false;

}


// Check every children of each node
void seek_last(void)
{
    // check to see if it is the last node
    bool is_last = true;

    int i;

    for (i = 0; i < N; i++)
    {
        // if not empty, move to that children and check new children
        if (cursor->children[i] != NULL)
        {
            // move tmp to next node
            cursor = cursor->children[i];
            // not the last
            is_last = false;
            // get our of the loop
            break;
        }
    }

    // if not last call itself
    if (is_last == false)
    {
        seek_last();
    }
    else
    {
        free(cursor);
        return;
    }

}
1

Your call to seek_last (by this name I'm not sure what it is supposed to do) will trigger a cascade of recursive calls that will eventually result in freeing one node.

You need to remember the path by which you got there, so you can continue with sibling nodes, and finally continue with the parent node once it no longer has children. The data structure for remembering that would be a stack (adding and removing elements at the same end of a list), and you can easily use the call stack, by adding a parameter to your recursive function. That parameter would be the current sub-trie to free, topmost call would pass the root pointer. You then would recursively unload all the children, then free the current sub-trie's root node (the node passed to the function).

3
  • Hi thanks for your answer. My function ´seek_last´ is supposed to find the last element of the tree, and then free that element, when I debug, I find the it does find the last element, but after "freeing" that node, nothing happens, when it is called again, it can reach again that element that I had freed. I do not know if it has something to do that I don't malloc any space for cursor
    – juancopi81
    Nov 19 '19 at 12:28
  • It finds and frees that element. But it does not remove it from the trie, that would require changing the pointer that got you there (that's not cursor, but where you copied its current value from). Also, this approach means you have to walk the same paths over and over, passing the root of a sub-trie to the recursive function would be more efficient.
    – Blauelf
    Nov 19 '19 at 13:25
  • Ah ok thanks, now I understood!! I was confused because when I implemented the hash table I did something similar (use a cursor to move in the linked list) and then freed that cursor and it worked. So I implemented what you told me "passing the root of a sub-trie to the recursive function would be more efficient" and now it is working
    – juancopi81
    Nov 19 '19 at 13:52

You must log in to answer this question.

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