4

I am trying to build a tree and I would like to link parent nodes to children based on a filepath like structure such as the one below, where The World is the root:

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

I want to turn it into this: enter image description here

The approach I am taking is as follows. I am wondering if someone could give me a hand in pointing me in the right direction. I am not sure if my logic is correct?

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

}
audiFanatic
  • 2,296
  • 8
  • 40
  • 56

3 Answers3

8

Hope the below code helps you in creating your folder structure using Tree

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. The "add" method takes folder or the entire path as input and it stores it in the tree as you required. It takes the first string and checks if it's already present in the tree other wise it adds it and proceed to the next string (folder in your terms).
  2. The print method help you verify the storage of the data in tree.
asifsid88
  • 4,631
  • 20
  • 30
0

Consider to use the new NIO.2 API.

Every Node could hold a Path object.

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Paths.html

Puce
  • 37,247
  • 13
  • 80
  • 152
0

If you add one backlash "/" at every end ,as we talked in chat, then this program will work

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);
        }
    }
anshulkatta
  • 2,044
  • 22
  • 30