Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7963851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:43:03+00:00 2026-06-04T05:43:03+00:00

I was recently informed about BFS & DFS and was asked to implement something

  • 0

I was recently informed about BFS & DFS and was asked to implement something in DFS: a directory listing/searching for a filename. I was able to pull that off (which in all fairness, I did get a hint on how to proceed), but I’ve been intrigued by BFS ever since and I’ve been unable to even grasp how to implement that concept on the same problem.

Based on the diagrams I found on Wikipedia and several google searches, here’s the closest thing I’ve gotten so far:

The Code:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.File;

public class foo {
    private List<List<String>> queue = new ArrayList<List<String>>();

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        foo f = new foo();
        f.traverse("src");
        f.report();
    }

    public void traverse(String dir) throws Exception {
        // add dir to the top of the tree
        queue.add(0, Arrays.asList(dir));
        traverse(dir, 1);
    }

    public void traverse(String dir, int depth) throws Exception {
        // add a new depth if this is a new one
        if (queue.size() <= depth) {
            queue.add(new ArrayList<String>());
        }

        File file = new File(dir);
        for (File curfile: file.listFiles()) {
            queue.get(depth).add(curfile.getPath());
            // recursive function call if curfile is a directory
            if (curfile.isDirectory()) traverse(curfile.getPath(), depth+1);
        }
    }

    public void report() {
        for (int i=0; i<queue.size()-1; i++) {
            log(String.format("****** Level %d ******", i));
            for (String node : queue.get(i))
                log(String.format("[%d] `%s'", i, node));
        }
    }

    public void log(String s) {
        System.out.printf("[foo] %s\n", s);
    }
}

The Output:

[foo] ****** Level 0 ******
[foo] [0] 'src'
[foo] ****** Level 1 ******
[foo] [1] 'src/A'
[foo] [1] 'src/C'
[foo] [1] 'src/foo.java'
[foo] [1] 'src/B'
[foo] ****** Level 2 ******
[foo] [2] 'src/A/A2'
[foo] [2] 'src/A/A1'
[foo] [2] 'src/C/C1'
[foo] ****** Level 3 ******
[foo] [3] 'src/A/A2/A2A'
[foo] [3] 'src/A/A1/A1A'
[foo] [3] 'src/C/C1/C1A'
[foo] [3] 'src/C/C1/C1B'
[foo] ****** Level 4 ******
[foo] [4] 'src/A/A2/A2A/A2A1'
[foo] [4] 'src/C/C1/C1A/C1A1'
[foo] ****** Level 5 ******
[foo] [5] 'src/A/A2/A2A/A2A1/A2A1A'
[foo] ****** Level 6 ******
[foo] [6] 'src/A/A2/A2A/A2A1/A2A1A/A2A1A1'
[foo] [6] 'src/A/A2/A2A/A2A1/A2A1A/A2A1A2'

I know this can’t be right because although it spits output that appears correct, I know the inner workings are wrong. It’s essentially a DFS masquerading as a BFS, using an ArrayList to hide the evidence.

Desperately hoping someone can help me get closure here because I’ve got a frameworks book burning a hole on my desk for almost a month now since I started procrastinating trying to understand this concept. And so, buried in a ton of rambling, my question is: how can BFS apply to directory structures? Also, is there a “For-Dummies” version of BFS/DFS implementation examples online or in print anywhere?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:43:05+00:00Added an answer on June 4, 2026 at 5:43 am

    How can BFS apply to directory structure? Both, BFS and DFS concepts are about tree data structures (directory can be a tree too). Assuming you understand tree depth, BFS is basically visiting all nodes in order from lowest depth to greatest. Stack is to DFS as Queue is to BFS.

    I’m not sure if there is a “for-dummies” implementation. I think the concept is as simple as I’ve explained. Wikipedia should provide the missing information.

    What doubt do you have about your implementaion? Like I said, the only diffence between DFS and BFS is using Stack or Queue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I asked a question on stackoverflow about multiline tabs. Below is the link
Recently i am learning something about unicode in python2.7, and i want to know
I recently asked about the wisdom of using multiple Xcode projects to separate project
Recently I've been thinking about how to transform a complex polygon into a non-complex
I've recently been informed that I need to define the requirements for the project
Recently I have been informed that it is possible to concatenate integers (and other
Recently I was asked to add an XML API to one of the Symfony
Recently in a technical interview, I was asked to write a program to find
Recently I needed to add drag & drop functionality to a Silverlight application. Can
Recently I took a test at brainbench and got not a bad result (something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.