tags:

views:

981

answers:

3

I'm a newbie in Java. I want to build a tree with one root node and multiple children nodes with some weight on each branch. Can somebody help me in this.

+2  A: 

This is just a sketch to get you started, and could be improved a lot. But your basic members could be as follows:

public class WeightedTree {
   private double weight;
   private List<WeightedTree> children;
}

I don't want to write more in case it's a homework question, but if you have specific follow up feel free to comment.

Nick Fortescue
While functional, I think it's not a good idea to name node objects as trees... so maybe call it WeightedTreeNode instead?
StaxMan
+4  A: 

Sample code:

class Node {
    public int weight;
    public List<Node> children = new ArrayList<Node> ();
}

Node root = new Node ();
Aaron Digulla
A: 

Hi, Are there any classes that are used for tree building and that can be imported? I too want to create a tree using Java!

fixxxer