Hi Guys,
A little advise please.
I am trying to create a tree out of following rows of data:
TOP Level1 NotLeaf
Level1 Data1 leaf
TOP Level11 NotLeaf
Level11 Level2 NotLeaf
Level11 Data4 leaf
Level2 Data2 leaf
Level2 Data3 leaf
The last column shows whether a node is a leaf or not. The tree, therefore, would look like:
Top
|
|- Level1, Level11
| |
| |
Data1 Level2, Data4
|
|
Data2, Data3
I am storing each row of the data in a bean and I then put the bean in a list.
public class Data {
private String parent;
private String name;
private int isLeaf;
public Data(String parent, String name, int isLeaf){
this.parent = parent;
this.name = name;
this.isLeaf= isLeaf;
//add to the list
}
//Getters
public List<Data> getDataList(){
return dataList;
}
}
Now for the tree, I wrote a Node class as below:
public class Node {
private final String nodeName;
private final List<Node> children;
public Node(String nodeName) {
this.nodeName = nodeName;
this.children = new ArrayList<Node>(10);
}
public String getNodeName() {
return nodeName;
}
public List<Node> getChildren() {
return Collections.unmodifiableList(children);
}
public void addChild(Node child) {
children.add(child);
}
}
But I can't seem to figure out how to insert the data into the Node and get the relationship as shown in the tree above.
Perhaps lack of sleep is hiding the obvious but any advise would be most helpful.
Thanks