简体   繁体   English

从 arrayList 格式的树中删除子树

[英]remove Subtree from tree in arrayList format

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.我正在使用 ArrayList 研究 Java 中的树,并且我正在尝试实现一种从树上的子节点中删除整个子树的方法,但我不断收到 NullPointerException 错误。 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 removeSubTree() 和 removeTree() 方法来实现这个

    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.我不完全确定我哪里出错了,但我认为问题在于我正在使用删除 Object 方法尝试从我的数组列表中删除一系列元素。 Is that right, or am I going about this process the wrong way?这是对的,还是我以错误的方式进行这个过程?

The removeTree method returns always null. removeTree方法总是返回 null。 The removeSubtree method returns removes.children but the removes object is always null. removeSubtree方法返回removes.children ,但removes的 object 始终是 null。

You should implement the removeTree method你应该实现 removeTree 方法

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

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