Bonjour,

Je dois réaliser une petite bibliothèque C articulée autour des arbres binaires, j'ai donc pondu le code suivant :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
 
#define MAXMOT 256
#define MAX(x,y) (((x)>(y))?(x):(y))
#define SIZE(t) ((t!=NULL)?(t->hauteur):(0))
 
typedef struct t_element {
  char *elt; 
} t_element;
 
typedef struct t_noeud {
  t_element *valeur;
  int hauteur;
  struct t_noeud *gauche;
  struct t_noeud *droit;
} t_arbre;
 
//allocation m�moire d'un t_element et initialisation par recopie de x
t_element *creer_elt_str(char x[MAXMOT])
{
    t_element *r = (t_element *) malloc ( sizeof(t_element) );
 
    if( r == NULL )
    {
        fprintf(stderr, "Plus de m�moire disponible\n");
        exit(1);
    }
 
    r->elt = (char *) malloc( sizeof(char) * MAXMOT );
 
    if( r->elt == NULL )
    {
        fprintf(stderr, "Plus de m�moire disponible\n");
        exit(2);
    }
 
    r->elt = strcpy( r->elt, x );
    return r;
}
//lib�ration de la m�moire occup�e par un t_element
void detruire_elt (t_element *e)
{
    if( e != NULL )
    {
        if( e->elt != NULL )
            free( e->elt );
 
        free( e );
    }
}
 
// cr�ation d'un arbre binaire
t_arbre *creer_arbre(t_element *e, t_arbre *g, t_arbre *d, int h)
{
    t_arbre *r = (t_arbre *) malloc( sizeof(t_arbre) );
 
    if( r == NULL )
    {
        fprintf(stderr, "Plus de m�moire disponible\n");
        exit(3);
    }
 
    r->valeur = e;
    r->hauteur = h;
    r->gauche = g;
    r->droit = d;
 
    return r;
}
 
// destruction d'un arbre binaire
void detruire_arbre(t_arbre *a)
{
    if( a != NULL )
    {
        detruire_elt( a->valeur );
        detruire_arbre( a->gauche );
        detruire_arbre( a->droit );
        free( a );
    }
}
 
// destruction d'un noeud de l'arbre, cette fonction renvoie l'�l�ment,
t_element *detruire_noeud_arbre(t_arbre *a)
{
    t_element *r = NULL;
 
    if( a != NULL )
    {
        r = a->valeur;
        detruire_arbre( a->gauche );
        detruire_arbre( a->droit );
        free( a );
    }
 
    return r;
}
 
// insertion d'un t_element v dans un t_arbre a, cette fonction renvoie le nouvel arbre
t_arbre *inserer_arbre(t_arbre *a, t_element *v)
{
    t_arbre *r = NULL;
    r = a;
 
    if( r == NULL )
        r = creer_arbre( v, NULL, NULL, 0 );
    else
    {
        switch( strcmp( r->valeur->elt, v->elt ) )
        {
            case 1:
                r->droit = inserer_arbre( r->droit, v );
                break;
            case -1:
                r->gauche = inserer_arbre( r->gauche, v );
                break;
        }
    }
 
    return r;
}
 
// creer un arbre contenant tous les mots de fichier de nom f
t_arbre *creer_arbre_fichier(char *f)
{
    FILE* fichier = NULL;
    char mot[MAXMOT] = "";
    t_arbre *arbre = NULL;
    t_element *element = NULL;
 
    fichier = fopen(f , "r");
    if (fichier != NULL)
    {
        if (fgets(mot, MAXMOT, fichier) != NULL)
        {
            element = creer_elt_str(mot);
            arbre = creer_arbre( element, NULL, NULL, 0 );
            while (fgets(mot, MAXMOT, fichier) != NULL)
            {
                element = creer_elt_str (mot);
                arbre = inserer_arbre(arbre, element);
            }
        }
        fclose(fichier);
        return arbre;
    }
    else
    {
        fprintf( stderr, "Impossible d'ouvrir le fichier %s\n", f );
        return NULL;
    }
}
 
// suppression d'un �l�ment, cette fonction renvoie le nouvel arbre (voir cours)
t_arbre *supprimer_arbre(t_arbre *a, t_element *v)
{
    t_arbre *r = NULL;
    t_arbre *o = NULL;
    t_arbre *p = NULL;
    t_arbre *i = NULL;
    r = a;
    i = p;
 
    if( r == NULL )
        return NULL;
 
    switch( strcmp( r->valeur->elt, v->elt ) )
    {
        case 1:
            p->droit = supprimer_arbre( r->droit, v );
            break;
        case -1:
            p->gauche = supprimer_arbre( r->gauche, v );
            break;
        case 0:
            if( p->gauche == NULL )
                r = r->droit;
            else if( p->droit == NULL )
                r = r->gauche;
            else
            {
                while( i != NULL )
                {
                    p = i;
                    i = i->gauche;
                }
                p->gauche = r->gauche;
                detruire_elt( r->valeur );
                o = r->droit;
                free( r );
                r = o;
            }
            break;
    }
    return r;
}
 
// recherche d'un t_element e dans le t_arbre a, cette fonction renvoie la r�f�rence du noeud dans l'arbre
t_arbre *rechercher_arbre(t_arbre *a, t_element *e)
{
    if( a == NULL )
        return NULL;
 
    switch( strcmp( a->valeur->elt, e->elt ) )
    {
        case 1:
        return rechercher_arbre( a->droit, e );
            break;
        case -1:
        return rechercher_arbre( a->gauche, e );
            break;
        case 0:
            return a;
            break;
    }
    return NULL;
}
// rechercher l'ensemble des mots du fichier de nom f
void rechercher_arbre_fichier(t_arbre *a, char *f)
{
    FILE* fichier = NULL;
    char mot[MAXMOT] = "";
    int find = 1;
    t_arbre *arbre = NULL;
    t_element *element = NULL;
 
    fichier = fopen(f , "r");
    if (fichier != NULL)
    {
        while (fgets(mot, MAXMOT, fichier) != NULL)
        {
            element = creer_elt_str (mot);
            arbre = rechercher_arbre(a, element);
            if (arbre == NULL)
            {
                fprintf( stderr, "%s non trouve", element->elt );
                find = 0;
            }
            detruire_elt(element);
        }
        if (find == 1)
            fprintf( stderr, "Tous les mots de %s ont etes trouve\n", f );
        else
            fprintf( stderr, "Des mots de %s n'ont pas ete trouve\n", f );
 
        fclose(fichier);
    }
    else
        fprintf( stderr, "Impossible d'ouvrir le fichier %s\n",f );
}
 
// calculer la hauteur des noeuds du t_arbre a
void calculer_hauteur(t_arbre *a)
{
    if( a != NULL )
    {
        if( a->gauche == NULL )
        {
            if( a->droit == NULL )
                a->hauteur = 0;
            else
            {
                calculer_hauteur( a->droit );
                a->hauteur = a->droit->hauteur + 1;
            }
        }
        else if( a->droit == NULL )
        {
            if( a->gauche == NULL )
                a->hauteur = 0;
            else
            {
                calculer_hauteur( a->gauche );
                a->hauteur = a->gauche->hauteur + 1;
            }
        }
        else
        {
            calculer_hauteur( a->droit );
            calculer_hauteur( a->gauche );
            a->hauteur = MAX( a->droit->hauteur, a->gauche->hauteur ) + 1;
        }
    }
}
 
// afficher l'arbre et la hauteur des noeuds
void afficher_arbre(t_arbre *a)
{
    if (a->droit != NULL)
        afficher_arbre(a->droit);
 
    if (a->valeur != NULL)
        printf("%d - %s", a->hauteur, a->valeur->elt);
 
    if (a->gauche != NULL)
        afficher_arbre(a->gauche);
}
 
t_arbre *rotation_droite(t_arbre *y)
{
    t_arbre* a = NULL;
 
    if (y != NULL && y->gauche != NULL)
    {
        a = y->gauche;
        y->gauche = a->droit;
        a->droit = y;
 
        a->droit->hauteur = MAX(SIZE(a->droit->gauche), SIZE(a->droit->droit))+1;
        a->gauche->hauteur = MAX(SIZE(a->gauche->gauche), SIZE(a->gauche->droit))+1;
        a->hauteur = MAX(SIZE(a->gauche), SIZE(a->droit))+1;
        //printf( "Rotation Droite effectu�e \n" );
        return a;
    }
    else
    {
        return y;
    }
}
 
t_arbre *rotation_gauche(t_arbre *x)
{
    t_arbre* a = NULL;
 
    if (x != NULL && x->gauche != NULL)
    {
        a = x->droit;
        x->droit = a->gauche;
        a->gauche = x;
 
        a->droit->hauteur = MAX(SIZE(a->droit->gauche), SIZE(a->droit->droit))+1;
        a->gauche->hauteur = MAX(SIZE(a->gauche->gauche), SIZE(a->gauche->droit))+1;
        a->hauteur = MAX(SIZE(a->gauche), SIZE(a->droit))+1;
        //printf( "Rotation Droite effectu�e \n" );
        return a;
    }
    else
        return x;
}
 
t_arbre *equilibrer_arbre(t_arbre *a)
{
    int hd = 0;
    if( a == NULL )
        return NULL;
 
    hd = SIZE(a->gauche) - SIZE(a->droit);
 
    while ( hd < -1 || hd > 1 )
    {
        if( hd > 1 )
        {
            a = rotation_gauche(a);
            a->gauche = equilibrer_arbre(a->gauche);
        }
        else
        {
            a = rotation_droite(a);
            a->droit = equilibrer_arbre(a->droit);
        }
 
        hd = SIZE(a->gauche) - SIZE(a->droit);
    }
 
    a->hauteur = MAX(SIZE(a->gauche), SIZE(a->droit))+1;
    return a;
}
 
int main (int argc, char *argv[])
{
    //t_arbre *a = creer_arbre_fichier("dico1.txt");
    t_arbre *a = creer_arbre_fichier(argv[1]);
 
  calculer_hauteur(a);
  printf("hauteur totale %d \n", a->hauteur);
  //afficher_arbre(a);
 
//  t_arbre *b = rotation_gauche(a);
    t_arbre *b = equilibrer_arbre(a);
//  rechercher_arbre_fichier(b, "a_rechercher.txt");
  calculer_hauteur(b);
 
  afficher_arbre(b);
  printf("hauteur totale %d \n", b->hauteur);
  detruire_arbre(b);
 
//  char* r;
//  scanf(r);
 
  return 0;
}
Mon soucis se situe sur la fonction d'équilibrage qui me fait une erreur de segmentation à la ligne 314.
Mes fonctions de rotations sont correcte et je pense que mon algo à un problème mais j'ai du mal à voir où il est.

Auriez-vous une idée,
Pour votre aide,
Par avance,
Merci