Java (urs)这闻起来像是递归。它们是关于向多个方向深入的算法节奏,你无法通过一个简单的循环完成它。或者当您尝试构建可以包含同一事物的多个实例的事物时,等等。不过要小心。如果您使用的是一种语言(大多数语言,除了一些语言,如……等,其中递归既是面包又是黄油),那么递归往往非常昂贵。如果你能想到一个算法节奏,它产生相同的结果,但不是递归的,它通常是便宜的 如果您决定问题需要递归,那么就着手解决:尝试找到递归的构建块。在您的案例中,是“创建一个节点并将其所有子节点添加到其中”。子节点应该包含它们的子节点,因此在添加子节点时,可以对每个子节点调用相同的步骤(步骤是查找并添加它们的子节点) 我们准备好了吗?在处理递归问题时,我通常觉得即使一切都很完美,也必须有一些事情要做。这是因为你没有从头到尾写算法节奏,而是以一种奇怪的不自然的顺序。对你来说,我们还远远没有准备好 为什么它不是无限的?在处理递归时,很容易让函数无限地调用自己,因此是一个函数。我们需要找到函数的边界。在您的例子中,如果一个节点没有更多的子节点,递归将结束。但是等待:如果连接是双向的,那么两个节点将是彼此的子节点,因此每个节点都将有一个子节点!不知何故,我们需要停止向树中添加节点!我能想到的最简单的解决方案是记住哪些节点是以前添加的,并且只在还没有添加的情况下添加一个节点。节点列表是有限的,因此我们最终将耗尽新节点。如果,则关键字为。每个递归中都应该有一个if。应该有一个条件分支,在那里递归停止 我的算法在做什么?当你觉得自己有进展时,停下来,试着想想你的算法目前在做什么。怎么开始?在某些情况下,您需要在开始递归之前编写几行初始化。在您的例子中,我需要创建根,创建一个字符串列表,并在调用递归之前将根的名称添加到列表中。确保你的算法节奏有一切开始。还要确保它在你想要的时候结束。确保你的条件在正确的地方。试着通过简单的例子来思考

Java (urs)这闻起来像是递归。它们是关于向多个方向深入的算法节奏,你无法通过一个简单的循环完成它。或者当您尝试构建可以包含同一事物的多个实例的事物时,等等。不过要小心。如果您使用的是一种语言(大多数语言,除了一些语言,如……等,其中递归既是面包又是黄油),那么递归往往非常昂贵。如果你能想到一个算法节奏,它产生相同的结果,但不是递归的,它通常是便宜的 如果您决定问题需要递归,那么就着手解决:尝试找到递归的构建块。在您的案例中,是“创建一个节点并将其所有子节点添加到其中”。子节点应该包含它们的子节点,因此在添加子节点时,可以对每个子节点调用相同的步骤(步骤是查找并添加它们的子节点) 我们准备好了吗?在处理递归问题时,我通常觉得即使一切都很完美,也必须有一些事情要做。这是因为你没有从头到尾写算法节奏,而是以一种奇怪的不自然的顺序。对你来说,我们还远远没有准备好 为什么它不是无限的?在处理递归时,很容易让函数无限地调用自己,因此是一个函数。我们需要找到函数的边界。在您的例子中,如果一个节点没有更多的子节点,递归将结束。但是等待:如果连接是双向的,那么两个节点将是彼此的子节点,因此每个节点都将有一个子节点!不知何故,我们需要停止向树中添加节点!我能想到的最简单的解决方案是记住哪些节点是以前添加的,并且只在还没有添加的情况下添加一个节点。节点列表是有限的,因此我们最终将耗尽新节点。如果,则关键字为。每个递归中都应该有一个if。应该有一个条件分支,在那里递归停止 我的算法在做什么?当你觉得自己有进展时,停下来,试着想想你的算法目前在做什么。怎么开始?在某些情况下,您需要在开始递归之前编写几行初始化。在您的例子中,我需要创建根,创建一个字符串列表,并在调用递归之前将根的名称添加到列表中。确保你的算法节奏有一切开始。还要确保它在你想要的时候结束。确保你的条件在正确的地方。试着通过简单的例子来思考,java,tree,n-ary-tree,Java,Tree,N Ary Tree,至少我是这样做的(在回答这个问题时也是这样:)。你试过我的代码吗?太棒了。:)你试过我的密码吗?太棒了。:)嘿,非常感谢你给我一个清晰详细的答案!我必须承认,当涉及到递归时,我似乎总是把我的思想弄得一团糟,但当然现在它看起来很明显,哈哈。我现在有一棵树,我的遍历正在工作:)再次感谢!检查我的编辑。我已经完成了一个关于递归的小教程,因为学习它很重要。嘿,非常感谢你给出了一个清晰详细的答案!我必须承认,当涉及到递归时,我似乎总是把我的思想弄得一团糟,但当然现在它看起来很明显,哈哈。我现在有一棵树,我

至少我是这样做的(在回答这个问题时也是这样:)。

你试过我的代码吗?太棒了。:)你试过我的密码吗?太棒了。:)嘿,非常感谢你给我一个清晰详细的答案!我必须承认,当涉及到递归时,我似乎总是把我的思想弄得一团糟,但当然现在它看起来很明显,哈哈。我现在有一棵树,我的遍历正在工作:)再次感谢!检查我的编辑。我已经完成了一个关于递归的小教程,因为学习它很重要。嘿,非常感谢你给出了一个清晰详细的答案!我必须承认,当涉及到递归时,我似乎总是把我的思想弄得一团糟,但当然现在它看起来很明显,哈哈。我现在有一棵树,我的遍历正在工作:)再次感谢!检查我的编辑。我已经完成了一个关于递归的小教程,因为学习它很重要。
           HomeStation
           /      \
    station1      station2
                /   |     \
        station3 station4 station 5
/**
* 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
private static void addNodesRecursive(TreeNode node) {
    for(String stationName : network.getConnections(node)) {
        TreeNode child = new TreeNode(stationName);
        node.addChild(child);
        addNodesRecursive(child);
    }   
}
private static void addNodesRecursive(TreeNode node, List<TreeNode> addedList) {
    for(String stationName : network.getConnections(node)) {
        TreeNode child = new TreeNode(stationName);
        node.addChild(child);
        addedList.add(child);
        addNodesRecursive(child, addedList);
    }   
}
private static void addNodesRecursive(TreeNode node, List<String> addedList) {
    for(String stationName : network.getConnections(node)) {
        if (!addedList.contains(stationName)) {
            TreeNode child = new TreeNode(stationName);
            node.addChild(child);
            addedList.add(child);
            addNodesRecursive(child, addedList);
        }
    }   
}
private static void createTree(String homeStation) {
    root = new TreeNode(homeStation);
    List<String> addedList = new ArrayList<String>();
    addedList.add(homeStation);
    addNodesRecursive(root, addedList);
}