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

2
Contributors
1
2
Views
8 Years
Discussion Span
Last Post by moutanna

Hi
There is may way to do that, this is about the linear representation of tree:

``````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)) {

for (int k = 0; k <= j; ++k) {
System.out.print(ss[k]);
}
System.out.println();
ss[j] = "---";
} else {
ss[j] = "---";
}
}
}``````

Hope it helps

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.