public void bfs()
{
// BFS uses Queue data structure
Queue q = new LinkedList();
q.add(rootNode);
visited[rootNode] = true;
printNode(rootNode);
while( !q.isEmpty() )
{
int n, child;
n = (q.peek()).intValue();
child = getUnvisitedChildNode(n); // Returns -1 if no unvisited niode left
if ( child != -1 )
{ // Found an unvisted node
visited[child] = true; // Mark as visited
printNode(child);
q.add(child); // Add to queue
}
else
{
q.remove(); // Process next node
}
}
}
|