I have a question concerning handling DataTrees within the Grasshopper C# Component.

So, I have a given set of planes that is structured as followed:
{0;0}
{0;1}
{0;2}
{0;3}

{1;0}
{1;1}
{1;2}
{1;3}
.
.
.
etc

where each of the branches holds a collection of geometry (so 3 dimensions in total).

I have created a DataTree as well as a 3-dimensional List<> structure for comparison like so:

DataTree < Plane > jointsTree = new DataTree();
List<List<List>> jointsList = new List<List<List>>();

Iâ€™m running into trouble when I try to iterate with for-loops using the datatree.
for example:
Assuming I have 2 joints each with 4 members as illustrated above, If I access the property jointsList.Count it returns the value of 2.
Calling jointsList[0].Count returns 4, which is the number of members of that joint.

However, using the DataTree structure there is no way to get the same counting method. if I call the property jointsTree .BranchCount, it returns the value of 8, which is the total number of members rather than the number of joints.
Calling jointsTree.Branch(0).Count returns the length of {0,0}, the number of geometry in that particular branch rather than the number of members, and calling **jointslist[4].Count returns the number of members of {1;0}, which basically means that the tree is somehow collapsed into two levels?

Using joints.Branch(i,j) i can access the data in multiple dimensions, but that assumes I have a way to calculate the max value for i and j â€¦

So, either I am missing something vital (I hope thats the case) or the DataTree structure seems unable to handle multidimensional data.

DataTree is a SortedList<GH_Path, List< object>>, so is just two dimensional, not a real tree structure. Itâ€™s like a container of labeled lists.
To count the number of nested branches you must create your own implementation, it is not difficult if you understand the GH_Path (a list of numbers).

I did not know about the SortedList, thanks for pointing that out, @Dani_Abalde! Learned something new.
Also thanks for the brief demonstration and the gh file, both very helpful @PeterFotiadis

hereâ€™s my attempt at counting the branches:
//count branches, should be 27
int currentBranch = 0;
int branchCount= 1;
foreach (GH_Path path in x.Paths)
{
if (path[0] != currentBranch)
{
branchCount++;
currentBranch = path[0];
}
}

//count sub branches in branch 0, should be 4
int subBranchCount = 0;
foreach (GH_Path path in x.Paths)
{
if (path[0] == 0)
{
subBranchCount++;
}
}
Print("first level branches count: " + branchCount.ToString());
Print("second level branches of branch 0: " + subBranchCount);

Iâ€™m sure thereâ€™s more efficient ways to count it, but at least I understand whatâ€™s going on.

You can also use a SortedList to perform that search.

private void RunScript(DataTree<System.Object> T, ref object A)
{
SortedList<GH_Path, int> dic = new SortedList<GH_Path, int>();
foreach(GH_Path path in T.Paths){
GH_Path parent = path.CullElement();
if(dic.ContainsKey(parent)){
dic[parent] += 1;
} else {
dic.Add(parent, 1);
}
}
A = dic;
//A = dic[SomePathOfT.CullElement()]; //gives you the number of subbranches of parent of dimension 1 using a subbranch in dimension 2.
}

Splendid. Suggestions work well, particularly like the HashSet method! @PeterFotiadis thanks for posting these strange, but informative scripts. Are these part of a class?