Bonjour,
je voudrais savoir comment afficher un arbre binaire "sous la forme d'un arbre"
exemple:
******* + ******************
*** - ******* 9
* 5 ** 6 *********************
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct s_
{
size_t x,y;
char** tab; // tab[y][x]
} bloc;
void allouer(bloc &b,const size_t x,const size_t y)
{
b.x = x;
b.y = y;
b.tab = (char**) malloc(y*sizeof(char*));
size_t j;
for (j=0; j<b.y; j++)
b.tab[j]= (char*) malloc(x*sizeof(char));
}
void liberer(bloc &b)
{
size_t j;
for (j=0; j<b.y; j++)
free(b.tab[j]);
free(b.tab);
}
void creer(bloc &b, const char* str)
{
size_t i,n=strlen(str);
allouer(b,n,1);
for (i=0; i<n; i++)
b.tab[0][i]=str[i];
}
void afficher(const bloc& b)
{
size_t i,j;
for (j=0; j<b.y; j++)
{
for (i=0; i<b.x; i++)
printf("%c",b.tab[j][i]);
printf("\n");
}
printf("\n");
}
// Fusion des petits blocs en un gros bloc
// r : bloc resultat (alloué par la fonction)
// h : bloc en haut centré (désalloué par la fonction)
// b : bloc en bas centré (désalloué par la fonction)
void fusionner(bloc &r, bloc &h, bloc &b, const char blanc=' ')
{
size_t
i,x1,x2,x3,x4,x5,
j,y1=h.y,y2=y1+b.y;
if (h.x<b.x)
{
x1=(b.x-h.x)/2;
x2=x1+h.x;
x3=0;
x4=b.x;
x5=x4;
}
else
{
x1=0;
x2=h.x;
x3=(h.x-b.x)/2;
x4=x3+b.x;
x5=x2;
}
allouer(r,x5,y2);
//----
for (j=0; j<y1; j++)
{
for (i=0; i<x1; i++)
r.tab[j][i]=blanc;
for (i=x1; i<x2; i++)
r.tab[j][i]=h.tab[j][i-x1];
for (i=x2; i<x5; i++)
r.tab[j][i]=blanc;
}
for (j=y1; j<y2; j++)
{
for (i=0; i<x3; i++)
r.tab[j][i]=blanc;
for (i=x3; i<x4; i++)
r.tab[j][i]=b.tab[j-y1][i-x3];
for (i=x4; i<x5; i++)
r.tab[j][i]=blanc;
}
//----
liberer(h);
liberer(b);
}
// Fusion des petits blocs en un gros bloc
// r : bloc resultat (alloué par la fonction)
// h : bloc en haut centré (désalloué par la fonction)
// g : bloc en bas à gauche (désalloué par la fonction)
// d : bloc en bas à droite (désalloué par la fonction)
void fusionner(bloc &r, bloc &h, bloc &g, bloc &d, const char blanc=' ')
{
size_t
x1 = g.x,
x2 = x1+h.x,
x3 = x2+d.x,
y1 = h.y,
y2 = (g.y>d.y) ? y1+g.y : y1+d.y;
allouer(r,x3,y2);
//------
size_t i, j;
for (j=0; j<y1; j++)
{
for (i=0; i<x1; i++)
r.tab[j][i]=blanc;
for (i=x1; i<x2; i++)
r.tab[j][i]=h.tab[j][i-x1];
for (i=x2; i<x3; i++)
r.tab[j][i]=blanc;
}
for (j=0; j<y2-y1; j++)
{
for (i=0; i<x1; i++)
r.tab[j+y1][i]=(g.y>j) ? g.tab[j][i] : blanc;
for (i=x1; i<x2; i++)
r.tab[j+y1][i]=blanc;
for (i=x2; i<x3; i++)
r.tab[j+y1][i]=(d.y>j) ? d.tab[j][i-x2] : blanc;
}
//------
liberer(g);
liberer(h);
liberer(d);
}
int main()
{
bloc b;
{
bloc b1;
{
creer(b1,"*");
}
bloc b2;
{
bloc b21;
{
creer(b21,"+");
}
bloc b22;
{
creer(b22,"1.23");
}
bloc b23;
{
creer(b23,"4.5");
}
fusionner(b2,b21,b22,b23);
}
bloc b3;
{
bloc b31;
{
creer(b31,"-");
}
bloc b32;
{
bloc b321;
{
creer(b321,"/");
}
bloc b322;
{
creer(b322,"6.7");
}
bloc b323;
{
creer(b323,"8.9");
}
fusionner(b32,b321,b322,b323);
}
fusionner(b3,b31,b32);
}
fusionner(b,b1,b2,b3);
}
afficher(b);
liberer(b);
system("PAUSE");
return 0;
}
bloc affichageArbre(Arbre a)
{
bloc b
switch (a.arite)
{
case 0: creer(b,a.operande)
case 1: bloc b1
creer(b1,a.operateur)
bloc b2 = affichageArbre(a.gauche)
fusionner(b,b1,b2)
case 2: bloc b1
creer(b1,a.operateur)
bloc b2 = affichageArbre(a.gauche)
bloc b3 = affichageArbre(a.droite)
fusionner(b,b1,b2,b3)
}
return b;
}void afficherArbre(bloc &b, const arbre a)
{
char ch[20];
bloc b1,b2,b3;
switch (a->arite)
{
case 0:
sprintf(ch,"%f",a->val.f);
creer(b,ch);
return;
case 1:
sprintf(ch,"%c",a->val.n.op_c);
creer(b1,ch);
afficherArbre(b2,a->val.n.filsg);
fusionner(b,b1,b2);
return;
case 2:
sprintf(ch,"%c",a->val.n.op_c);
creer(b1,ch);
afficherArbre(b2,a->val.n.filsg);
afficherArbre(b3,a->val.n.filsd);
fusionner(b,b1,b2,b3);
return;
}
}
void afficherArbre(arbre a)
{
bloc b;
afficherArbre(b,a);
afficher(b);
liberer(b);
}
Combien cela coûte-t-il au total ? Quelles aides apportent l'état et les acteurs du marché pour alléger cette charge non choisie ? Tous les détails sur Commentçamarche.net.