Commit 7ab3be6b53c797db7d18d6f61d34534b0fd947a4
1 parent
03168857
debut free
Showing
4 changed files
with
31 additions
and
66 deletions
Show diff stats
... | ... | @@ -13,9 +13,12 @@ int main() |
13 | 13 | tab[i]=NULL; |
14 | 14 | } |
15 | 15 | |
16 | - load_tree(fp,tab);//supprimer la case | |
16 | + //supprimer la case avant de load | |
17 | + load_tree(fp,tab);//chargement de l'arbre | |
17 | 18 | |
18 | 19 | printf("Loading done!\n"); |
20 | + | |
21 | + free_dico(tab);//libérer la mémoire | |
19 | 22 | |
20 | 23 | return 0; |
21 | 24 | } | ... | ... |
1 | 1 | #include "tree.h" |
2 | 2 | |
3 | -/* | |
4 | -typedef struct node { | |
5 | - int val; | |
6 | - int fin; | |
7 | - struct node* fils[]; | |
8 | - int nbr_fils; | |
9 | -}Node, *PtNode, *Tree; | |
10 | - | |
11 | -#include <stdio.h> | |
12 | -#include <stdlib.h> | |
13 | - | |
14 | -int main(){ | |
15 | - int tmp; | |
16 | - tmp = getchar(); | |
17 | - printf("%d\n",tmp); | |
18 | - return 0; | |
19 | -} | |
20 | - | |
21 | - */ | |
22 | - | |
23 | 3 | void cons_tree(struct node ** ptr_tree, int val) |
24 | 4 | { |
25 | 5 | *ptr_tree = malloc(sizeof(struct node)); |
... | ... | @@ -28,9 +8,6 @@ void cons_tree(struct node ** ptr_tree, int val) |
28 | 8 | (*ptr_tree)->nbr_fils=0; |
29 | 9 | (*ptr_tree)->fils = malloc(sizeof(struct node*)); |
30 | 10 | (*ptr_tree)->fils[0]=NULL; |
31 | - | |
32 | - //(*ptr_tree)->fils = realloc(sizeof(struct node*)*nbr_fils); | |
33 | - //(*ptr_tree)->fils[nbr_fils]; | |
34 | 11 | } |
35 | 12 | |
36 | 13 | void mk_empty_tree(struct node **ptr_tree) |
... | ... | @@ -43,45 +20,6 @@ int is_leaf(struct node *tree) |
43 | 20 | return tree->fin||tree->fils[0]==NULL; |
44 | 21 | } |
45 | 22 | |
46 | -/*void add(struct node **tab_ptr_tree, char val[],int taille, int fl) | |
47 | -{ | |
48 | - Node* node = tab_ptr_tree[fl]; | |
49 | - for(int i=0;i<taille;i++) | |
50 | - { | |
51 | - if(node==NULL){ | |
52 | - node = malloc(sizeof(struct node)); | |
53 | - cons_tree(&node,val[i]); | |
54 | - printf("%d",node->val); | |
55 | - tab_ptr_tree[fl] = node; | |
56 | - continue; | |
57 | - | |
58 | - } | |
59 | - int trouve = -1; | |
60 | - for(int j=0;j<(node)->nbr_fils;j++) | |
61 | - { | |
62 | - if((node)->fils[j]->val==val[i]) | |
63 | - { | |
64 | - trouve=j; | |
65 | - break; | |
66 | - } | |
67 | - } | |
68 | - if(trouve == -1) | |
69 | - {//ajouter fils | |
70 | - (node)->nbr_fils++; | |
71 | - (node)->fils = realloc((node)->fils,((node)->nbr_fils)*sizeof(struct node*)); | |
72 | - cons_tree(&((node)->fils[((node)->nbr_fils)-1]),val[i]); | |
73 | - trouve = 0; | |
74 | - } | |
75 | - node = (node)->fils[trouve]; | |
76 | - if(i==taille-1) | |
77 | - { | |
78 | - (node)->fin=1; | |
79 | - } | |
80 | - } | |
81 | - //mettre fin à 1 pour le bon | |
82 | - //if(i==taille-1)(*(tab_ptr_tree[fl]))->fin=1; // | |
83 | - }*/ | |
84 | - | |
85 | 23 | void add(struct node **tab_ptr_tree, char val[],int taille, int fl) |
86 | 24 | { |
87 | 25 | if(tab_ptr_tree[fl]==NULL)cons_tree(&(tab_ptr_tree[fl]),val[0]+97); |
... | ... | @@ -137,7 +75,23 @@ void load_tree(FILE *fp, struct node **tab_ptr_tree) |
137 | 75 | if(ferror(fp)) printf("ERREUR de lecture\n"); |
138 | 76 | } |
139 | 77 | |
140 | -void free_tree(struct node **ptr_tree) | |
78 | +void free_tree(struct node *ptr_tree) | |
141 | 79 | { |
142 | - | |
80 | + for(int i=0;i<ptr_tree->nbr_fils;i++) | |
81 | + { | |
82 | + if(ptr_tree->fils[i]->nbr_fils!=0)free_tree(ptr_tree->fils[i]); | |
83 | + free(ptr_tree->fils[i]); | |
84 | + } | |
85 | + //free(ptr_tree); | |
86 | +} | |
87 | + | |
88 | +void free_dico(struct node **tab_ptr_tree) | |
89 | +{ | |
90 | + for(int i=0;i<26;i++) | |
91 | + { | |
92 | + if(tab_ptr_tree[i]!=NULL) | |
93 | + { | |
94 | + free_tree(tab_ptr_tree[i]); | |
95 | + } | |
96 | + } | |
143 | 97 | } | ... | ... |