/*categoryDTOS是树的全部数据
* superId是根Id
*/
private List formatTreeCatagorys(List categoryDTOS, Long superId) {
List list =new ArrayList<>();
for(CategoryDTO categoryDTO : categoryDTOS) {
if (superId.equals(categoryDTO.getSuperId())) {
List subArray =this.formatTreeCatagorys(categoryDTOS, categoryDTO.getId());
CategoryTreeVO categoryTreeVO =new CategoryTreeVO(subArray, categoryDTO);
list.add(categoryTreeVO);
}
}
returnlist;
}
/*
* 对应VO类
*/
public CategoryTreeVO(List children, CategoryDTO category) {
this.setCategoryKey(category.getCategoryKey());
this.setCategoryName(category.getCategoryName());
this.setId(category.getId());
this.setSuperId(category.getSuperId());
this.children = children;
if(this.children.size() ==0) {
this.isLeaf =true;
} else{
this.isLeaf =false;
}
}
//解释
root
l1 l2
l11 l12 l21 l22
深度优先思路:从根开始。回调所有的数据。找出第一个节点L1,然后再以L1作为根。重新回调。找出L1下的L11,再以L11作为根节点回调遍历,直到L11下无叶子节点。
然后换成L22为根遍历,直到L22也无叶子,则L1回调结束,L11和L22放到L1的child中.然后换成L2