-
-
Save rebcabin/bb9d1fc5b6bd5e468a6e70dfbca3264f to your computer and use it in GitHub Desktop.
breadth-first search is annoying in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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