4

私はツリーを構築しようとしています。次のような構造のようなファイルパスに基づいて、親ノードを子にリンクしたいと思います。ここでは、世界がルートです。

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

私はそれをこれに変えたい: ここに画像の説明を入力

私が取っているアプローチは次のとおりです。誰かが私を正しい方向に向けるために手を差し伸べてくれるかどうか疑問に思っています. 私の論理が正しいかどうかわかりませんか?

 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);
       }
    }

}
4

3 に答える 3

8

以下のコードが、ツリーを使用してフォルダー構造を作成するのに役立つことを願っています

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. 「add」メソッドは、フォルダーまたはパス全体を入力として受け取り、必要に応じてツリーに保存します。最初の文字列を取得し、ツリーに既に存在するかどうかを確認します。それ以外の場合は、それを追加して次の文字列 (用語のフォルダー) に進みます。
  2. print メソッドは、ツリー内のデータの格納を確認するのに役立ちます。
于 2013-05-15T10:11:12.763 に答える
0

チャットで話したように、すべての末尾にバックラッシュ「/」を 1 つ追加すると、このプログラムは機能します。

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);
        }
    }
于 2013-05-15T10:10:02.950 に答える