直接解释您的条件A. remove if subProjects and documents are empty
或B. all children have no documents
(假设“他的子项目都没有文件”是指所有孩子,而不仅仅是直接孩子)
定义一个布尔函数通常有助于检查节点的状态,然后可以查询它以检查是否应该删除节点
该代码假定您将其放入Project
,如果您不是,则需要将其作为参数传递
boolean isEmpty()
{
return subProjects.isEmpty() && documents.isEmpty();
}
boolean isChildrenEmpty()
{
//put before the recursion for speed
if(!documents.isEmpty()) //check if self has documents
return false;
for(Project child : subProjects)
if(!child.isChildrenEmpty()) //check if any child has documents
return false;
return true; //all children + self have no documents
}
boolean toRemove()
{
if(isEmpty()) //no children, no documents
return true;
for(Project child : subProjects)
if(!child.isChildrenEmpty()) //a child has a document
return false;
return true;
}