Created
June 7, 2018 05:09
-
-
Save Sophia-Gold/4d5113a001ac1835bafa6ceab9d5917f 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