0

I've been struggling with freeing the nodes in pset6 for the past week. All other functions are completely ok.

This is my implementation of node:

typedef struct node
{
    bool is_word;
    struct node* children[27];
}
node;

I have tried to do something along the lines of the answer here https://cs50.stackexchange.com/questions/2462/unload-function-implementation , but I keep getting Segmentation faults OR when it does run ok, Valgrind shows that I have managed to free like 2 nodes maximum...

How can I solve that?

1
  • I removed the code but it is not deleted entirely; you can click the "edited ... mins/hours/days ago" link to see a full revision history of your post. Each revision has a link that says "source" which gives you the exact text that went into that revision. You can find your formatted code there. – Air Nov 18 '14 at 16:39
1

As said in the question https://cs50.stackexchange.com/questions/2462/unload-function-implementation, the main thing we have to take care of is:

Since each child is accessible only via its direct parent, you don't wanna lose (free) this parent until you make sure that all of its children were already freed.

We have many different kinds of traversals in a tree; for instance, inorder, postorder, preorder. So which traversal satisfies the above condition? Post order traversal.

Post order traversal is:

  1. Traverse the left subtree by recursively calling the post-order function.
  2. Traverse the right subtree by recursively calling the post-order function.
  3. Display the data part of element (current element).

So as we are deleting the tree, the only modification we have to do is:

  1. Traverse the left subtree by recursively calling the post-order function.
  2. Traverse the right subtree by recursively calling the post-order function.
  3. Free the data part of element (current element).

This Wikipedia page might help you become comfortable with unload.

You must log in to answer this question.

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