user65457
user65457

Reputation: 247

Building a tree in Java

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.

Upvotes: 0

Views: 8471

Answers (2)

Aaron Digulla
Aaron Digulla

Reputation: 328556

Sample code:

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

Node root = new Node ();

Upvotes: 7

Nick Fortescue
Nick Fortescue

Reputation: 44153

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.

Upvotes: 3

Related Questions