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