我有一个节点树。

class Node {
    String mName;
    List<Node> children = new ArrayList<>();
    Node(String name) {
        mName = name;
    }
}

我想检查所有的树节点,如果我发现有一个节点的mname是“leonardo”,我想把所有leonardo的孩子都加到leonardo的父亲那里,然后把leonardo自己移走。
例如
初始树是:
                        Jessy
                   /      |       \
 Leonardo                John                  Leonardo
 /     \                 /   \                  /   \
Rafael  David      Leonardo   Keren         Denis  Leonardo
                                                       |
                                                   Phillipe

结果应该是:
                        Jessy
                   /      |       \
 Rafael  David           John         Denis  Phillipe
                          |
                        Keren

(关于列奥纳多、丹尼斯、菲利普和列奥纳多的子树结果的解释:杰西的第三个儿子是列奥纳多,因此列奥纳多被除名,他的儿子(丹尼斯和列奥纳多)成为杰西的儿子。)但是另一个莱昂纳多被撤职了,菲利普代替了他。)
孩子们的顺序无关紧要(也就是说,拉斐尔和大卫可能会追随菲利普)
假设当“Leonardo”在根中时,没有办法得到初始树。
我试过了,可惜不行。
for (int i = 0; i < root.children.size(); ++i) {
        removeLeonardo(root, root.children.get(i));
}

public void removeLeonardo(Node curr, Node child) {
    if (child == null || child.children.isEmpty() == true) {
        return;
    }

    if (child.mName.equals("Leonardo") == true) {
        // add all Leonardo's children to current father
        for (int i = 0; i < child.children.size(); ++i) {
            curr.children.add(child.children.get(i));
            // apply the func for the current father with each of Leonardo's children
            removeLeonardo(curr, child.children.get(i));
        }
        // remove "Leonardo" from the current father
        curr.children.remove(child);
    }

    else { // no "Leonardo" found, so apply the func for each child and this child's children
        for (int i = 0; i < child.children.size(); ++i) {
            removeLeonardo(child, child.children.get(i));
        }
    }
}

但是Java不能通过引用来工作你能帮我解决这个问题吗?

最佳答案

我相信这应该管用。
如果您担心效率,那么从arraylist中删除并插入它并不是最好的。LinkedList会更好,但这样你就不想通过孩子的索引来访问他们您可能会改用listiterator,并使用a d d和remove方法。

public static void main(String[] args)
{
    Node root = new Node("Jessy");
    root.children.addAll(Arrays.asList(new Node[]{new Node("Leonardo"), new Node("John"), new Node("Leonardo")}));
    root.children.get(0).children.addAll(Arrays.asList(new Node[]{new Node("Rafael"), new Node("David")}));
    root.children.get(1).children.addAll(Arrays.asList(new Node[]{new Node("Leonardo"), new Node("Keren")}));
    root.children.get(2).children.addAll(Arrays.asList(new Node[]{new Node("Denis"), new Node("Leonardo")}));
    root.children.get(2).children.get(1).children.add(new Node("Phillipe"));

    print(root, "");
    pruneByName(root, "Leonardo");
    print(root, "");
}

static void pruneByName(Node node, String name)
{
    assert !node.mName.equals(name);

    for(int i=0; i<node.children.size(); )
    {
        Node child = node.children.get(i);
        if(child.mName.equals(name))
        {
            node.children.remove(i);
            node.children.addAll(i, child.children);
        }
        else
        {
            pruneByName(child, name);
            i += 1;
        }
    }
}

static void print(Node node, String ind)
{
    System.out.println(ind + node.mName);
    for(Node child : node.children)
    {
        print(child, ind + "  ");
    }
}

输出:
Jessy
  Leonardo
    Rafael
    David
  John
    Leonardo
    Keren
  Leonardo
    Denis
    Leonardo
      Phillipe
Jessy
  Rafael
  David
  John
    Keren
  Denis
  Phillipe

10-05 18:20