Today i was told to create tree data structure with the below class,
public class Node(){
private string lable;
private List<Node> children;
}
After started to create the tree, i struck at very first.
It contains Node inside a node. I am totally confused. Well, tree maybe very familiar to you.
You may think Whats this! Its clear. How you getting confused something like that.
For me, this the first time i am trying to create a tree in java. To be honest, i only used setters and getters inside a class in java. With these methods, I cannot think of inserting new nodes after the first level.
I have seen some examples in google and many in stackoverflow. But for a beginner(in tree) like me they looks incomplete. May be they may thought, the OP can continue with that.
If anyone explain me its concept and how to add more children, with any generic example i would be appreciative.
Update:
It may look weired to you, but this is how i started at the beginning.
Node node = new Node();
String label = "Bikes";
ArrayList<Node> children = new ArrayList<Node>();
Node childNode = new Node();
childNode.setLabel("Yamaha");
children.add(childNode);
childNode = new Node();
childNode.setLabel("Suzuki");
children.add(childNode);
childNode = new Node();
childNode.setLabel("Honda");
children.add(childNode);
node.setLabel(label);
node.setChildren(children);
After that, like i told i cannot think for next level.
After doing some searches i have found that they are having a method addChild()
Then i created mine,
public void addChildren(Node node){
children.add(node);
}
I continued like this,
ArrayList<Node> children = new ArrayList<Node>();
node.setLabel(label);
node.addChildren(node);
Now again i strucked here. Again i cannot think of adding more branches to the root node. i hope this makes some what clear.
Listinterface?I have seen some examples in google and many in stackoverflow. But for a beginner(in tree) like me they looks incomplete...