From 71c4e293ff8efa5b2c740a4fb7da0f95b119fce7 Mon Sep 17 00:00:00 2001 From: kitdim Date: Sun, 12 Oct 2025 02:47:26 +0300 Subject: [PATCH] Add a solution for Chapter 7 --- 07_trees/java/Main.java | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 07_trees/java/Main.java diff --git a/07_trees/java/Main.java b/07_trees/java/Main.java new file mode 100644 index 00000000..527146f4 --- /dev/null +++ b/07_trees/java/Main.java @@ -0,0 +1,61 @@ +import java.io.File; +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Deque; +import java.util.List; + +public class Main { + public static void main(String[] args) { + // Create a File object for the "pics" directory in the current working directory + File file = new File(System.getProperty("user.dir"), "pics"); + + // Print all file names using iterative approach + printAllNamesFiles(file); + + // Print all file names using recursive approach + printAllNamesFilesWithRec(file); + } + + /** + * Iteratively prints the names of all files in the given directory and its subdirectories. + * Uses a stack (Deque) for breadth-first traversal. + */ + public static void printAllNamesFiles(File dir) { + // Initialize the stack with all files and directories in the starting directory + Deque filesAndDirs = new ArrayDeque<>(List.of(dir.listFiles())); + + // Continue until there are no more files/directories to process + while (!filesAndDirs.isEmpty()) { + // Get the next file or directory from the stack + File someFileOrDir = filesAndDirs.pop(); + + if (someFileOrDir.isFile()) { + // If it is a file, print its name + System.out.println(someFileOrDir.getName()); + } else if (someFileOrDir.isDirectory()){ + // If it is a directory, add its contents to the stack + filesAndDirs.addAll(Arrays.asList((someFileOrDir.listFiles()))); + } + } + } + + /** + * Recursively prints the names of all files in the given directory and its subdirectories. + * Uses depth-first traversal. + */ + public static void printAllNamesFilesWithRec(File dir) { + // Get all files and directories in the current directory + File[] filesAndDirs = dir.listFiles(); + + // Iterate through each file/directory + for (File someFileOrDir : filesAndDirs) { + if (someFileOrDir.isFile()) { + // If it is a file, print its name + System.out.println(someFileOrDir.getName()); + } else if (someFileOrDir.isDirectory()) { + // If it is a directory, recursively process its contents + printAllNamesFilesWithRec(someFileOrDir); + } + } + } +} \ No newline at end of file