从数组列表格式的树中移除子树。

huangapple 未分类评论45阅读模式
英文:

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&lt;Tree&gt;
{
    private Tree data = null; //create a tree
    private List&lt;GeneralTree&gt; children = new ArrayList&lt;&gt;(); //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&lt;Tree&gt; newChild = new GeneralTree&lt;&gt;(data);
        this.addChild(newChild);
    }
    public void addChildren(List&lt;GeneralTree&gt; children) //create a method to add children to a parent
    {
        for(GeneralTree treeAdd: children)
        {
            treeAdd.setParent(this);
        }
        this.children.addAll(children);
    }
    public List&lt;GeneralTree&gt; 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&lt;String&gt; root = new GeneralTree&lt;&gt;(&quot;Root&quot;); //create a root node

        GeneralTree&lt;String&gt; child1 = new GeneralTree&lt;&gt;(&quot;Child 1&quot;); //first child node
        child1.addChild(&quot;Grandchild 1&quot;); //first grandchild node
        child1.addChild(&quot;Grandchild 2&quot;); //second grandchild node
        GeneralTree&lt;String&gt; child2 = new GeneralTree&lt;&gt;(&quot;Child 2&quot;); //second child node
        child2.addChild(&quot;Grandchild 3&quot;); //third grandchild node
        root.addChild(child1);
        root.addChild(child2);
        root.addChild(&quot;Child 3&quot;); //third child node
        root.addChildren(Arrays.asList(new GeneralTree&lt;&gt;(&quot;Child 4&quot;), new GeneralTree&lt;&gt;(&quot;Child 5&quot;), new GeneralTree&lt;&gt;(&quot;Child 6&quot;)));//add fourth, fifth, and sixth children nodes

        System.out.println(root.isEmpty()? &quot;Empty&quot;: &quot;Not Empty&quot;);
        root.removeSubtree(child1);

        for(GeneralTree node: root.getChildren()) //get and print the children as long as they&#39;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

huangapple
  • 本文由 发表于 2020年7月24日 12:20:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/63066756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定