You can do this using the recursive generator function. I assume that the root of the node in the tree always appears in front of all its children in the source list.
tree = [[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [5, 6], [4, 6], [3, 6],
[0, 7], [7, 6], [8, 9], [9, 6]]
paths = {}
for t in tree:
if t[0] not in paths: paths[t[0]] = []
paths[t[0]].append(tuple(t))
used = set()
def get_paths(node):
if node[1] in paths:
for next_node in paths[node[1]]:
used.add(next_node)
for path in get_paths(next_node):
yield [node[0]] + path
else:
yield [node[0], node[1]]
for node in tree:
if tuple(node) in used: continue
for path in get_paths(node):
print path
Output:
[0, 1, 2, 3, 6]
[0, 1, 2, 4, 6]
[0, 1, 2, 5, 6]
[0, 7, 6]
[8, 9, 6]
: node. node, , , node , . node, node, .
, . , , node .