Construct a tree from a matrix