Salut à tous,
J'ai essayé de réaliser une fonction qui parcourt un arbre binaire de recherche en largeur. Mais celle-ci boucle à l'infini.
Voilà le code entier :
Code C : 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
 
#include <stdio.h>
#include <stdlib.h>
 
typedef struct noeud noeud;
struct noeud
{
    int data;
    struct noeud *pSuiv;
    struct noeud *pPred;
};
 
typedef struct fileFIFO fileFIFO;
struct fileFIFO
{
    size_t length;
    struct noeud *pTete;
    struct noeud *pQueue;
};
 
fileFIFO *initFile(void)
{
    fileFIFO *nouvelFile = malloc(sizeof(fileFIFO));
    if (nouvelFile != NULL)
    {
        nouvelFile->length = 0;
        nouvelFile->pTete = NULL;
        nouvelFile->pQueue = NULL;
    }
    return nouvelFile;
}
 
void pushFront(fileFIFO **file , int data)
{
	if (file != NULL)
	{
		noeud *nouvelElement = malloc(sizeof(noeud));
		if (nouvelElement != NULL)
		{
			nouvelElement->data = data;
			nouvelElement->pPred = NULL;
		}
		if((*file)->pQueue == NULL)
		{
			nouvelElement->pSuiv = NULL;
			(*file)->pTete = nouvelElement;
			(*file)->pQueue = nouvelElement;
		}
		else
		{
			nouvelElement->pSuiv = (*file)->pTete;
			(*file)->pTete->pPred = nouvelElement;
			(*file)->pTete = nouvelElement;
		}
		(*file)->length++;
	}      
}
 
 
int popBack(fileFIFO **file)
{
	int v;
	if (*file != NULL)
	{
		if ((*file)->pTete != NULL)
		{
			noeud *aSupprimer = (*file)->pQueue;
			(*file)->pQueue = (*file)->pQueue->pPred;
			v = aSupprimer->data;
			free(aSupprimer);
			(*file)->length--;
		}
		else
			printf("La file est vide\n");
 
	}
	return v;
}
 
/********************************************************************************/
typedef struct arbresBinaireRecherche arbresBinaireRecherche;
struct arbresBinaireRecherche
{
	int val;
	struct arbresBinaireRecherche *pABRGauche;
	struct arbresBinaireRecherche *pABRDroit;
};
 
 
arbresBinaireRecherche *initABR(int r)
{
	arbresBinaireRecherche * ABR = malloc(sizeof(arbresBinaireRecherche));
	if (ABR != NULL)
	{
		ABR->val = r;
		ABR->pABRGauche = NULL;
		ABR->pABRDroit = NULL;
		return ABR;
	}
	return ABR;
}
 
int racine (arbresBinaireRecherche *ABR)
{
	return ABR->val;
}
 
arbresBinaireRecherche *ABRGauche(arbresBinaireRecherche * ABR)
{
	return ABR->pABRGauche;
}
 
arbresBinaireRecherche *ABRDroit(arbresBinaireRecherche * ABR)
{
	return ABR->pABRDroit;
}
 
int estVide(arbresBinaireRecherche * ABR)
{
	return (ABR != NULL) ? 1 : 0;
}
 
void inserer(arbresBinaireRecherche **ABR , int v)
{
	if (estVide(*ABR) == 0)   *ABR = initABR(v);
 
	if (v <= (*ABR)->val) 
	{
		if ((*ABR)->pABRGauche != NULL) 
			inserer( &(*ABR)->pABRGauche , v);
		else
			(*ABR)->pABRGauche = initABR(v);
	}
	else
	{
		if ((*ABR)->pABRDroit != NULL) 
			inserer( &(*ABR)->pABRDroit , v);
		else
			(*ABR)->pABRDroit = initABR(v);
	}
 
 
}
 
void parcoursBFS(arbresBinaireRecherche *ABR)
{
	if (ABR != NULL)
	{
		fileFIFO * maFile = initFile();
		pushFront(&maFile,ABR->val);
		while (maFile->pTete != NULL)
		{
			int x = popBack(&maFile);
			printf("%d ",x);
			if (ABR->pABRGauche != NULL) pushFront(&maFile,ABR->pABRGauche->val);
			if (ABR->pABRDroit != NULL) pushFront(&maFile,ABR->pABRDroit->val);
		}
	}
	else
		{printf("L'arbre est vide !\n");return;}
}
 
void trier (arbresBinaireRecherche *ABR)
{
	if (ABR != NULL)
	{
		if (ABR->pABRGauche != NULL)	trier(ABR->pABRGauche);
		printf("%d ",ABR->val);
		if (ABR->pABRDroit != NULL)		trier(ABR->pABRDroit);
	}
	else
		printf("L'arbre est vide !\n");
}
 
 
void viderArbre (arbresBinaireRecherche **ABR)
{
	if (estVide(*ABR) == 1)
	{
		viderArbre(&(*ABR)->pABRGauche);
		viderArbre(&(*ABR)->pABRDroit);
		free(*ABR);
		*ABR = NULL;
	}
}
 
 
void afficherListe(fileFIFO *liste)
{
	if (liste != NULL)
	{
		if (liste->pTete == NULL) printf("Liste vide");
		else
		{
			noeud *tmp = liste->pTete;
			for(int i = 0 ; i < liste->length && tmp != NULL ; ++i)
			{
				printf("%d " , tmp->data);
				tmp = tmp->pSuiv;
			}
		}
	}
	else
		printf("Liste vide.");
 
}
 
int main()
{
	arbresBinaireRecherche *monABR = NULL;
	//fileFIFO *f = initFile();
	int e;
	printf("Donnez les nombres de l'arbre de recherche binaire (séparer par un espace et finir par une lettre pour ternimer la saisie) : \n");
	 while (scanf("%d", &e) == 1)
		inserer(&monABR,e);
 
	scanf("%*[^\n]");
	//afficherListe(f);
	//printf("\n%d \n",popBack(&f));
	//afficherListe(f);
	//if (f->pQueue == NULL) printf("null \n");
	//trier(monABR);
	parcoursBFS(monABR);
	viderArbre(&monABR);	
	return 0;
}

la fonction "trier" marche très bien. Mais la fonction "parcoursBFS" (ligne 144) boucle à l'infinie et n'affiche que la racine. J'utilise une file FIFO pour faire le parcours.
Merci.