Commit 03168857d03d3bf45e6625f69759189e0f5eb8ed

Authored by mertz
1 parent 541fd894

create tree op

Showing 3 changed files with 66 additions and 15 deletions   Show diff stats
main.c 0 → 100644
@@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
  1 +#include "tree.h"
  2 +
  3 +int main()
  4 +{
  5 + FILE* fp = fopen("american-english","r");
  6 + //FILE* fp = fopen("dico_test","r");
  7 +
  8 + if(fp == NULL) return 1 ; //File is not readable
  9 +
  10 + struct node* tab[26];
  11 + for(int i=0;i<26;i++)
  12 + {
  13 + tab[i]=NULL;
  14 + }
  15 +
  16 + load_tree(fp,tab);//supprimer la case
  17 +
  18 + printf("Loading done!\n");
  19 +
  20 + return 0;
  21 +}
@@ -43,20 +43,23 @@ int is_leaf(struct node *tree) @@ -43,20 +43,23 @@ int is_leaf(struct node *tree)
43 return tree->fin||tree->fils[0]==NULL; 43 return tree->fin||tree->fils[0]==NULL;
44 } 44 }
45 45
46 -void add(struct node ***tab_ptr_tree, char val[],int taille, int fl) 46 +/*void add(struct node **tab_ptr_tree, char val[],int taille, int fl)
47 { 47 {
48 - Node** node = tab_ptr_tree[fl]; 48 + Node* node = tab_ptr_tree[fl];
49 for(int i=0;i<taille;i++) 49 for(int i=0;i<taille;i++)
50 { 50 {
51 if(node==NULL){ 51 if(node==NULL){
52 node = malloc(sizeof(struct node)); 52 node = malloc(sizeof(struct node));
53 - cons_tree(node,val[i]);continue;  
54 - printf("%d",(*node)->val); 53 + cons_tree(&node,val[i]);
  54 + printf("%d",node->val);
  55 + tab_ptr_tree[fl] = node;
  56 + continue;
  57 +
55 } 58 }
56 int trouve = -1; 59 int trouve = -1;
57 - for(int j=0;j<(*node)->nbr_fils;j++) 60 + for(int j=0;j<(node)->nbr_fils;j++)
58 { 61 {
59 - if((*node)->fils[j]->val==val[i]) 62 + if((node)->fils[j]->val==val[i])
60 { 63 {
61 trouve=j; 64 trouve=j;
62 break; 65 break;
@@ -64,19 +67,45 @@ void add(struct node ***tab_ptr_tree, char val[],int taille, int fl) @@ -64,19 +67,45 @@ void add(struct node ***tab_ptr_tree, char val[],int taille, int fl)
64 } 67 }
65 if(trouve == -1) 68 if(trouve == -1)
66 {//ajouter fils 69 {//ajouter fils
67 - (*node)->nbr_fils++;  
68 - (*node)->fils = realloc((*node)->fils,((*node)->nbr_fils)*sizeof(struct node*));  
69 - cons_tree(&((*node)->fils[((*node)->nbr_fils)-1]),val[i]); 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]);
70 trouve = 0; 73 trouve = 0;
71 } 74 }
72 - *node = (*node)->fils[trouve]; 75 + node = (node)->fils[trouve];
73 if(i==taille-1) 76 if(i==taille-1)
74 { 77 {
75 - (*node)->fin=1; 78 + (node)->fin=1;
76 } 79 }
77 } 80 }
78 //mettre fin à 1 pour le bon 81 //mettre fin à 1 pour le bon
79 //if(i==taille-1)(*(tab_ptr_tree[fl]))->fin=1; // 82 //if(i==taille-1)(*(tab_ptr_tree[fl]))->fin=1; //
  83 + }*/
  84 +
  85 +void add(struct node **tab_ptr_tree, char val[],int taille, int fl)
  86 +{
  87 + if(tab_ptr_tree[fl]==NULL)cons_tree(&(tab_ptr_tree[fl]),val[0]+97);
  88 + Node* noeudtest = tab_ptr_tree[fl];
  89 + for(int i = 1;i<taille;i++)
  90 + {
  91 + int trouve = -1;
  92 + for(int j=0;j<noeudtest->nbr_fils;j++)
  93 + {
  94 + if(noeudtest->fils[j]->val==val[i])trouve=j;
  95 + }
  96 + if(trouve==-1)
  97 + {
  98 + //ajouter lettre
  99 + noeudtest->nbr_fils++;
  100 + noeudtest->fils = realloc(noeudtest->fils,(noeudtest->nbr_fils)*sizeof(struct node*));
  101 + cons_tree(&(noeudtest->fils[(noeudtest->nbr_fils)-1]),val[i]);
  102 + trouve = 0;
  103 + }
  104 +
  105 + noeudtest = noeudtest->fils[trouve];//on jump au noeud suivant
  106 + }
  107 + noeudtest->fin = 1;
  108 +
80 } 109 }
81 110
82 int size(char val[]) 111 int size(char val[])
@@ -90,16 +119,17 @@ int size(char val[]) @@ -90,16 +119,17 @@ int size(char val[])
90 } 119 }
91 120
92 121
93 -void load_tree(FILE *fp, struct node ***tab_ptr_tree) 122 +void load_tree(FILE *fp, struct node **tab_ptr_tree)
94 { 123 {
95 //fl (first letter) 124 //fl (first letter)
96 char val[50]; 125 char val[50];
97 126
98 while(fscanf(fp, "%s",val)==1) 127 while(fscanf(fp, "%s",val)==1)
99 { 128 {
  129 + int taille = size(val);
100 if(val[0]<97)val[0]+=32; 130 if(val[0]<97)val[0]+=32;
101 val[0]-=97; 131 val[0]-=97;
102 - add(tab_ptr_tree,val,size(val),(int)val[0]); 132 + add(tab_ptr_tree,val,taille,(int)val[0]);
103 } 133 }
104 134
105 //On peut tester la bonne ou mauvaise terminaison de la lecture 135 //On peut tester la bonne ou mauvaise terminaison de la lecture
@@ -14,8 +14,8 @@ void mk_empty_tree(struct node **); @@ -14,8 +14,8 @@ void mk_empty_tree(struct node **);
14 14
15 int is_leaf(struct node *); 15 int is_leaf(struct node *);
16 16
17 -void add(struct node ***, char* ,int ,int); 17 +void add(struct node **, char* ,int ,int);
18 18
19 -void load_tree(FILE *, struct node ***); 19 +void load_tree(FILE *, struct node **);
20 20
21 void free_tree(struct node **); 21 void free_tree(struct node **);