Build tree of segments.