I have created the following graph using Clojure Zipper
A
/ | \
B C D
/ \
E F
using the following code:
(require '[clojure.zip :as z])
(def g (z/vector-zip ["A" ["B" "C" "D"["E" "F"]]]))
Now I want to create a Visualization in d3, So that I want to represent the graph in EDN format like,
[{:from "A" :to "B"}
{:from "A" :to "C"}
{:from "A" :to "D"}
{:from "D" :to "E"}
{:from "D" :to "F"}]
I've tried this
(loop [t g]
(if-not (z/end? t)
(do
(if-not (z/branch? t)
(println {:from (-> t (get 1) :ppath :l) :to (z/node t)})
)
(recur (z/next t))
)
)
)
The only problem is with child E & F, I could not track its parent node D.
You could slightly change the syntax for your tree there to have bascially a pair of parent and childs stored in a vector and then roll your own zipper. E.g.
I think that @cfrick answer makes sens. It is more convenient to have pairs of [parent [children]] in your tree vector. But nonetheless, here is a solution where you can keep your structure and use
vector-zip
.parent-node
takes the current zipper and returns the parent node in the define structure. E.g when zipper is on node:c
,parent-node
returns:a
.edge-seq
build a lazy-seq of the graph edges. It can be useful to filter specific edges.With the following vector/tree :
Keep only edges from
:d