第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何創(chuàng)建遞歸方法來(lái)生成二叉樹(shù)?

如何創(chuàng)建遞歸方法來(lái)生成二叉樹(shù)?

ABOUTYOU 2023-10-19 21:45:33
當(dāng)右側(cè)的遞歸迭代到達(dá)狀態(tài) 2 并返回時(shí),問(wèn)題就出現(xiàn)了,因?yàn)楦赣H取了一個(gè)不應(yīng)該取的值。這是我的節(jié)點(diǎn)創(chuàng)建者類,它有右側(cè)和左側(cè):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;    }這是我的遞歸類,我初始化構(gòu)造函數(shù),此時(shí)我開(kāi)始遞歸: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 Recursivelypublic 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(" ");        }它不會(huì)生成我需要的所有節(jié)點(diǎn)。查看包含所有節(jié)點(diǎn)的圖像: 包含我需要的節(jié)點(diǎn)的圖像 ,int 我需要遞歸執(zhí)行而不是掃描。
查看完整描述

1 回答

?
弒天下

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊

為此,您需要在 TreeNode 的構(gòu)造函數(shù)中使用它: System.arraycopy(vectorPosible, 0, this.vectorPosible, 0, vectorPosible.length);


那么代碼將如下所示:


 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;

  }

}

除此之外,您還必須在SolutionTree中聲明兩個(gè)newNode(1和2),因?yàn)槿绻暶饕粋€(gè),它將被相同的向量引用。那么代碼將如下所示:


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;

  }

 }

}




查看完整回答
反對(duì) 回復(fù) 2023-10-19
  • 1 回答
  • 0 關(guān)注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)