Created
June 13, 2016 10:53
-
-
Save jarek-przygodzki/5d70ae26860f9a6c4e9413b12a1c3872 to your computer and use it in GitHub Desktop.
Recursive DFS using ES6 generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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