私は宿題プロジェクトに取り組んでおり、ファイルから接続ステーションのリストを読み取り、形式 (キー = String ステーション、値 = 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