I am struggling with data trees in Grasshopper trying to match two data trees in input. What I need to know is the list of object of the tree T2 correspondent to any given object of the tree T1. I have been looking for a function in the SDK but I could not find it. I have developed a script to do it but it is very inefficient and still I do not know how to handle trees with short paths. Also I understand that the criterion is the longest list on the paths and again in the matching branches but I am not sure.
Is there any function that can be used for this task? At least to find matching paths.
Thanks in advance,

Does the problem reduce to iterating over all the values in one tree and seeing if they are present in some other collection? Or do you specifically need to figure out the loci of both values in both trees when they are identical?

Ah I see. The default Grasshopper tree iterator which components use to match up input data fully ignores the paths themselves and only concerns itself with the order of paths.

If the number of paths differs, then the last branch is used over and over again to match the subsequent branches in the other tree. The same goes for items. If your path counts and list lengths are the same between both trees, it will significantly simplify the looping.

var treeA = ColourTree;
var treeB = PointTree;
int maxPathCount = Math.Max(treeA.PathCount, treeB.PathCount);
for (int k = 0; k < maxPathCount; k++)
{
int kA = Math.Min(k, treeA.PathCount - 1);
int kB = Math.Min(k, treeB.PathCount - 1);
var listA = treeA.Branch[kA];
var listB = treeB.Branch[kB]
int maxListLength = Math.Max(listA.Count, listB.Count);
for (int i = 0; i < maxListLength; i++)
{
int iA = Math.Min(i, listA.Count - 1);
int iB = Math.Min(i, listB.Count - 1);
var colour = listA[iA];
var point = listB[iB];
// (colour, point) is the matched pair.
}
}

If the trees have the same topology, it simplifies to this:

var treeA = ColourTree;
var treeB = PointTree;
for (int k = 0; k < treeA.PathCount; k++)
{
var listA = treeA.Branch[k];
var listB = treeB.Branch[k]
for (int i = 0; i < listA.Count; i++)
{
var colour = listA[i];
var point = listB[i];
// (colour, point) is the matched pair.
}
}

@DavidRutten Could you help me to deal with more complex structure than in this example, please? I would like to learn how to iterate through complex data trees. Is there some routine to learn? A â€śuniversalâ€ť input script that matches relevant data together? I have a script that works just for exact data structure. How to make the script more robust and universal so it works for different structures as well?

I know that I can repeat â€śGâ€ť and â€śRPâ€ť input trees to march â€śTPâ€ť tree, but I guess that it should be simpler than that.