This commit is contained in:
2023-11-29 15:05:29 +01:00
parent 1a35bf185b
commit 738be28799
22 changed files with 875 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,50 @@
import java.util.ArrayList;
/**
* La classe Noeud représente un nœud dans une structure d'arbre.
* Chaque nœud possède un identifiant unique (ID), un nom, un rang, et une liste d'enfants.
*/
public class Noeud {
/** Le nom du nœud */
public String name;
/** La liste des enfants du nœud */
public ArrayList<Noeud> enfants;
public Noeud(String nom) {
this.name = nom;
this.enfants = new ArrayList<>();
}
/**
* Ajoute un nœud en tant qu'enfant du nœud actuel.
*
* @param enfant Le nœud à ajouter en tant qu'enfant.
*/
public void ajouterEnfant(Noeud enfant) {
this.enfants.add(enfant);
}
/**
* Obtient la liste des enfants du nœud.
*
* @return La liste des enfants du nœud.
*/
public ArrayList<Noeud> getEnfants() {
return enfants;
}
/**
* Obtient une représentation textuelle du nœud (son nom).
*
* @return Le nom du nœud.
*/
@Override
public String toString() {
return name;
}
}

View File

@@ -0,0 +1,52 @@
import java.io.*;
public class repertoires {
public static void main(String args[])
{
try {
File f = new File(args[0]);
System.out.println("Files are:");
Noeud test = explo(f);
afficherArbre(test);
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
private static Noeud explo(File f)
{
File[] files = f.listFiles();
Noeud root = new Noeud(f.getName());
if (files != null) {
for (File fi : files) {
Noeud node = new Noeud(fi.getName());
node.ajouterEnfant(explo(fi));
root.ajouterEnfant(root);
}
}
return root;
}
public static void afficherArbre(Noeud racine) {
afficherArbre(racine, 0);
}
private static void afficherArbre(Noeud noeud, int niveau) {
// Affiche le nœud actuel avec une indentation basée sur le niveau
StringBuilder indentation = new StringBuilder();
for (int i = 0; i < niveau; i++) {
indentation.append(" "); // Deux espaces par niveau pour l'indentation
}
System.out.println(indentation.toString() + noeud);
// Appelle récursivement la méthode pour chaque enfant du nœud actuel
for (Noeud enfant : noeud.getEnfants()) {
afficherArbre(enfant, niveau + 1);
}
}
}