2016-10-29 4 views
0

So habe ich einige Beispiele von Bäumen gefunden und enteist, um sie für ein Spiel zu testen, das einen Baum aller Wörter im Wörterbuch enthält. Das Beispiel, das ich online fand, hatte keine Implementierung von freeTree, um Speicherlecks zu vermeiden, also versuche ich, mein eigenes zu erstellen. Allerdings habe ich seit einiger Zeit nicht mehr mit c gearbeitet und stoße auf Probleme.Einen Baum freilassen

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")]); 

dies ist ein einfacher im Lauftest und die Ergebnisse nach dem kostenlosen sind die gleiche wie vor

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 

hier ist die Struktur im

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

und die freien Implementierung mit

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); 
} 

Wh de ich erstelle einen neuen Baumknoten und malloc den Speicher dafür, damit ich weiß, dass ich frei machen muss.

Antwort

1
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; 
} 
+0

Danke für die Antwort funktioniert jetzt! Kommentare waren sehr klar und informativ danke! – user3267256

2

Ich denke, Ihr Problem ist, dieser Teil:

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

Nun, Sie müssen auf NULL befreien sie es dann eingestellt. Sonst verlierst du sowieso schon die Adresse.