Predict a tree structure out of nodes with different features
Don't use machine-learning for this - you probably don't have millions of training examples.

Instead, what you are looking for is a simple depth-first search.

Begin at the root, then define a priority list, e.g. below-left, below, below-right, in which order to proceed. Don't visit nodes twice, but when first visiting them mark them as seen, and add them to your tree instead.

Alternatively, define a priority pattern of how to define the parent node.  Construct a tree based on this single "best parent" relationship,, but make sure you don't create cycles.