How to find first child in tree or last child?

I want to find only the first child of treeleaf or last parent, how can i do it?

Thanks

The naive answer is parentNode.findTreeChildrenNodes().first(). However…

If you are using TreeLayout, you may be sorting the children, by setting TreeLayout.sorting and TreeLayout.comparer (or more flexibly by setting those two properties on TreeVertex). So the naive answer above will only work when TreeLayout.sorting is go.TreeLayout.SortingForwards, which happens to be the default value for that property.

But if you are sorting the children, I don’t know that there is an easy answer. You might need to iterate through the parentNode.findTreeChildrenNodes() collection to find the Node that has the smallest child.location.y, or the largest if you are looking for the last child. However…

Even looking for the child node with the smallest X or Y might not get you the “first” child. For example, if TreeLayout.angle is 270, the first logical child will have the largest Y location. Or if you have set the TreeLayout.breadthLimit, there may be multiple logical rows of children, causing there to be multiple children that may have the smallest X or Y position.