Skip to content

Instantly share code, notes, and snippets.

@Sophia-Gold
Created June 7, 2018 05:09
Show Gist options
  • Save Sophia-Gold/4d5113a001ac1835bafa6ceab9d5917f to your computer and use it in GitHub Desktop.
Save Sophia-Gold/4d5113a001ac1835bafa6ceab9d5917f 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