I'm working on some Python code where I have a representation of data as n-ary trees where n is given by the user. The arity of the tree is definite but I'm struggling with an algorithm to enumerate the path from the root to each element.
For example, if I have a 3-ary tree
.
/|\
/ | \
/ | \
/ | \
. . .
/|\ /|\ /|\
a b cd . hi j k
/|\
e f g
represented by the nested list
[[a, b, c], [d, [e, f, g], h], [i, j, k]]
I'd like to get a list of tuples like
[(a, 00), (b, 01), (c, 02), (d, 10), (e, 110), (f, 111), (g, 112), (h, 12), (i, 20), (j, 21), (k, 22)]
I did find a similar problem here Enumerating all paths in a tree but it's not quite what I need and I'm not sure how I might achieve the kind of enumeration I'm looking for.