英文:
remove Subtree from tree in arrayList format
问题
以下是翻译好的内容:
public class GeneralTree<Tree>
{
    private Tree data = null; // 创建一个树
    private List<GeneralTree> children = new ArrayList<>(); // 创建一个ArrayList
    private GeneralTree parent = null; // 创建一个父节点
    public GeneralTree(Tree data) // 构造函数
    {
        this.data = data;
    }
    public void addChild(GeneralTree child) // 创建一个子节点方法
    {
        child.setParent(this);
        this.children.add(child);
    }
    public void addChild(Tree data) // 创建一个将数据放入子节点的方法
    {
        GeneralTree<Tree> newChild = new GeneralTree<>(data);
        this.addChild(newChild);
    }
    public void addChildren(List<GeneralTree> children) // 创建一个将子节点添加到父节点的方法
    {
        for(GeneralTree treeAdd: children)
        {
            treeAdd.setParent(this);
        }
        this.children.addAll(children);
    }
    public List<GeneralTree> getChildren() // 获取子节点
    {
        return this.children;
    }
    public Tree getData() // 获取子节点中的数据
    {
        return data;
    }
    public void setData(Tree data) // 设置子节点的数据
    {
        this.data = data;
    }
    public void setParent(GeneralTree parent) // 设置父节点
    {
        this.parent = parent;
    }
    public GeneralTree getParent() // 获取父节点
    {
        return this.parent;
    }
    public boolean isEmpty()
    {
        return this.children.isEmpty();
    }
    public List removeSubtree(GeneralTree child)
    {
        GeneralTree removed = removeTree(child);
        return removed.children;
    }
    // ... 省略其他方法
}
    public List removeSubtree(GeneralTree child)
    {
        GeneralTree removed = removeTree(child);
        return removed.children;
    }
    public GeneralTree removeTree(GeneralTree remover)
    {
        return null;
    }
    public static void main(String[] args)
    {
        GeneralTree<String> root = new GeneralTree<>("Root"); // 创建一个根节点
        GeneralTree<String> child1 = new GeneralTree<>("Child 1"); // 第一个子节点
        child1.addChild("Grandchild 1"); // 第一个孙子节点
        child1.addChild("Grandchild 2"); // 第二个孙子节点
        GeneralTree<String> child2 = new GeneralTree<>("Child 2"); // 第二个子节点
        child2.addChild("Grandchild 3"); // 第三个孙子节点
        root.addChild(child1);
        root.addChild(child2);
        root.addChild("Child 3"); // 第三个子节点
        root.addChildren(Arrays.asList(new GeneralTree<>("Child 4"), new GeneralTree<>("Child 5"), new GeneralTree<>("Child 6"))); // 添加第四、五、六个子节点
        System.out.println(root.isEmpty()? "Empty": "Not Empty");
        root.removeSubtree(child1);
        for(GeneralTree node: root.getChildren()) // 获取并打印根节点下的子节点
        {
            System.out.println(node.getData()); // 获取数据
        }
    }
}
关于您的问题,我目前看不出您代码中出现的明显错误。然而,您在 removeTree 方法中返回了 null,这可能会导致一些问题。如果您在其中执行了删除操作,您应该返回删除后的树,而不是 null。另外,请确保在 removeTree 方法中正确处理您想要实现的子树删除逻辑,以避免出现空指针异常。
英文:
I'm studying trees in Java using an ArrayList, and I'm trying to implement a method to remove an entire subtree from a child node on my tree, but I keep getting a NullPointerException error.
Here's my code:
public class GeneralTree<Tree>
{
    private Tree data = null; //create a tree
    private List<GeneralTree> children = new ArrayList<>(); //create an arraylist
    private GeneralTree parent = null; //create a parent
    public GeneralTree(Tree data) //constructor
    {
        this.data = data;
    }
    public void addChild(GeneralTree child) //create a child method to create a child
    {
        child.setParent(this);
        this.children.add(child);
    }
    public void addChild(Tree data) //create a method to put data into the children
    {
        GeneralTree<Tree> newChild = new GeneralTree<>(data);
        this.addChild(newChild);
    }
    public void addChildren(List<GeneralTree> children) //create a method to add children to a parent
    {
        for(GeneralTree treeAdd: children)
        {
            treeAdd.setParent(this);
        }
        this.children.addAll(children);
    }
    public List<GeneralTree> getChildren() //get the children
    {
        return this.children;
    }
    public Tree getData() //get the data in the children
    {
        return data;
    }
    public void setData(Tree data) //set the data of the children together
    {
        this.data = data;
    }
    public void setParent(GeneralTree parent) //set the parent together
    {
        this.parent = parent;
    }
    public GeneralTree getParent() //get the parent
    {
        return this.parent;
    }
    public boolean isEmpty()
    {
        return this.children.isEmpty();
    }
    public List removeSubtree(GeneralTree childs)
    {
        GeneralTree removes = removeTree(this.parent);
        return removes.children;
    }
removeSubTree() and removeTree() methods to implement this
    public List removeSubtree(GeneralTree childs)
    {
        GeneralTree removes = removeTree(this.parent);
        return removes.children;
    }
    public GeneralTree removeTree(GeneralTree remover)
    {
        return null;
    }
main method
    public static void main(String[] args)
    {
        GeneralTree<String> root = new GeneralTree<>("Root"); //create a root node
        GeneralTree<String> child1 = new GeneralTree<>("Child 1"); //first child node
        child1.addChild("Grandchild 1"); //first grandchild node
        child1.addChild("Grandchild 2"); //second grandchild node
        GeneralTree<String> child2 = new GeneralTree<>("Child 2"); //second child node
        child2.addChild("Grandchild 3"); //third grandchild node
        root.addChild(child1);
        root.addChild(child2);
        root.addChild("Child 3"); //third child node
        root.addChildren(Arrays.asList(new GeneralTree<>("Child 4"), new GeneralTree<>("Child 5"), new GeneralTree<>("Child 6")));//add fourth, fifth, and sixth children nodes
        System.out.println(root.isEmpty()? "Empty": "Not Empty");
        root.removeSubtree(child1);
        for(GeneralTree node: root.getChildren()) //get and print the children as long as they're under the root
        {
            System.out.println(node.getData()); //get the data
        }
    }
}
I'm not fully sure where I've gone wrong, but I think the problem is that I'm using a remove Object method to try & remove a series of elements from my array list. Is that right, or am I going about this process the wrong way?
答案1
得分: 0
removeTree 方法始终返回 null。
removeSubtree 方法返回 removes.children,但 removes 对象始终为 null。
你应该实现 removeTree 方法。
英文:
The removeTree method returns always null.
The removeSubtree method returns removes.children but the removes object is always null.
You should implement the removeTree method
专注分享java语言的经验与见解,让所有开发者获益!



评论