2016-10-23 6 views
1

右ノードを左ノードに追加する際に問題が発生しているようです。 私はここで予約注文予約注文トラバーサルツリー

Fred 1900 
2 
John 1925 
3 
Mary 1950 
2 
Jason 1972 
0 
Heather 1975 
2 
Sydney 2002 
0 
Hailey 2005 
0 
John 1951 
1 
Amy 1983 
0 
Fred 1953 
3 
Mark 1977 
0 
Sarah 1979 
1 
Michael 2005 
0 
Adam 1982 
0 
Joan 1927 
2 
Susan 1949 
0 
David 1952 
1 
Fred 1980 
0 

にリストされている入力ファイル(.txt)を持っているが、私のNodeクラスです:

public class Node { 
    public String name; 
    public int year; 
    //this will help with determining the parent in the insertion process 
    public int children;  
    public Node parent; 
    public Node left; 
    public Node right; 


    public Node(String name, int year, int children){ 
     this.name = name; 
     this.year = year; 
     this.children = children; 
    } 
} 

私のノードが正常に作成されていると仮定すると、私が持っているように見えます実際のツリーを作成する問題。

public class FamilyTree {  

    public Node familyTree; 

    private Node pivotalNode; 
    private Node parent; 
    private int children = 0; 
    //This method adds a family member to the family tree  
    public void add(Node newNode){ 
     familyTree = add(familyTree,newNode); 
    } 
    private Node add(Node familyTree, Node newNode){   
     if(familyTree == null){ 
      children = newNode.children; 
      newNode.parent = parent; 
      familyTree = newNode; 

     } 
     else if(children > 0){ 
      parent = familyTree; 
      familyTree.left = add(familyTree.left, newNode); 
      pivotalNode = familyTree.left; 
     } 
     else if(children == 0){   

      familyTree.right = add(familyTree.right, newNode); 
      return pivotalNode; 
     }   
     return familyTree; 
    } 
} 

結果は次のようにツリーを表示するには、次のとおりです。

public class Operations { 

    //Not necessary but it helps me to get more organized. Just want to extract information first. 
    public static ArrayList<String> information = new ArrayList<String>(); 

    public static void main(String args[]){ 
     //extract information from file 
     getFileContents(); 

     //Object initialization 
     FamilyTree family = new FamilyTree(); 

     //some useful variables for loop below 
     int children =0; 
     String[] splitted = null; 
     Node member = null;   

     for(int i=0; i<information.size(); i++){ 
      //Every other line in the text file perform a different operation 
      if(i % 2 == 1){ 
       try{ 
        children = Integer.parseInt(information.get(i)); 
        member = new Node(splitted[0], Integer.parseInt(splitted[1]), children); 
        family.add(member); 
       } 
       catch(Exception e){ 
        //this determines if the pattern is broken 
        break; 
       } 
      } 
      if(i % 2 == 0){        
       splitted = information.get(i).split("\\s+"); 
       //this determines a pattern difference     
       if(splitted.length < 2){ 
        break; 
       } 
      } 
     } 
     System.out.print("hi"); 
    } 
    //Pretty self explanatory. Read each line of the file and store it into an array. 
    //Not necessary as everything could technically be done at once (insertion), but this keeps me 
    //more organized to put everything together later on 
    public static void getFileContents(){ 
     try{ 
      BufferedReader br = new BufferedReader(new FileReader("includes\\assn2in.txt"));    

      String line; 
      String info; 

      while ((line = br.readLine()) != null) { 
       info = line.replaceAll("\\s+", " "); 
       information.add(info);    
      }    
      br.close(); 


     } 
     catch(IOException e){ 
      System.out.println("Error: "+e); 
     } 
    } 
} 

任意の助けをいただければ幸いありがとう:enter image description here

は、ここに私の主な方法です。つまり、それは子供たちを表現するためにleftrightメンバーが含まれています - - あなたが持っている

答えて

0

一つの大きな問題は、あなたのNodeクラスモデル二分木構造があることであるが、データ自体は、この構造に準拠していません。ダイアグラムを見ると、ノードには3人の子供が多いことがわかります(たとえば、John 1925とFred 1953)。限りデータファイルからツリーを構築するとして、私は次のようにStack構造を使用することになり

public class Node { 
    public String name; 
    public int year; 
    //this will help with determining the parent in the insertion process 
    public int expectedNumberOfChildren;  
    public Node parent; 
    public ArrayList<Node> children; 

    public Node(String name, int year, int expectedNumberOfChildren){ 
     this.name = name; 
     this.year = year; 
     this.expectedNumberOfChildren = expectedNumberOfChildren; 
     this.children = new ArrayList<Node>(expectedNumberOfChildren); 
    } 
} 

:あなたはあなたのNodeは子供の任意の数を扱うことができるようにArrayList代わりのleftrightを使用する必要がありますアルゴリズム(擬似コード):

  • 空のスタックを作成します。
  • "root"というノード変数をnullに初期化します。
  • 読み取りのためにデータファイルを開きます。
    • は、次の名前、年およびファイルから子ども数の期待値を読む:ファイルの終わりではないものの
    • 名前、年、および予想される子の数から新しいNodeを作成します。
    • ルートノードがnullの場合:
      • ルートノードを新しいノードに設定します。
      • 新しいノードをスタックに押し込みます。
    • そうでない場合:
      • は(この "親" と呼ぶ)スタックから最後のノードをポップ。
      • 新しいノードの親をスタックにポップした親ノードに設定します。
      • 新しいノードを親の子リストに追加します。
      • 親の子のリストの実際のサイズが、ファイルから最初に読み込まれた親の予想される子の数より少ない場合は、親をスタックに戻します。
      • 新しいノードの予想される子ノードの数が0より大きい場合は、ノードをスタックにプッシュします。
  • ファイルを閉じます。

これだけです。ループが終了すると、入力ファイルが整形式であると仮定して、 "ルート"ノードが完全に構築されたツリーを指します。 (その後スタックは必要なくなりました)