Skip to content

Instantly share code, notes, and snippets.

@jack-wong-build
Created February 18, 2014 14:32
Show Gist options
  • Save jack-wong-build/9072123 to your computer and use it in GitHub Desktop.
Save jack-wong-build/9072123 to your computer and use it in GitHub Desktop.
Depth First Search
function depthFirstSearch(node){
if (node === null) return;
// visit node
console.log(node.value);
node.visited = true;
for (var i = 0; i < node.children.length; i++){
if (node.children[i].visited === false){
depthFirstSearch(node.children[i]);
}
}
}
// a = {value:1, visited:false, children:[]};
// b = {value:2, visited:false, children:[]};
// c = {value:3, visited:false, children:[]};
// d = {value:4, visited:false, children:[]};
// e = {value:5, visited:false, children:[]};
// f = {value:6, visited:false, children:[]};
// g = {value:7, visited:false, children:[]};
// h = {value:8, visited:false, children:[]};
// a.children.push(b);
// a.children.push(c);
// a.children.push(d);
// b.children.push(e);
// c.children.push(f);
// d.children.push(g);
// e.children.push(h);
// depthFirstSearch(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment