为了实现一个方法来整理没有叶子节点的树形结构,我们可以通过递归遍历树的每个节点,检查每个分支是否存在叶子节点。如果某个分支没有叶子节点,则将其剔除。下面是一个示例方法的实现,命名为 整理无叶子节点的树形结构
。
代码实现
java
import java.util.ArrayList;
import java.util.List;
class DicEquClassify {
private String id; // 节点ID
private String parentId; // 父节点ID
private String nodeType; // 节点类型
private List<DicEquClassify> childrens; // 子节点列表
// 构造函数
public DicEquClassify(String id, String parentId, String nodeType) {
this.id = id;
this.parentId = parentId;
this.nodeType = nodeType;
this.childrens = new ArrayList<>();
}
// Getter 和 Setter
public String getId() {
return id;
}
public String getParentId() {
return parentId;
}
public String getNodeType() {
return nodeType;
}
public List<DicEquClassify> getChildrens() {
return childrens;
}
public void addChild(DicEquClassify child) {
childrens.add(child);
}
// 整理无叶子节点的树形结构
public static List<DicEquClassify> organizeTreeWithoutLeafNodes(List<DicEquClassify> tree) {
List<DicEquClassify> result = new ArrayList<>();
for (DicEquClassify node : tree) {
if (hasLeafNode(node)) {
result.add(node);
}
}
return result;
}
// 检查当前节点是否有叶子节点
private static boolean hasLeafNode(DicEquClassify node) {
if (node.getChildrens().isEmpty()) {
return "equ".equals(node.getNodeType()); // 如果没有子节点,检查是否为叶子节点
}
// 递归检查子节点
for (DicEquClassify child : node.getChildrens()) {
if (hasLeafNode(child)) {
return true; // 如果子节点中有叶子节点,返回true
}
}
return false; // 如果没有叶子节点,返回false
}
}
方法说明
-
organizeTreeWithoutLeafNodes
方法- 输入:树形结构的列表
tree
。 - 输出:整理后的树形结构列表,剔除没有叶子节点的分支。
- 逻辑:遍历树中的每个节点,调用
hasLeafNode
方法检查是否有叶子节点。如果有,则将该节点添加到结果列表中。
- 输入:树形结构的列表
-
hasLeafNode
方法- 输入:当前节点
node
。 - 输出:布尔值,表示该节点是否有叶子节点。
- 逻辑:
- 首先检查当前节点的子节点列表是否为空,如果为空,判断其类型是否为 "equ"。
- 如果不为空,递归检查每个子节点,直到找到叶子节点为止。
- 输入:当前节点
示例用法
public class Main {
public static void main(String[] args) {
// 创建示例树结构
DicEquClassify root = new DicEquClassify("1", null, "node");
DicEquClassify child1 = new DicEquClassify("2", "1", "node");
DicEquClassify child2 = new DicEquClassify("3", "1", "node");
DicEquClassify leaf1 = new DicEquClassify("4", "2", "equ");
DicEquClassify leaf2 = new DicEquClassify("5", "3", "node");
root.addChild(child1);
root.addChild(child2);
child1.addChild(leaf1);
child2.addChild(leaf2); // 假设child2有一个非叶子节点
List<DicEquClassify> treeList = new ArrayList<>();
treeList.add(root);
// 整理无叶子节点的树形结构
List<DicEquClassify> organizedTree = DicEquClassify.organizeTreeWithoutLeafNodes(treeList);
System.out.println("整理后的树形结构:");
for (DicEquClassify node : organizedTree) {
System.out.println("节点ID: " + node.getId());
}
}
}
业务实现
// 整理非标准树 传来的就是 合起来的 分级菜单和设备信息
List<DicEquClassify> treeList = this.getTree(equClassifies);
//清理树,若没有挂接设备(没有叶子节点),则直接剔除
List<DicEquClassify> newTreeList = this.organizeTreeWithoutLeafNodes(treeList);
resultObject.setResult(newTreeList);
return resultObject;
}
// 整理无叶子节点的树形结构
public static List<DicEquClassify> organizeTreeWithoutLeafNodes(List<DicEquClassify> tree) {
List<DicEquClassify> result = new ArrayList<>();
for (DicEquClassify node : tree) {
if (hasLeafNode(node)) {
result.add(node);
}
}
return result;
}
// 检查当前节点是否有叶子节点
private static boolean hasLeafNode(DicEquClassify node) {
if (node.getChildrens().isEmpty()) {
return "equ".equals(node.getNodeType()); // 如果没有子节点,检查是否为叶子节点
}
// 递归检查子节点
for (DicEquClassify child : node.getChildrens()) {
if (hasLeafNode(child)) {
return true; // 如果子节点中有叶子节点,返回true
}
}
return false; // 如果没有叶子节点,返回false
}
//自己写的树形结构 递归找儿
private List<DicEquClassify> getChildrenCategory(DicEquClassify root,List<DicEquClassify> all){
List<DicEquClassify> children = all.stream().filter(categoryEntity -> {
// 这里可能会出现NullPointerException 因为客户的数据导入没有sys_id,也就是没有系统分类id,看看怎么处理
return StringUtils.isNotBlank(categoryEntity.getParentId()) && categoryEntity.getParentId().equals(root.getId());
}).map(categoryEntity -> {
//找到了子分类并设置
categoryEntity.setChildrens(getChildrenCategory(categoryEntity,all));
return categoryEntity;
}).sorted((category1,category2) ->{
return ( category1.getSort() == null ? 0: category1.getSort() ) -
(category2.getSort() == null ? 0: category2.getSort() );
}).collect(Collectors.toList());
return children;
}
//传入树形结构list,返回树形
public List<DicEquClassify> getTree(List<DicEquClassify> categoryList) throws Exception {
List<DicEquClassify> categoryTree = categoryList.stream().filter(categoryEntity -> {
//过滤 返回一级分类
return categoryEntity.getParentId().equals("0");
}).map(category -> {
//进行map映射 ,给每个分类进行子分类(用递归)
List<DicEquClassify> childrenCategory = getChildrenCategory(category, categoryList);
category.setChildrens(childrenCategory);
return category;
}).sorted((category1, category2) -> {
return (category1.getSort() == null ? 0 : category1.getSort()) - (category2.getSort() == null ? 0 : category2.getSort());
}).collect(Collectors.toList());
return categoryTree;
}
/**
* 查询设备树
*
* @param simpleQuery
* @param resultObject
* @return
*/
private List<DicEquClassify> queryEquTree(SimpleQuery<DicEquClassify> simpleQuery, List<EqumEqu> equBaseInfos, ResultObject<List<DicEquClassify>> resultObject) {
if (Objects.isNull(simpleQuery)) {
resultObject.setError("查询条件为空");
return null;
}
String shipId = simpleQuery.getStringValue("shipId");
// 查询设备分类
List<DicEquClassify> equClassifies = dicEquClassifyMapper.selectList(new LambdaQueryWrapper<DicEquClassify>().orderByAsc(DicEquClassify::getSort));
if (org.springframework.util.CollectionUtils.isEmpty(equClassifies)) {
return equClassifies;
}
//循环将nodeType设置成classify
equClassifies.forEach(one -> one.setNodeType("classify"));
// 查询设备基本信息
if (!StringUtils.isEmpty(shipId)) {
if (!org.springframework.util.CollectionUtils.isEmpty(equBaseInfos)) {
// 将设备转换为设备分类对象用于组装树形结构
for (EqumEqu one : equBaseInfos) {
DicEquClassify equClassify = new DicEquClassify();
equClassify.setId(one.getId());
equClassify.setParentId(one.getSysId());
equClassify.setName(one.getPurpose());
equClassify.setNodeType("equ");
equClassify.setEquModel(one.getModel());
equClassifies.add(equClassify);
}
equBaseInfos.clear();
}
}
return equClassifies;
}