Skip to content

Instantly share code, notes, and snippets.

@nrkn
Created June 2, 2017 03:30
Show Gist options
  • Save nrkn/e152e28a168ab4a6879c0ddd91bb1933 to your computer and use it in GitHub Desktop.
Save nrkn/e152e28a168ab4a6879c0ddd91bb1933 to your computer and use it in GitHub Desktop.
Generator for depth first search of a tree node
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
}
}
}
@nrkn
Copy link
Author

nrkn commented Jun 2, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment