I want to generate a BFS forest of of a DAG (Direct Acyclic Graph). This means my Tree class needs to be a general tree and not a binary tree (in other words, I can't know the number of children a node will have ahead of time when I am generating a forest). Most of the code is written and shown below, however I lack one line that, for the life of me, escapes me!
public Tree BFS(V start)
{
reset();
LinkedList<GraphMatrixVertex<V>> list = new LinkedList<GraphMatrixVertex<V>>();
GraphMatrixVertex<V> vert = dict.get(start);
Tree root = new Tree(vert);
list.add(vert);
do
{
vert = list.getFirst();
Iterator<V> ni = neighbors(start);
while(ni.hasNext())
{
V v = ni.next();
GraphMatrixVertex<V> vtx = dict.get(v);
if(!vtx.isVisited())
{
list.add(vtx);
vtx.visit();
root.addChild(new Tree(vtx));
}
}
//code goes here
}
while(!list.isEmpty());
return root;
}
My Tree class stores a value parameter, a parent reference, and a list of children. My problem is referencing the next tree node. Once I have added all the unvisited neighbors as childs of the current node, how do I get to the next node?
EDIT:
So it would look something like this?
public void bfs(Tree parent)
{
Iterator<V> ni = neighbors((V) parent.value());
if(ni.hasNext())
{
while(ni.hasNext())
{
V next = ni.next();
GraphMatrixVertex<V> vert = dict.get(next);
if(!vert.isVisited())
parent.addChild(new Tree(next));
}
}
}
where does the recursive call go?