Can anybody give a link for a simple explanation on BFS and DFS with its implementation?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Lets say you are given the following structure:
A breadth first search visits all of a node’s children before visiting their children. Here’s the pseudocode and the solution for the above structure:
A depth first search visits the lowest level (deepest children) of the tree first instead. There are two types of depth first search: pre-order and post-order. This just differentiates between when you add the node to the output (when you visit it vs leave it).
var rootNode = structure.getRoot(); var preOrder = new Array(); var postOrder = new Array(); function DepthFirst( rootNode ){ // Pre-order preOrder[ preOrder.length ] = rootNode; for( var child in rootNode ){ DepthFirst( child ); } // Post-order postOrder[ postOrder.length ] = rootNode; }