paths = [
"nodeA1",
"nodeA1/nodeB1/nodeC1",
"nodeA1/nodeB1/nodeC1/nodeD1/nodeE1",
"nodeA1/nodeB1/nodeC2",
"nodeA1/nodeB2/nodeC2",
"nodeA3/nodeB2/nodeC3"
]


you have list of strings there. how can i create a tree by system.out.println from all those paths.

nodea1
----nodeb1
-----------nodec1
-------------------noded1
------------------------------nodee1
-----------nodec2
-----nodeb2
-----------nodec2
nodea3
-------nodeb2
------------nodec3

Hi
There is may way to do that, this is about the linear representation of tree:
Your code should look lik:

String path[] = {
            "nodeA1",
            "nodeA1/nodeB1/nodeC1",
            "nodeA1/nodeB1/nodeC1/nodeD1/nodeE1",
            "nodeA1/nodeB1/nodeC2",
            "nodeA1/nodeB2/nodeC2",
            "nodeA3/nodeB2/nodeC3"
        };
        //The string at the i postion
        String s;
        //Array for holding the split result
        String[] ss;
        //List for holding the unique axis existing in the structure
        List<String> subtree = new ArrayList<String>();
        for (int i = 0; i < path.length; ++i) {
            String axis="";
            s = path[i];
            ss = s.split("/");
            for (int j = 0; j < ss.length; ++j) {
               
               axis+=ss[j]+"/";
                if (!subtree.contains(axis)) {
                    
                    subtree.add(axis);
                    for (int k = 0; k <= j; ++k) {
                        System.out.print(ss[k]);
                    }
                    System.out.println();
                    ss[j] = "---";
                } else {
                    ss[j] = "---";
                }
            }
        }

Hope it helps

This article has been dead for over six months. Start a new discussion instead.