Skip to content

Instantly share code, notes, and snippets.

@rebcabin
Forked from Sophia-Gold/bfs.clj
Created January 17, 2023 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebcabin/bb9d1fc5b6bd5e468a6e70dfbca3264f to your computer and use it in GitHub Desktop.
Save rebcabin/bb9d1fc5b6bd5e468a6e70dfbca3264f to your computer and use it in GitHub Desktop.
breadth-first search is annoying in Clojure
;; https://stackoverflow.com/questions/11409140/stumped-with-functional-breadth-first-tree-traversal-in-clojure
(def tree [1 [2 [4] [5]] [3 [6]]])
(defn bfs
[tree]
(loop [ret []
queue (conj [] tree)]
(if (seq queue)
(let [[node & children] (first queue)]
(println queue)
(println ret)
(recur (conj ret node)
(into children (next queue))))
ret)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment