Skip to main content
Update code
Source Link

I am on pset5 and have completed the check and load functions. However, with the unload function I am having trouble understanding how to visualize it. As suggested in the walkthrough, I tried to implement the recursion method but I do not understand how that can work. So, we are on the root node with 27 elements in the children array. We point at one of the elements and check if they are NULL. If not, then we apply recursion. What I do not understand is how do we reach the children array of the first children element of the root node with recursion? When we apply recursion, to me, it seems that we keep checking if the same element of the root node is NULL or not but how does that help since we need to reach inside the children element of all the subsequent nodes and free them?

This is my code for the unload function. However, I keep running into an error with valgrind. It says that I have accessed 8 bytes of memory that is not mine.

void delete_node (node *trie)
{
  for (int i = 0; i < 27; i++)
  {
    if (trie -> children[i] != NULL)
    {
        delete_node(trie -> children[i]);
    }
  free(trie);
  }
}

bool unload(void)
{
  node * trav = root;
  delete_node(trav);
  return true;
}

Brief explanation of my code the way I get it. Please correct me if there is something wrong with it - Create a function to free nodes and run it through all the children of the node. If the children is not NULL, call it recursively till you reach a children element that is NULL. Then free it. (If not NULL, keep calling recursively, else if NULL, then free it). Call the function in unload and, using traversal pointer, free all the nodes and return true.

I am on pset5 and have completed the check and load functions. However, with the unload function I am having trouble understanding how to visualize it. As suggested in the walkthrough, I tried to implement the recursion method but I do not understand how that can work. So, we are on the root node with 27 elements in the children array. We point at one of the elements and check if they are NULL. If not, then we apply recursion. What I do not understand is how do we reach the children array of the first children element of the root node with recursion? When we apply recursion, to me, it seems that we keep checking if the same element of the root node is NULL or not but how does that help since we need to reach inside the children element of all the subsequent nodes and free them?

I am on pset5 and have completed the check and load functions. However, with the unload function I am having trouble understanding how to visualize it. As suggested in the walkthrough, I tried to implement the recursion method but I do not understand how that can work. So, we are on the root node with 27 elements in the children array. We point at one of the elements and check if they are NULL. If not, then we apply recursion. What I do not understand is how do we reach the children array of the first children element of the root node with recursion? When we apply recursion, to me, it seems that we keep checking if the same element of the root node is NULL or not but how does that help since we need to reach inside the children element of all the subsequent nodes and free them?

This is my code for the unload function. However, I keep running into an error with valgrind. It says that I have accessed 8 bytes of memory that is not mine.

void delete_node (node *trie)
{
  for (int i = 0; i < 27; i++)
  {
    if (trie -> children[i] != NULL)
    {
        delete_node(trie -> children[i]);
    }
  free(trie);
  }
}

bool unload(void)
{
  node * trav = root;
  delete_node(trav);
  return true;
}

Brief explanation of my code the way I get it. Please correct me if there is something wrong with it - Create a function to free nodes and run it through all the children of the node. If the children is not NULL, call it recursively till you reach a children element that is NULL. Then free it. (If not NULL, keep calling recursively, else if NULL, then free it). Call the function in unload and, using traversal pointer, free all the nodes and return true.

Source Link

I don't understand how a trie works. Please help

I am on pset5 and have completed the check and load functions. However, with the unload function I am having trouble understanding how to visualize it. As suggested in the walkthrough, I tried to implement the recursion method but I do not understand how that can work. So, we are on the root node with 27 elements in the children array. We point at one of the elements and check if they are NULL. If not, then we apply recursion. What I do not understand is how do we reach the children array of the first children element of the root node with recursion? When we apply recursion, to me, it seems that we keep checking if the same element of the root node is NULL or not but how does that help since we need to reach inside the children element of all the subsequent nodes and free them?