简体   繁体   中英

Freeing a trie tree

So i found some examples of trie trees on line and deiced to try them out to help for a game im working on that will contain a trie tree of all the words in the dictionary. The example i found on line did not have any implementation of a freeTree to avoid memory leaks so i am trying to make my own. However I have not worked with c in a while and am running into problems.

char keys[][8] = {"the", "a", "there", "answer", "any",
                 "by", "bye", "their"};
char output[][32] = {"Not present in trie", "Present in trie"};
struct TrieNode *root = getNode();

// Construct trie
int i;
for (i = 0; i < ARRAY_SIZE(keys); i++){
    insert(root, keys[i]);
}

// Search for different keys
printf("%s --- %s\n", "the", output[search(root, "the")] );
printf("%s --- %s\n", "these", output[search(root, "these")] );
printf("%s --- %s\n", "their", output[search(root, "their")] );
printf("%s --- %s\n", "thaw", output[search(root, "thaw")] );

freeTree(root);

printf("test after free\n");
printf("%s --- %s\n", "the", output[search(root, "the")] );
printf("%s --- %s\n", "these", output[search(root, "these")] );
printf("%s --- %s\n", "their", output[search(root, "their")] );
printf("%s --- %s\n", "thaw", output[search(root, "thaw")] );

this is a simple test im running and the results after the free are the same as before

the --- Present in trie
these --- Not present in trie
their --- Present in trie
thaw --- Not present in trie
test after free
the --- Present in trie
these --- Not present in trie
their --- Present in trie
thaw --- Not present in trie

here is the structure im using

struct TrieNode
{
    struct TrieNode *children[ALPHABET_SIZE];
    bool isLeaf;
};

and the free implementation

void freeChildren(struct TrieNode *node){
    int i;
    if (node->isLeaf == false){
      for (i=0;i<ALPHABET_SIZE;i++){
         if (node->children[i] != NULL){
            freeChildren(node->children[i]);
         }
      }
    }

   if (node != NULL){
      node = NULL;
      free(node);
   }
}

void freeTree(struct TrieNode *root){
  freeChildren(root);
  free(root);
}

When i create a new tree node i malloc the memory for it so i know that i need to free.

I think your problem is this part:

if (node != NULL){
  node = NULL;
  free(node);
}

Well, you need to free it then set it to NULL. Otherwise you already lose the address anyway.

void freeChildren(struct TrieNode *node){
    int i;
    if (node != NULL && node->isLeaf == false){//NULL check important only for the root
      for (i=0;i<ALPHABET_SIZE;i++){
         if (node->children[i] != NULL){
            freeChildren(node->children[i]);
            node->children[i] = NULL]; //you have to remove the address, otherwise it stays, just is no longer allocated (but it is still possible to access it, though it might crash or do whatever else)
         }
      }
    }

    if (node != NULL){ //again only root could be NULL
      free(node);//this has to go first
      //node = NULL; this has no meaning, as this is the local variable, not the place you passed it to
   }
}

void freeTree(struct TrieNode *root){
  freeChildren(root);
  // free(root); this is already done in the freeChildren
  // you might want to realloc the root there though, as otherwise you don't have anything allocated to root
  root = NULL;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM