mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-09 01:44:19 +01:00
19 lines
349 B
JavaScript
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));
|
|
}
|
|
}
|
|
|