Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
import numpy as np
H = np.array(
    [[ 7, -1,  1, -1],
     [-1, -1,  2,  0],
     [-1, -1,  3,  1],
     [-1, -1,  4,  2],
     [-1, -1,  5,  3],
     [ 6, -1, -1,  4],
     [-1,  5, -1,  4],
     [ 8,  0, -1, -1],
     [-1,  7, -1, -1]])

def T(i):
    children = [(h, j) for j, h in enumerate(H) if h[3] == i]
    children.sort(key = lambda h: h[0][1])
    return {c[1]: T(c[1]) for c in children}

print T(-1)

Thanks to Falko at stack overflow for this answer.