So I'm building this tree which has 1..* Nodes where each node has a list which in themselves can have 1..* nodes.
The first three levels of the tree I can build just fine but then it won't work more if I don't code all the levels which are just plain stupid. The solution is of course to use some kind of recursive method and a BFS.
Basically, in my TreeBuilder class, I want to be able to call tree.getNodesAtDepth(depth))
and get a list of all nodes that are at that depth. Problem is that I don't understand how I can implement the getNodesAtDepth(depth)
method.
All the examples I find are for binary trees. An alternative is to have the addChild method take a depth argument so I can specify at which depth to insert the child at.
In the end, this is what I want: I have a tree with a root. The root has a list which has 4 children nodes. I want to get the 4 children. For each child generate three nodes. So for child 0 has 3 children, child 1 has 3 children, child 3 has 3 children and child 4 has 3 children. And so forth
Maybe a possible soultion is to have a level attribute on each node and search for that node and then return it's parent. Beacuse it's parent should have a list of all the nodes at the searched for node.
expected depth -1
), then collect all children in that depth. You should recurse for all children. Use a temporary variable to increment the depth while you iterate. – mjlowky