Today I was told to create a tree data structure with the underlying class,
public class Node(){
private string lable;
private List<Node> children;
}
After starting to create a tree, I hit first.
It contains the Node inside the node. I am completely confused. Well, a tree can be very familiar to you. You might think something like this.
This is the first time for me when I try to create a tree in java. Honestly, I used only setters and getters inside a class in java. Using these methods, I cannot think of inserting new nodes after the first level.
I saw several examples on google and many on stackoverflow. But for a novice (in a tree) like me, they look incomplete. Perhaps they might think that the OP could continue this.
If someone would explain to me his concept and how to add more children, with some general example I would be grateful.
Update:
, .
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);
, , .
, addChild()
,
public void addChildren(Node node){
children.add(node);
}
:
ArrayList<Node> children = new ArrayList<Node>();
node.setLabel(label);
node.addChildren(node);
. node. , , .
user405398