Skip to content

Instantly share code, notes, and snippets.

@jarek-przygodzki
Created June 13, 2016 10:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarek-przygodzki/5d70ae26860f9a6c4e9413b12a1c3872 to your computer and use it in GitHub Desktop.
Save jarek-przygodzki/5d70ae26860f9a6c4e9413b12a1c3872 to your computer and use it in GitHub Desktop.
Recursive DFS using ES6 generator
class Node {
constructor(name, childNodes) {
this.name = name;
this.childNodes = childNodes;
this.visited = false;
}
}
function *dfs(u) {
u.visited = true;
yield u;
for(let v of u.childNodes) {
if(!v.visited) {
yield *dfs(v)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment