I’m reading a tree data structure fron a json file, and I need to retrieve the vector of
edge tuples, (parent, child), to define a LightGraphs.SimpleDiGraph
and get the node positions via NetworkLayout.Buchheim
layout
An old Python function, that still works and I’d like to convert to a Julia one, is defined by:
import itertools
def edgelist(d, counter = next(itertools.count())):
#d is the dictionary read from the jsonfile
d["name"] = counter() if d["name"] is None else d["name"]
if "children" in d:
for child in d["children"]:
if child:
child["name"] = counter() if child["name"] is None else child["name"]
yield (d["name"], child["name"])
for child in d["children"]:
if child:
for node in edgelist(child, counter):
yield node
Since there isn’t an equivalent for next
to get the next element in Julia Iterators.countfrom()
, only using for...
, as well as no yield
correspondent, I cannot figure out how could I get the tree edges in a Julian way.
LE. An example of tree_dict:
dtree = Dict("name"=> "A",
"children"=>[Dict("name"=>"B",
"children"=> [Dict("name"=>"B1",
"children"=>[Dict("name"=> "B11",
"children"=> [Dict("name"=> "B111")]),
Dict("name"=>"B12"),
Dict("name"=>"B13")]),
Dict("name"=> "B2",
"children"=> [Dict("name"=>"B21"),
Dict("name"=>"B22")])]
),
Dict("name"=>"C",
"children"=> [Dict("name"=>"C1",
"children"=>[Dict("name"=> "C11"),
Dict("name"=>"C12", "children"=> [Dict("name"=> "C121")]),
Dict("name"=>"C13")]),
Dict("name"=> "C2",
"children"=> [Dict("name"=>"C21"),
Dict("name"=>"C22")])]
)
]);