Issue
i have a multiple node tree and i'm trying to fill the tree with specified depth (can be user input or or a constant variable). Each node will have 7 children and each children will have 7 children and so on. the problem is to add children recursively
this is how to do it using normal methods
MyTreeNode root = new MyTreeNode(new int[6][7]);
addChildren(root);// depth1
addChildrenToChildren(root.getChildren());// depth2
for (int i = 0; i < 7; i++) {
addChildrenToChildren(root.getChildren().get(i).getChildren()); // depth3
}
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
addChildrenToChildren(root.getChildren().get(i).getChildren().get(j).getChildren()); // depth4
}
}
methods used
private static void addChildrenToChildren(List<MyTreeNode> children) {
for(MyTreeNode child : children){
addChildren(child);
}
}
private static void addChildren(MyTreeNode root) {
root.addChildren(Arrays.asList(
new MyTreeNode(new int[6][7]),
new MyTreeNode(new int[6][7]),
new MyTreeNode(new int[6][7]),
new MyTreeNode(new int[6][7]),
new MyTreeNode(new int[6][7]),
new MyTreeNode(new int[6][7]),
new MyTreeNode(new int[6][7])
));
}
what i have tried doing
public static void depth(MyTreeNode root, int n){
if (n == 1){
addChildren(root);
} else {
for (int i = 0; i < 7; i++) {
addChildrenToChildren(root.getChildren());
depth(root.getChildren().get(i), n - 1);
}
}
}
myTreeNode class
public class MyTreeNode{
private int[][] grid;
private List<MyTreeNode> children = new ArrayList<MyTreeNode>();
private MyTreeNode parent = null;
public MyTreeNode(int[][] grid) {
this.grid = grid;
}
public void addChild(MyTreeNode child) {
child.setParent(this);
this.children.add(child);
}
public void addChild(int[][] grid) {
MyTreeNode newChild = new MyTreeNode(grid);
this.addChild(newChild);
}
public void addChildren(List<MyTreeNode> children) {
for(MyTreeNode t : children) {
t.setParent(this);
}
this.children.addAll(children);
}
public List<MyTreeNode> getChildren() {
return children;
}
public int[][] getGrid() {
return grid;
}
public void setGrid(int[][] grid) {
this.grid = grid;
}
private void setParent(MyTreeNode parent) {
this.parent = parent;
}
public MyTreeNode getParent() {
return parent;
}
}
Solution
In your attempt at recursion, addChildrenToChildren(root.getChildren())
is executed 7 times -- this is not right. Moreover, you don't need addChildrenToChildren
, as that will be the job of the recursive call. Instead you should call addChildren(root);
also when n
is greater than 1. The only difference with the base case is the loop where the recursive call is made on each child.
I would also treat the base case at n
is 0, so that the main caller could even call it with n
equal to 0 and get the corresponding result (no children are added then):
public static void depth(MyTreeNode root, int n){
if (n <= 0) return; // Nothing to do
addChildren(root);
for (MyTreeNode child : root.getChildren()) {
depth(child, n - 1);
}
}
Answered By - trincot
Answer Checked By - Robin (JavaFixing Admin)