我正在做一个家庭作业项目,我从文件中读取连接站列表并创建格式为(键 = 字符串站,值 = ArrayList 连接站)的哈希图,到目前为止一切都很好。
然后,用户可以选择一个家庭电台,此时我正在尝试创建一棵树来表示家里所有可访问的电台。例如,树可能如下所示:
HomeStation
/ \
station1 station2
/ | \
station3 station4 station 5
但是我无法解决如何将这些站点添加到树中,而不仅仅是根及其子项。所以任何人都可以给我一些关于我应该做什么/看什么的指示。
到目前为止,我的 TreeNode 类:
/**
* TreeNode class
* Represents a N-ary tree node
* Uses ArrayList to hold the children.
* @author Ásta B. Hansen (11038973)
*
*/
public class TreeNode {
private String station;
private TreeNode parent;
private List<TreeNode> children;
/**
* Constructor
* @param station - the station to be stored in the node
*/
public TreeNode(String station) {
this.station = station;
parent = null;
children = new ArrayList<TreeNode>(); //Empty list of children
}
/**
* Sets the station in this node
* @param station - the station to be stored
*/
public void setStation(String station) {
this.station = station;
}
/**
* Returns the station in this node
* @return station
*/
public String getStation() {
return station;
}
/**
* Sets the parent of this node
* @param parent - the parent node
*/
public void setParent(TreeNode parent) {
this.parent = parent;
}
/**
* Returns the parent of this node or null if there is no parent
* @return parent
*/
public TreeNode getParent() {
return parent;
}
/**
* Adds a single child to this node
* @param newChild - the child node to be added
*/
public void addChild(TreeNode newChild) {
children.add(newChild);
newChild.setParent(this);
}
/**
* Returns a list of the children of this node
* @return children - the children of the node
*/
public List<TreeNode> getChildren() {
return children;
}
/**
* Returns the number of children this node has
* @return number of children
*/
public int getNumberOfChildren() {
return children.size();
}
/**
* Indicates whether this is a leaf node (has no children)
* @return true if the node has no children
*/
public boolean isLeaf() {
return children.isEmpty();
}
/**
* TODO print preOrder tree
*/
public void printPreOrder() {
}
/**
* TODO print postOrder tree
*/
public void printPostOrder() {
}
}
在主要:
private static void selectHome() {
if(network != null) {
System.out.print("Please enter the name of the home station> ");
homeStation = scan.next();
if(!network.hasStation(homeStation)) { //if station does not exist
System.out.println("There is no station by the name " + homeStation + "\n");
homeStation = null;
} else {
//create the tree with homeStation as root
createTree(homeStation);
}
} else {
System.out.println("You must load a network file before choosing a home station.\n");
}
}
private static void createTree(String homeStation) {
root = new TreeNode(homeStation); //create root node with home station
//TODO Construct the tree
//get list of connecting stations from network (string[])
//and add the stations as children to the root node
for(String stationName : network.getConnections(homeStation)) {
TreeNode child = new TreeNode(stationName);
root.addChild(child);
//then for every child of the tree get connecting stations from network
//and add those as children of the child.
//TODO as long as a station doesn't already exist in the tree.
}
}
编辑: 车站输入文件
Connection: Rame Penlee
Connection: Penlee Rame
Connection: Rame Millbrook
Connection: Millbrook Cawsand
Connection: Cawsand Kingsand
Connection: Kingsand Rame
Connection: Millbrook Treninnow
Connection: Treninnow Millbrook
Connection: Millbrook Antony
Connection: Antony Polbathic
Connection: Polbathic Rame