jsketcher/modules/gems/traverse.js
2020-02-05 23:24:10 -08:00

19 lines
349 B
JavaScript

export function dfs(node, children, callback) {
const visited = new Set();
const stack = [];
stack.push(node);
while (stack.length) {
const node = stack.pop();
if (visited.has(node)) {
continue;
}
visited.add(node);
if (callback(node)) {
return;
}
children(node, child => stack.push(child));
}
}