Blame view

tree.c 2.32 KB
9cf06b18   mertz   debut_add_tree
1
  #include "tree.h"
f7d9ccda   mertz   ajout_libtree
2
  
284154ca   Thorsieger   gestion des accents
3
  void cons_tree(struct node ** ptr_tree, wchar_t val)
f7d9ccda   mertz   ajout_libtree
4
  {
9cf06b18   mertz   debut_add_tree
5
6
7
8
9
10
    *ptr_tree = malloc(sizeof(struct node));
    (*ptr_tree)->val = val;
    (*ptr_tree)->fin = 0;
    (*ptr_tree)->nbr_fils=0;
    (*ptr_tree)->fils = malloc(sizeof(struct node*));
    (*ptr_tree)->fils[0]=NULL;
f7d9ccda   mertz   ajout_libtree
11
12
  }
  
9cf06b18   mertz   debut_add_tree
13
14
15
16
  void mk_empty_tree(struct node **ptr_tree)
  {
    *ptr_tree = NULL;
  }
f7d9ccda   mertz   ajout_libtree
17
  
9cf06b18   mertz   debut_add_tree
18
19
20
21
  int is_leaf(struct node *tree)
  {
    return tree->fin||tree->fils[0]==NULL;
  }
f7d9ccda   mertz   ajout_libtree
22
  
284154ca   Thorsieger   gestion des accents
23
  void add(struct node **tab_ptr_tree, wchar_t val[],int taille, int fl)
03168857   mertz   create tree op
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  {
    if(tab_ptr_tree[fl]==NULL)cons_tree(&(tab_ptr_tree[fl]),val[0]+97);
    Node* noeudtest = tab_ptr_tree[fl];
    for(int i = 1;i<taille;i++)
      {
        int trouve = -1;
        for(int j=0;j<noeudtest->nbr_fils;j++)
  	{
  	  if(noeudtest->fils[j]->val==val[i])trouve=j;
  	}
        if(trouve==-1)
  	{	  
  	  //ajouter lettre
  	   noeudtest->nbr_fils++;
  	   noeudtest->fils = realloc(noeudtest->fils,(noeudtest->nbr_fils)*sizeof(struct node*));
  	   cons_tree(&(noeudtest->fils[(noeudtest->nbr_fils)-1]),val[i]);
aeb78bdf   mertz   correction fin de...
40
  	   trouve = noeudtest->nbr_fils-1;
03168857   mertz   create tree op
41
42
43
44
45
46
  	}
        
        noeudtest = noeudtest->fils[trouve];//on jump au noeud suivant
      }
    noeudtest->fin = 1;
  
9cf06b18   mertz   debut_add_tree
47
  }
f7d9ccda   mertz   ajout_libtree
48
  
284154ca   Thorsieger   gestion des accents
49
  int size(wchar_t val[])
541fd894   mertz   on_avance_tree
50
51
52
53
54
55
56
57
58
  {
    int cpt = 0;
    while(val[cpt]!='\0')
      {
        cpt++;
      }
    return cpt;
  }
  
284154ca   Thorsieger   gestion des accents
59
  void toLowerCase(wchar_t mot[],int size)
aeb78bdf   mertz   correction fin de...
60
61
62
63
64
65
66
67
68
69
  {
    for(int i=0;i<size;i++)
      {
        if(mot[i]<='Z' && mot[i]>='A')
  	{
  	  mot[i]+=32;
  	}
      }
  }
  
541fd894   mertz   on_avance_tree
70
  
03168857   mertz   create tree op
71
  void load_tree(FILE *fp, struct node **tab_ptr_tree)
9cf06b18   mertz   debut_add_tree
72
73
  {
    //fl (first letter)
284154ca   Thorsieger   gestion des accents
74
    wchar_t val[50];
541fd894   mertz   on_avance_tree
75
    
284154ca   Thorsieger   gestion des accents
76
    while(fwscanf(fp, L"%ls",val)==1)
541fd894   mertz   on_avance_tree
77
      {
03168857   mertz   create tree op
78
        int taille = size(val);
34d66d1d   mertz   ajout du toLowerCase
79
        toLowerCase(val,taille);
284154ca   Thorsieger   gestion des accents
80
        if(val[0]<'a' || val[0]>'z')continue;
34d66d1d   mertz   ajout du toLowerCase
81
82
83
        //if(val[0]<97)val[0]+=32;
        //val[0]-=97;
        add(tab_ptr_tree,val,taille,(int)val[0]-97);
541fd894   mertz   on_avance_tree
84
85
86
87
88
      }
  
      //On peut tester la bonne ou mauvaise terminaison de la lecture
      if(feof(fp))    printf("Fin normal de lecture\n");
      if(ferror(fp))  printf("ERREUR de lecture\n");
9cf06b18   mertz   debut_add_tree
89
  }
f7d9ccda   mertz   ajout_libtree
90
  
7ab3be6b   Thorsieger   debut free
91
  void free_tree(struct node *ptr_tree)
9cf06b18   mertz   debut_add_tree
92
  {
131759ee   Thorsieger   fin free arbre
93
94
95
    if(ptr_tree==NULL)return;
    if(ptr_tree->nbr_fils==0){free(ptr_tree->fils);return;}
    for(int i=(ptr_tree->nbr_fils)-1;i>=0;i--)
7ab3be6b   Thorsieger   debut free
96
    {
131759ee   Thorsieger   fin free arbre
97
      free_tree(ptr_tree->fils[i]);
7ab3be6b   Thorsieger   debut free
98
99
      free(ptr_tree->fils[i]);
    }
131759ee   Thorsieger   fin free arbre
100
    free(ptr_tree->fils);
7ab3be6b   Thorsieger   debut free
101
102
103
104
105
106
107
108
109
  }
  
  void free_dico(struct node **tab_ptr_tree)
  {
    for(int i=0;i<26;i++)
    {
      if(tab_ptr_tree[i]!=NULL)
      {
        free_tree(tab_ptr_tree[i]);
131759ee   Thorsieger   fin free arbre
110
        free(tab_ptr_tree[i]);
7ab3be6b   Thorsieger   debut free
111
112
      }
    }
9cf06b18   mertz   debut_add_tree
113
  }