Data Structures And Algorithms

Algorithms and Data Structures

Assignment – Generalized Trees

Introduction

The file system on most modern operating systems is organized as a generalized tree structure. The nodes of the tree are regular files and subdirectories. The subdirectories provide hierarchical structure, as they can contain other subdirectories as well as regular files.

For this assignment, write a Java program that recursively traverses the file system starting at a fully qualified subdirectory. The subdirectory must be provided as a command line parameter. When processing begins, the first line of output is the fully qualified subdirectory.

For each subdirectory encountered, get a list of the files in that subdirectory. For each entry in the list, if it is a regular file, output the file name and extension. Do not include the path name; just the file name and extension. If the file is a subdirectory, output just the subdirectory name; do not include the pathname that precedes the subdirectory name. In either case, after outputting the name, recursively descend into subdirectory and continue. You may recognize this as a pre-order traversal.

For each line of output, prefix the file or subdirectory name with indentation that provides a visual indication of the depth of the recursive descent.

Students are responsible for

· public FileTreeWalk(String pathname)

· public String listAllFiles()

· public String toString()

class FileTreeWalk

FileTreeWalk is a Java class used for traversing a file system. In addition to a constructor, the class provides two public methods for traversing the file system, and a public toString method for displaying the traversal results.

The public interface to the class follows:

public FileTreeWalk (String pathname)

The constructor takes a String parameter that specifies a pathname. The pathname is a starting point for the file system traversal. The parameter is either a fully qualified path name, or a relative pathname. Fully qualified pathnames start with an optional drive letter, or a slash. On other operating systems (e.g., Linux, Unix), a fully qualified pathname starts with a slash. Relative pathnames start at the current working directory. In either case, the pathname parameter specifies where the file system traversal is supposed to begin.

As FileTreeWalk traverses the file system, it adds file names and subdirectory names to a generalized tree structure that when finished will represent the current state of the file system from the starting point specified by the String parameter, pathname.

public String listAllFiles ()

This method returns a string representation of the generalized tree of the file system starting at the pathname specified in and built by the constructor. Provided the argument to the constructor is the string, “C:\\Users\BigJoe\Documents”, the output might look like this:

C:\\Users\BigJoe\Documents

FileTreeWalk

Build

Classes

FileTreeWalk

FileTreeWalk.class

.netbeans_automatic_build

.netbeans_update_resources

nbproject

private

private.properties

build-impl.xml

henfiles.properties

project.properties

project.xml

src

filetreewalk

FileTreeWalk.java

test

build.xml

manifest.mf

assignment03.docx

Notice that the indentation reflects the position of the files within the files system hierarchy. Do not store the indentation in the tree structure. Add it during the pre-order traversal of the tree.

From the example, notice that subdirectory FileTreeWalk contains four subdirectories (Build, nbproject, src, and test), and two regular files (build.xml, and manifest.mf). Also notice that when a subdirectory is first encountered, it’s name is output before the files within it, making it a pre-order traversal.

public String toString ()

This method returns a String representation of the file system traversal created by the constructor. It just returns the String value that listAllFiles()returns.

Additional Note

Java 8 provides a class, java.nio.file.Files, that encapsulates all of the recursion needed to solve this assignment with no recursion in the calling code (code that you will write). Which is great. But this assignment is to help you better understand recursion. Do not use java.nio.file.Files for this assignment. If you do use it, you can pretty much count on receiving no credit for the assignment.

Prior to Java 8, the way you would approach this problem is to use java.io. File. Use this class and its methods to traverse the file system. An example of this method is provided on the canvas module.

Do not include a public static void main method to your java class. I will be using my own class for testing.