Search code examples
javarecursionbinary-treebacktracking

How to create a recursive method to generate a Binary Tree?


The problem comes when the recursive iteration on the right reaches state 2 and is returned, since the father takes a value that he should not.

This is my node creator class that it have the right and left side to go:

public class TreeNodo{
    public byte vectorPosible[]; 
    public int stage; //my stages and index posision of array

    public TreeNodo right; // if goes to right mean 1, in index posision of array
    public TreeNodo left; // if goes to right mean 0, in the index posision of array

    //contructor
    public TreeNodo(byte vectorPosible[], int currentStage){
        this.vectorPosible=vectorPosible;
        this.stage=currentStage; 
        this.right=null;
        this.left=null;
    }
}

This is my recursive class, it i initialize the constructor and in this moment i start recursion:

public class SolutionTree{

TreeNodo root; //it saves the vector [-1,-1] that its initial stage

//contructor 
public SolutionTree(byte vectorPosible[], int currentStage){
    this.root=new TreeNodo(vectorPosible, currentStage);
    this.generarTreePSR(root, vectorPosible, vectorPosible.length, currentStage+1); //Generate a Tree of Possible Solutions Recursively
}

//Metod to insert Tree Node Recursively

public static void generarTreePSR(TreeNode father,byte vectorPosible[],int maxStage, int currentStage){  //in this case maxStage is 2

    TreeNode newNode= new TreeNode(vectorPosible, currentStage);
    System.out.println("newNode.left: "+(newNode.left==null));
    System.out.println("newNode.right: "+(newNode.right==null));
    System.out.println();       

    System.out.println("newNode stage: "+newNode.stage);

    if(newNode.stage==maxStage){ //BASE CASE, IF STAGE == 2
        System.out.println("Reached this stage max: "+newNode.stage);
        System.out.println("I return");
        return;
    }else{ // it isn't in the base case, so tree follow growing

        System.out.print("Look i'm father and i'm before of left, my vector is: ");
        for (int j=0;j<father.vectorPosible.length;j++) { //i show the father vector's
            System.out.print(father.vectorPosible[j]);
            System.out.print(" ");
        }
        System.out.println();


        System.out.println("I'm before if Left, and it is: "+(father.left==null));
        if(father.left==null){
                newNode.vectorPosible[newNode.stage]=0; //insert 0
                father.left=newNode; //asign node

                for (int j=0;j<father.left.vectorPosible.length;j++) { //i show what i insert into this left node
                    System.out.print(father.left.vectorPosible[j]);
                    System.out.print(" ");
                }
                System.out.println();
                System.out.println("Nodo added left with success");
                System.out.println();
                generarTreePSR(father.left, father.left.vectorPosible, maxStage, currentStage+1);
        }

        System.out.print("Look i'm father and i'm before of right, my vector is: ");
        for (int j=0;j<father.vectorPosible.length;j++) { //i show the father vector's
            System.out.print(father.vectorPosible[j]);
            System.out.print(" ");
        }
        System.out.println();

        System.out.println("I'm before if Right, and it is: "+(father.right==null));
        if(father.right==null){
                newNode.vectorPosible[newNode.stage]=1; //insert 1
                father.right=newNode; //asign node

                for (int j=0;j<father.right.vectorPosible.length;j++) {  //i show what i insert into this right node
                    System.out.print(father.right.vectorPosible[j]);
                    System.out.print(" ");
                }
                System.out.println();
                System.out.println("Nodo added right with success");
                System.out.println();
                generarTreePSR(father.right, father.right.vectorPosible, maxStage, currentStage+1);
        }
        return;
    }
}
}

This is my Main class to run it with a vector what i want:

public class TryTree{

public static void main(String[] args) {

    byte vectorPosibles[]={-1,-1};
    SolutionTree tree=new SolutionTree(vectorPosibles); //tree of posible solutions and it need to pass a vector [-1,-1]

  }
}

It isn't generate all the nodes what i need. Look the image with all nodes: Image with nodes what i need , int I need do it recursively and not with scan.


Solution

  • For this to work, you need use this in the constructor TreeNode: System.arraycopy(vectorPosible, 0, this.vectorPosible, 0, vectorPosible.length);

    Then the code would look like this:

     public class TreeNodo{
        public byte vectorPosible[]; 
        public int stage; //my stages and index posision of array
    
        public TreeNodo right; // if goes to right mean 1, in index posision of array
        public TreeNodo left; // if goes to right mean 0, in the index posision of array
    
        //contructor
        public TreeNodo(byte vectorPosible[], int currentStage){
          this.vectorPosible=vectorPosible;
          this.stage=currentStage; 
          this.right=null;
          this.left=null;
      }
    }
    

    In addition to this, you must declare two newNode (1 and 2) in SolutionTree, because if you declare one, it will be referenced with the same vector. Then the code would look like this:

    public class SolutionTree{
    
      TreeNodo root; //it saves the vector [-1,-1] that its initial stage
    
      //contructor 
      public SolutionTree(byte vectorPosible[], int currentStage){
          this.root=new TreeNodo(vectorPosible, currentStage);
          this.generarTreePSR(root, vectorPosible, vectorPosible.length, currentStage+1); //Generate a Tree of Possible Solutions Recursively
      }
    
      //Metod to insert Tree Node Recursively
    
      public static void generarTreePSR(TreeNode father,byte vectorPosible[],int maxStage, int currentStage){  //in this case maxStage is 2
    
        if(currentStage==maxStage){ //BASE CASE, IF STAGE == 2
          System.out.println("Reached this stage max: "+currentStage);
          System.out.println("I return");
          return;
        }else{ // it isn't in the base case, so tree follow growing
    
          //NEW TREE NODE1
            TreeNode newNode1=new TreeNode(father.getVectorPos(), currentStage); //<------Solucion
            newNode1.stage=currentStage;
    
          //NEW TREE NODE2
            TreeNode newNode2= new TreeNode(father.getVectorPos(), currentStage); //<------Solucion
            newNode2.stage=currentStage;
    
    
          System.out.print("Look i'm father and i'm before of left, my vector is: ");
          for (int j=0;j<father.vectorPosible.length;j++) { //i show the father vector's
              System.out.print(father.vectorPosible[j]);
              System.out.print(" ");
          }
          System.out.println();
    
    
          System.out.println("I'm before if Left, and it is: "+(father.left==null));
          if(father.left==null){
                  newNode.vectorPosible[newNode.stage]=0; //insert 0
                  father.left=newNode; //asign node
    
                  for (int j=0;j<father.left.vectorPosible.length;j++) { //i show what i insert into this left node
                      System.out.print(father.left.vectorPosible[j]);
                      System.out.print(" ");
                  }
                  System.out.println();
                  System.out.println("Nodo added left with success");
                  System.out.println();
                  generarTreePSR(father.left, father.left.vectorPosible, maxStage, currentStage+1);
        }
    
          System.out.print("Look i'm father and i'm before of right, my vector is: ");
          for (int j=0;j<father.vectorPosible.length;j++) { //i show the father vector's
              System.out.print(father.vectorPosible[j]);
              System.out.print(" ");
          }
          System.out.println();
    
          System.out.println("I'm before if Right, and it is: "+(father.right==null));
          if(father.right==null){
                  newNode.vectorPosible[newNode.stage]=1; //insert 1
                  father.right=newNode; //asign node
    
                  for (int j=0;j<father.right.vectorPosible.length;j++) {  //i show what i insert into this right node
                      System.out.print(father.right.vectorPosible[j]);
                      System.out.print(" ");
                  }
                  System.out.println();
                  System.out.println("Nodo added right with success");
                  System.out.println();
                  generarTreePSR(father.right, father.right.vectorPosible, maxStage, currentStage+1);
          }
          return;
      }
     }
    }