Hacker News new | past | comments | ask | show | jobs | submit login

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();
       }
   }



Interesting, 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: