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
| public class Arbre {
/**
* @param args
*/
private String noeud;
private Arbre[] feuilles;
private int nbFeuilles;
public Arbre(String root){
this.noeud = root;
Arbre tabFeuil[] = {};
this.feuilles = tabFeuil;
this.nbFeuilles = 0;
}
public Arbre(String root, Arbre feuil1){
this.noeud = root;
Arbre tabFeuil[] = {feuil1};
this.feuilles = tabFeuil;
this.nbFeuilles = 1;
}
public Arbre(String root, Arbre feuil1, Arbre feuil2){
this.noeud = root;
Arbre tabFeuil[] = {feuil1,feuil2};
this.feuilles = tabFeuil;
this.nbFeuilles = 2;
}
public Arbre(String root, Arbre feuil1, Arbre feuil2, Arbre feuil3){
this.noeud = root;
Arbre tabFeuil[] = {feuil1,feuil2,feuil3};
this.feuilles = tabFeuil;
this.nbFeuilles = 3;
}
public String getNoeud(){
return(this.noeud);
}
public Arbre[] getFeuilles(){
return(this.feuilles);
}
public Arbre getFeuille(int feuilleNum){
return(this.getFeuilles()[feuilleNum+1]);
}
public int getNbFeuilles(){
return(this.nbFeuilles);
}
public String chaine(){
String result="(";
result = result.concat(this.getNoeud());
int i=0;
while(i<this.getNbFeuilles()){
result = result.concat(",").concat(this.getFeuille(i).chaine()));
i++;
}
result = result.concat(")");
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Arbre A = new Arbre("3");
Arbre B = new Arbre("5");
Arbre C = new Arbre("TRUE");
Arbre D = new Arbre("10");
Arbre E = new Arbre("20");
Arbre AB = new Arbre("+",A,B);
Arbre CDE = new Arbre("if",C,D,E);
Arbre ABCDE = new Arbre("*",AB,CDE);
System.out.println(ABCDE.chaine());
}
} |
Partager