Stack Overflow Asked on December 20, 2021
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<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;
}
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("The tree is empty.");
return false;
}
else
{
return true;
}
}
Main Method to drive the class
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
}
}
}
I don’t know if the issue is with the parent node or the design of the isEmpty() method?
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("The tree is empty.");
}
}
to:
if(root.isEmpty())
System.out.println("The tree is empty.");
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()?"Empty":"Not empty");
Answered by aran on December 20, 2021
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).
Answered by user13784117 on December 20, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP