【问题标题】:Finding the path from the root node to a specified node in a tree using recursion使用递归查找从根节点到树中指定节点的路径
【发布时间】:2020-01-22 01:47:42
【问题描述】:

背景: 这是我第一次使用树,我的任务是使用文件 data.txt 使用皇室创建树

data.txt:

King George VI
King George VI > Princess Margaret
Princess Margaret > David, Viscount Linley
Princess Margaret > Lady Sarah
Lady Sarah > Samuel Chatto
Lady Sarah > Arthur Chatto
David, Viscount Linley > Charles Armstrong-Jones
David, Viscount Linley > Margarita Armstrong-Jones
King George VI > Queen Elizabeth II
Queen Elizabeth II > Charles, Prince of Wales
Charles, Prince of Wales > Prince William of Wales
Prince William of Wales > Prince George of Cambridge
Charles, Prince of Wales > Prince Harry of Wales
Queen Elizabeth II > Anne, Princess Royal
Anne, Princess Royal > Peter Phillips
Peter Phillips > Savannah Phillips
Peter Phillips > Isla Phillips
Anne, Princess Royal > Zara Tindall
Queen Elizabeth II > Andrew, Duke of York
Andrew, Duke of York > Princess Beatrice of York
Andrew, Duke of York > Princess Eugenie of York
Queen Elizabeth II > Edward, Earl of Wessex
Edward, Earl of Wessex > Lady Louise Windsor
Edward, Earl of Wessex > James, Viscount Severn

背景(科特迪瓦)然后我搜索约克的比阿特丽斯公主,并找到她各自的节点。

    public static void main(String[] args)
    {
        //Define a variable to store the root node
        TNode<String> root = null;

        //TODO: SETUP TREE DATA
        //1. Use Scanner to read the data.txt file
        //2. The first line in data.txt is the root node
        //3. For each line in data.txt in the format A > B
        //      - *find* the A node
        //      - add B as a child of A
        try
        {
            Scanner s = new Scanner(new File("data.txt"));
            while(s.hasNextLine())
            {
                String[] split = null;
                if(!s.nextLine().contains(">"))
                {
                    root = new TNode<String>(s.nextLine());
                }else{
                     split = s.nextLine().split(" > ");
                }
                find(root,split[0]).setParent(new TNode(split[3]));
            }
            s.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        //TODO: test printPath method
        TNode<String> child = find(root, "Princess Beatrice of York");
        String path = getPath( child );
        System.out.println(path);

    }

查找方法:

    /**
     *  @return node if its data matches name, or return a child node with data that matches name
     */
    public static TNode<String> find(TNode<String> node, String name)
    {
        //use recursion to check this node and all of its children to see if their data matches the specified name
        if(node.getData().equals(name))
        {
            return node;
        }
        for(int i = 0; i < node.getChildren().size(); i++)
        {
            return find(node.getChildren().get(i),name);
        }
        return null;
    }

目标:返回一个String,其中包含从根节点到指定节点的路径,以'->'分隔,从子节点开始

我的尝试:


    public static String getPath(TNode<String> node)
    {
        //use recursion to concatenate the getPath of this node's parent with this node's data
        if(!node.getParent().equals(null))
            return getPath(node.getParent()) + " -> " + node.getData(); 
        return getPath(node.getParent());
    }

问题:

1.

这是否可能首先发生

2.

如果没有可以连接的字符串,如何连接字符串。

3.

有哪些方法可以帮助我找出递归问题的逻辑

【问题讨论】:

  • 这能回答你的问题吗? Find path to node in Tree?
  • 请限制自己每个问题只问一个问题,并确保您发布的代码是说明您的问题或支持您的问题所必需的最低限度。
  • @MichaelBianconi,如果我没看错,我相信它不会。那个帖子是根的。我正在尝试获取从子节点到根节点的路径。

标签: java tree concatenation


【解决方案1】:

绝对有可能。看一眼就可以从Princess Beatrice of York追溯到King George VI

要创建家族字符串(假设这是您想要的),您可以将每个人存储在 TNode 中,并将姓名与他们之间的 &gt; 连接起来,从根开始。

就学习考虑递归算法而言,从更简单的事情开始,例如字符串的长度或计算数字的阶乘。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    相关资源
    最近更新 更多