fin arbre
This commit is contained in:
42
DEV3.2/arbre/repertoire/Noeud.java
Normal file
42
DEV3.2/arbre/repertoire/Noeud.java
Normal file
@@ -0,0 +1,42 @@
|
||||
import java.io.File;
|
||||
|
||||
// Classe représentant un nœud de l'arbre
|
||||
public class Noeud {
|
||||
String nom;
|
||||
Noeud[] enfants;
|
||||
|
||||
public Noeud(String nom) {
|
||||
this.nom = nom;
|
||||
this.enfants = new Noeud[0]; // Initialement pas d'enfants
|
||||
}
|
||||
|
||||
public void setEnfants(Noeud[] enfants) {
|
||||
this.enfants = enfants;
|
||||
}
|
||||
|
||||
public void afficher(String prefixe) {
|
||||
System.out.println(prefixe + nom);
|
||||
for (Noeud enfant : enfants) {
|
||||
enfant.afficher(prefixe + " ");
|
||||
}
|
||||
}
|
||||
|
||||
// Méthode pour construire un arbre à partir d'un répertoire
|
||||
public static Noeud construireArbre(File fichier) {
|
||||
Noeud noeud = new Noeud(fichier.getName());
|
||||
if (fichier.isDirectory()) {
|
||||
File[] contenu = fichier.listFiles();
|
||||
if (contenu != null) {
|
||||
Noeud[] enfants = new Noeud[contenu.length];
|
||||
for (int i = 0; i < contenu.length; i++) {
|
||||
System.out.print(fichier.getName()+":");
|
||||
System.out.print(contenu[i]);
|
||||
System.out.println("");
|
||||
enfants[i] = construireArbre(contenu[i]);
|
||||
}
|
||||
noeud.setEnfants(enfants);
|
||||
}
|
||||
}
|
||||
return noeud;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user