Skip to content

Instantly share code, notes, and snippets.

@ddeaguiar
Created October 26, 2012 21:41
Show Gist options
  • Save ddeaguiar/3961734 to your computer and use it in GitHub Desktop.
Save ddeaguiar/3961734 to your computer and use it in GitHub Desktop.
clojure exercise
(def tree [:f [:b [:a nil nil] [:d [:c nil nil] [:e nil nil]]] [:g nil [:i [:h nil nil] nil]]])
(fact
(depth-first-preorder tree) => [:f :b :a :d :c :e :g :i :h])
;cheating? =)
(defn depth-first-preorder
"Perform a preorder traversal of a tree, depth-first."
[tree]
(filter #(not (nil? %)) (flatten tree)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment