Write a complete and simple to understand JavaScript code fo
Write a complete and simple to understand JavaScript code for implementing Deapth First Search(DFS) algorithm
Solution
Below is the code for DFS:
public void dfs()
{
Stack s=new Stack();
s.push(this.rootNode);
rootNode.visited=true;
printNode(rootNode);
while(!s.isEmpty())
{
Node n=(Node)s.peek();
Node child=getUnvisitedChildNode(n);
if(child!=null)
{
child.visited=true;
printNode(child);
s.push(child);
}
else
{
s.pop();
}
}
clearNodes();
}
Thank you.
