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?
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.