如何检查 Java 中的 ArrayList 树是否为空?

huangapple go评论80阅读模式
英文:

How to check if arraylist tree in Java is empty or not?

问题

{
    private T data = null; // 创建一个树
    private List<GeneralTree<T>> children = new ArrayList<>(); // 创建一个ArrayList
    private GeneralTree<T> parent = null; // 创建一个父节点

    public GeneralTree(T data) // 构造方法
    {
        this.data = data;
    }

    public void addChild(GeneralTree<T> child) // 创建一个子节点
    {
        child.setParent(this);
        this.children.add(child);
    }

    public void addChild(T data) // 将数据放入子节点
    {
        GeneralTree<T> newChild = new GeneralTree<>(data);
        this.addChild(newChild);
    }

    public void addChildren(List<GeneralTree<T>> children) // 将子节点添加到父节点
    {
        for(GeneralTree<T> t: children)
        {
            t.setParent(this);
        }
        this.children.addAll(children);
    }

    public List<GeneralTree<T>> getChildren() // 获取子节点
    {
        return this.children;
    }

    public T getData() // 获取子节点中的数据
    {
        return data;
    }

    public void setData(T data) // 设置子节点的数据
    {
        this.data = data;
    }

    public void setParent(GeneralTree<T> parent) // 设置父节点
    {
        this.parent = parent;
    }

    public GeneralTree<T> getParent() // 获取父节点
    {
        return this.parent;
    }

    public boolean isEmpty() // 主要有问题的isEmpty()方法
    {
        if(this.parent == null) // 检查值是否为空。如果为真,树为空。
        {
            System.out.println("树为空。");
            return false;
        }
        else
        {
            return true;
        }
    }
}

// 主方法驱动类
public static void main(String[] args)
{
    GeneralTree<String> root = new GeneralTree<>("Root"); // 创建一个根节点

    if(root.isEmpty())
    {
        if(true)
        {
            System.out.println("树为空。");
        }
    }
    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"))); // 添加第四、第五和第六个子节点
    for(GeneralTree<String> node: root.getChildren()) // 获取并打印子节点,只要它们位于根节点之下
    {
        System.out.println(node.getData()); // 获取数据
    }
}
英文:

I'm creating a tree in Java. And when I create a method to check if the tree is empty, it doesn't work correctly. When I debug the program, and get to the if statement that checks if root is empty or not, I keep getting "parent = null". I think the problem may be due to the parent set method, but I'm not sure. Here's my code:

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

Main isEmpty() method that I'm having trouble with

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

Main Method to drive the class

    public static void main(String[] args)
    {
        GeneralTree&lt;String&gt; root = new GeneralTree&lt;&gt;(&quot;Root&quot;); //create a root node

        if(root.isEmpty())
        {
            if(true)
            {
                System.out.println(&quot;The tree is empty.&quot;);
            }
        }
        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
        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 don't know if the issue is with the parent node or the design of the isEmpty() method?

答案1

得分: 3

你的isEmpty()方法在我看来似乎没有意义。

它在询问父节点是否为空。这与是否为空有什么关系呢?

“父节点是否为空?”意味着“我是否是根节点?”。

或者换一种说法 - 根节点从不具有父节点,因此根据你的测试,root.isEmpty()永远为真。

按照我的想法,“空树”意味着“没有子节点”(如果可能的话还意味着“这里没有数据”)。

英文:

Your isEmpty() method does not seem to make sense to me.

It asks whether the parent is null. What has that got to do with being empty?

"Is the parent null?" means "am I the root?".

Or to say it differently - the root never has a parent, so per your test, root.isEmpty() is permanently true.

To my way of thinking, "empty tree" means "no children" (and "no data here" if that's a possibility).

答案2

得分: 2

如果您没有任何childrenArrayList 将永远不会被填充,那么为什么不将其更改为:

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.");

如果您只想要输出结果(如评论中所见),您还可以使用一些像下面这样的精巧的 Java 代码:

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

If you don't have any children the ArrayList is never fulfilled, so why not just change it to:

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

Also, you should remove the (true) condition, from this:

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

to:

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

If you wish to just output the result (as seen on the comments) you could also do some fancy java sh** like:

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

huangapple
  • 本文由 发表于 2020年7月24日 07:05:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63064371.html
匿名

发表评论

匿名网友

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

确定