【问题标题】:How to check if arraylist tree in Java is empty or not?如何检查Java中的arraylist树是否为空?
【发布时间】:2020-07-23 23:05:38
【问题描述】:

我正在用 Java 创建一棵树。当我创建一个方法来检查树是否为空时,它不能正常工作。当我调试程序并进入检查 root 是否为空的 if 语句时,我不断收到“parent = null”。我认为问题可能是由于父设置方法,但我不确定。这是我的代码:

{
    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 t: children)
        {
            t.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;
    }

我遇到问题的主要 isEmpty() 方法

    public boolean isEmpty()
    {
        if(this.parent == null) //check if value is null. if it is true, the tree is full.
        {
            System.out.println("The tree is empty.");
            return false;
        }
        else
        {
            return true;
        }
    }

驱动类的Main方法

    public static void main(String[] args)
    {
        GeneralTree<String> root = new GeneralTree<>("Root"); //create a root node

        if(root.isEmpty())
        {
            if(true)
            {
                System.out.println("The tree is empty.");
            }
        }
        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
        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
        }
    }
}

不知道是父节点的问题还是isEmpty()方法的设计问题?

【问题讨论】:

    标签: java arraylist tree


    【解决方案1】:

    您的 isEmpty() 方法对我来说似乎没有意义。

    它询问父级是否为空。这和空虚有什么关系?

    “父级是否为空?”意思是“我是根吗?”。

    或者换一种说法 - 根永远没有父母,所以根据您的测试,root.isEmpty() 永远为真。

    在我看来,“空树”意味着“没有孩子”(如果可能的话,“这里没有数据”)。

    【讨论】:

    • 所以,我应该检查父 GeneralTree 对象是否有任何子对象?在这种情况下,我还应该创建一个 hasChildren() 方法来检查这个?
    • 没有。 x.isEmpty() 表示“x 是否有孩子或数据”。育儿是不涉及的一件事。
    【解决方案2】:

    如果您没有任何children ArrayList 永远不会满足,那么为什么不将其更改为:

    public boolean isEmpty()
    {
       return children.isEmpty();
    }
    

    此外,您应该从中删除 (true) 条件:

    if(root.isEmpty())
    {
        if(true)
        {
           System.out.println("The tree is empty.");
        }
     }
    

    到:

    if(root.isEmpty())
        System.out.println("The tree is empty.");
    

    如果您希望仅输出结果(如 cmets 上所见),您还可以执行 一些花哨的 java sh** 之类的:

    System.out.println(root.isEmpty()?"Empty":"Not empty");
    

    【讨论】:

    • 我就是这样做的,并且在 main 方法中,我添加了一个 if-else 语句来检查这一点。然而,这对在场的孩子们不起作用。当我尝试使用未注释掉的孩子时,我仍然“空”。我做错了什么?
    • ```if(root.isEmpty()) { System.out.println("empty"); } else { System.out.println("非空"); }
    • 我总是看到“?”在Java中标记,但我不知道那是什么。你能解释一下吗?
    • 在您的代码中,您在创建根之后(并且在添加任何子项之前)调用该方法您可以将 isEmpty() 条件放在代码末尾,或者至少在添加之后一个孩子?
    • 很高兴它帮助了交配:)
    猜你喜欢
    • 2012-12-18
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 2015-05-02
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多