This is one way to do it functionally, though it's a bit more verbose.
"get-nodes" performs a BFS walk of the tree between two nodes, returning the
set of visited nodes:
(defn get-nodes [from to]
(loop [queue [from]
visited #{}]
(let [[x & xs] queue]
(if-not x
visited
(recur (concat xs (candidates x to))
(conj visited x))))))
With that in hand, "build-tree" can build the adjacency list by simply
calling "candidates" on each node on the walk, ignoring nodes with no
children:
(defn build-tree [from to]
(into {} (for [n (get-nodes from to)]
(when-let [children (seq (candidates n to))]
[n children]))))
user=> (build-tree :a :e)
{:a (:b), :c (:e), :b (:c :d), :d (:e)}
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en