Skip to content

Instantly share code, notes, and snippets.

@darwin
Created September 23, 2015 17:28
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 darwin/6fbc796aa6a8a17e71b3 to your computer and use it in GitHub Desktop.
Save darwin/6fbc796aa6a8a17e71b3 to your computer and use it in GitHub Desktop.
#_(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