views:

146

answers:

1

I'm trying to make my own tree class but keep getting horribly confused. Basically I'm trying to make a weighted tree. I've made the following node class:

 import java.util.*;

public class subdivNode {

private int nodevalue;
private int nodeID;
private List<subdivNode> childnodes; 

public subdivNode(int value, int id){
 nodevalue = value;
 nodeID = id;
}

public int getValue(){
 return nodevalue;
}

public int getId(){
 return nodeID;
}

public void addChild(subdivNode child){
 childnodes.add(child);
}

public int getNumChildren(){
 return childnodes.size();
}

public subdivNode getChild(int pos){ //return's i'th child
 return childnodes.get(pos);
}
}

And this is the skeleton for my tree class so far:

public class subdivTree {

private subdivNode rootnode;

public subdivTree(){
 rootnode = new subdivNode(0,0);
}

public void addNode(int parent, int value){

}

public int getNodeValue(int node){

 return 0;
}

public int getNumChildren(int node){
 return 0;
}


}

Beyond that I have no idea

EDIT: Sorry for the ambiguity. My question should have been how would I go about implementing the addnode method in subdivTree. The end goal is to create an alogrithim which searches the tree for the path between any two nodes such that the greatest value(adding the value of all the in between nodes) is obtained.

+2  A: 

Before working on addNode, tell us how getNodeValue(int node) is going to work.

You have a rootNode, what method of that are you going call with that "node" value?

I'm not sure, but I think that your interface is broken. I think your concepts of get by position and get by Id are confused.

Draw a picture of the data structure you expect.

djna