CSci 151: Foundations of computer science II
Home Syllabus Assignments Tests

Assignment 9: Directory tree

Due: 5:00pm, Friday, November 7. Value: 30 pts.

The program contains the following recursive method that descends through the whole directory tree rooted by base and displays the total size of files within each directory.

private static void search(File base) {
    File[] sub = base.listFiles();
    if(sub != null && sub.length > 0) {
        long total = base.length();
        for(int i = 0; i < sub.length; i++) {
            total += sub[i].length();
        System.out.printf("%8dKB %s\n", (total + 1023) / 1024, base.getPath());

Using the stack-based technique shown in class, convert the method to a version that uses no recursion. The converted program should use a stack, and it should some definite correspondence to the original method.