Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

A possible solution:

def sortkey_to_digraph(sk,G,root):
    if not sk:
        return
    G.add_vertex(root)
    d = sk.pop(0)
    for _ in range(d):
        child = max(G.vertices()) + 1
        sortkey_to_digraph(sk, G, child)
        G.add_edge(root, child)

# conversion function from RootedTree to a DiGraph with a root at 0
def rt_to_digraph(T):
    D = DiGraph()
    sortkey_to_digraph(list(T.sort_key()),D,0)
    return D

for T in RootedTrees(5):
    D = rt_to_digraph(T)
    D.show(layout='tree')
    print( { v: D.neighbors_out(v) for v in D.vertices() } )

To see how it works, run it at Sagecell.