繁体   English   中英

使用递归查找从根节点到树中指定节点的路径

[英]Finding the path from the root node to a specified node in a tree using recursion

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

数据.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

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

    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;
    }

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

我的尝试:


    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.

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

这是绝对可能的。 单看它,就可以追溯到从Princess Beatrice of YorkKing George VI

要创建家庭行字符串(假设这是您想要的),您可以将每个人存储在一个TNode ,并从根开始用>将名字连接起来。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM