Created
June 2, 2017 03:30
-
-
Save nrkn/e152e28a168ab4a6879c0ddd91bb1933 to your computer and use it in GitHub Desktop.
Generator for depth first search of a tree node
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
const dfsGenerator = function*( node ){ | |
const nodes = [ node ] | |
while( nodes.length > 0 ){ | |
const current = nodes.pop() | |
yield current | |
let child = current.lastChild | |
while( child ){ | |
nodes.push( child ) | |
child = child.previousSibling | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dfs-iterator.js