Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#_(def data [1 [11 12] [13 14 [21]] 2])
(def data ["33L" ["32R" ["31L" ["30R" [false]] ["11R" ["01L" ["00R" [true]]] ["00L" [false]]]] ["30L" [false]] ["22L" ["11R" ["01L" ["00R" [true]]] ["00L" [false]]] ["02R" ["01L" ["00R" [true]]] ["00L" [false]]]]] ["31R" ["30L" [false]] ["11L" ["01R" ["00L" [false]]] ["00R" [true]]]] ["22R" ["11L" ["01R" ["00L" [false]]] ["00R" [true]]] ["02L" ["01R" ["00L" [false]]] ["00R" [true]]]]])
(defn next-bfs [[loc queue]]
(let [new-queue (conj queue loc)]
(if-let [right-loc (z/right loc)]
[right-loc new-queue]
(loop [remaining-queue new-queue]
(if (empty? remaining-queue)
[[(z/node loc) :end] nil]
(let [queued-loc (first remaining-queue)]
(if-let [down-loc (z/down queued-loc)]
[down-loc (vec (rest remaining-queue))]
(recur (rest remaining-queue)))))))))
(let [root-loc (z/vector-zip data)
dfs-visits (map z/node (take-while (complement z/end?) (iterate z/next root-loc)))
bfs-visits (map z/node (take-while (complement z/end?) (map first (iterate next-bfs [root-loc []]))))]
(log "dfs" dfs-visits)
(log "bfs" bfs-visits))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment