3 ms·
And this being front-end, ie. JS, you can just use map/flat in a loop to get breadth first traversal. function bfs(list, cb) { while (list.length > 0
by megous 8y ago
And this being front-end, ie. JS, you can just use map/flat in a loop to get breadth first traversal.
function bfs(list, cb) {
while (list.length > 0) {
let i = list.find(cb);
if (i) return i;
list = list.map(i => i.cn || []).flat();
}
}
- vram22 8y agoInteresting, thanks. The Go Programming Language book also has a couple of nice examples of DFS and BFS, in the middle chapters. One is for sorting courses by prerequisites, and the other is for a web crawler, IIRC.