I've been doing this task and can't wrap my head around how to convert my for cycle to be recursive one AND find the depth of my tree. Is it even possible to cover all the tree's leaves without a for loop? Because a tree can have many branches and I am not sure how to measure the depth without the loop.
static int RecursiveMethodMeasureDepth(Branch branch)
{
int value = 1;
int highestValue = 1;
for (int i = 0; i < branch.Count(); i++)
{
value = RecursiveMethodMeasureDepth(branch.GetBranch(i)) + 1;
highestValue = value > highestValue ? value : highestValue;
}
return highestValue;
}
if anyone is wondering about the Branch class, there it is:
public class Branch
{
private List<Branch> branches;
public Branch()
{
branches = new List<Branch>();
}
public void AddBranch(Branch branch)
{
branches.Add(branch);
}
public Branch GetBranch(int index)
{
return branches[index];
}
public int Count()
{
return branches.Count;
}
}
I added a picture of a tree bellow and a method that creates same data structure tree:
static Branch initializeTree()
{
Branch root = new Branch();
Branch branch2 = new Branch();
Branch branch3 = new Branch();
root.AddBranch(branch2);
root.AddBranch(branch3);
Branch branch4 = new Branch();
branch2.AddBranch(branch4);
Branch branch5 = new Branch();
Branch branch6 = new Branch();
Branch branch7 = new Branch();
branch3.AddBranch(branch5);
branch3.AddBranch(branch6);
branch3.AddBranch(branch7);
Branch branch8 = new Branch();
branch5.AddBranch(branch8);
Branch branch9 = new Branch();
Branch branch10 = new Branch();
branch6.AddBranch(branch9);
branch6.AddBranch(branch10);
Branch branch11 = new Branch();
branch9.AddBranch(branch11);
return root;
}
[example of a tree][1] [1]: https://i.stack.imgur.com/BqYU2.png