2013-05-15 39 views
3

我想建立一棵樹,我想父節點鏈接到基於類似結構的文件路徑的孩子,如下面的一個,其中世界是根:從文件路徑構建樹,我的邏輯正確嗎?

The World 
    The World/Asia 
    The World/Asia/Afghanistan 
    The World/Asia/Iran 
    The World/Asia/China"; 

我想打開它, into: enter image description here

我採取的方法如下。我想知道是否有人能夠幫我指點正確的方向。我不確定我的邏輯是否正確?

public void linkNodeToParent(String path, Node n) 
{ 
    String[] pathNodes = path.split("/"); 
    Node parent = root; 
    for(int i = 0; i < pathNodes.length; ++i) 
    { 
     for(int j = 0; j < parent.getChildren().size(); ++j) 
     { 
      if(parent.getChildren().get(j).getNodeName().equals(pathNodes[i])) 
       parent = parent.getChildren().get(j); 
     } 
    } 

} 
+0

什麼問題?你的問題是什麼? –

+0

我不知道如果我正確實施它,如果我的邏輯是正確的 – audiFanatic

+0

**「我的邏輯是否正確?」** *是一個*問題。如果這是你的問題,請[編輯]進入問題中(http://stackoverflow.com/posts/16560509/edit) –

回答

0

如果添加一個反彈「/」在每個月底,我們在聊天聊,那麼這個程序將工作

void split() 
    { 
     String path= 
       "The World/"+ 
       "The World/Asia/"+ 
       "The World/Asia/Afghanistan/"+ 
       "The World/Asia/Iran/"+ 
       "The World/Asia/China/"; 
     String[] pathNodes = path.split("/"); 

//  for(String s:pathNodes) 
//  { 
//   System.out.println(s); 
//  } 

     String root="The World"; 
     String continent="Asia"; 
     List<String> ls=new ArrayList<String>(); 

     for(int i=0;i<pathNodes.length;i++) 
     { 
      if(pathNodes[i].equals(root)) 
      { 
       if(pathNodes[i+1].equals(continent)) 
       { 
        ls.add(pathNodes[i+2]); 
       } 
      } 
     } 
     for(String s:ls) 
     { 
      System.out.println(s); 
     } 
    } 
5

希望下面的代碼可以幫助你創建你的文件夾結構使用樹

import java.util.*; 
class Tree 
{ 
    class Node 
    { 
     String data; 
     ArrayList<Node> children; 

     public Node(String data) 
     { 
      this.data = data; 
      children = new ArrayList<Node>(); 
     } 

     public Node getChild(String data) 
     { 
      for(Node n : children) 
       if(n.data.equals(data)) 
        return n; 

      return null; 
     } 
    } 

    private Node root; 

    public Tree() 
    { 
     root = new Node(""); 
    } 

    public boolean isEmpty() 
    { 
     return root==null; 
    } 

    public void add(String str) 
    { 
     Node current = root; 
     StringTokenizer s = new StringTokenizer(str, "/"); 
     while(s.hasMoreElements()) 
     { 
      str = (String)s.nextElement(); 
      Node child = current.getChild(str); 
      if(child==null) 
      { 
       current.children.add(new Node(str)); 
       child = current.getChild(str); 
      } 
      current = child; 
     } 
    } 

    public void print() 
    { 
     print(this.root); 
    } 

    private void print(Node n) 
    { 
     if(n==null) 
      return; 
     for(Node c : n.children) 
     { 
      System.out.print(c.data + " "); 
      print(c); 
     } 
    } 

    public static void main(String[] args) 
    { 
     Tree t = new Tree(); 
     t.add("The World"); 
     t.add("The World/Asia"); 
     t.add("The World/Asia/Afghanistan"); 
     t.add("The World/Asia/Iran"); 
     t.add("The World/Asia/China"); // Even if you insert only this statement. 
              // You get the desired output, 
              // As any string not found is inserted 

     t.print(); 
    } 
} 
  1. 「添加」方法需要的文件夾或作爲輸入的整個路徑,它可儲存我按照你的要求在樹上。它接受第一個字符串並檢查它是否已經存在於樹中,否則它會添加它並繼續到下一個字符串(您的術語中的文件夾)。
  2. 打印方法可幫助您驗證樹中數據的存儲。