1、广度优先搜索(Breadth-First Search,简称 BFS)算法是一种用于图或树的遍历算法。它从一个节点开始,逐层遍历节点,即先访问起始节点的所有邻接节点,再访问邻接节点的邻接节点,依此类推。BFS 通常使用队列数据结构来实现。
2、表结构
CREATE TABLE `outline` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`parentId` bigint(20) DEFAULT '-1',
`status` enum('normal','frozen') DEFAULT 'normal',
`level` enum('Catalog','Chapter','Section','Item','TestPoint','SubTestPoint') DEFAULT NULL COMMENT '层级:篇、章、节、目、知识点、子知识点',
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=300014242 DEFAULT CHARSET=utf8 COMMENT='考点';
3、构造树结构方法
/**
* 构造树形结构
*
* @param treeNodes
* @return
*/
static <T, N extends TreeNode<T, N>> List<N> makeTree(List<N> treeNodes, T rootNodeId) {
if (treeNodes == null || treeNodes.isEmpty()) {
return treeNodes;
}
//父级map k:v = parentId:OutlineVo
Map<T, List<N>> parentIdMapping = treeNodes.stream()
.filter(item -> item.getParentId() != null)
.collect(Collectors.groupingBy(node -> node.getParentId()));
//包含根的子节点列表 = 根节点列表
List<N> rootNodes = parentIdMapping.get(rootNodeId);
//队列--ArrayDeque 的方式是 BFS 算法的标准实现之一,
//因为它允许您高效地从队列的前端移除元素,并且能够快速地在队列的尾端添加元素
Queue<N> processQueue = new ArrayDeque<N>();
if (CollectionUtils.isEmpty(rootNodes)) {
return treeNodes;
}
processQueue.addAll(rootNodes);
while (!processQueue.isEmpty()) {
//遍历队列
N treeNode = processQueue.poll();
//判断当前节点是否有孩子
List<N> children = parentIdMapping.get(treeNode.getId());
if (children != null) {
treeNode.setChildren(children);
for (N child : children) {
processQueue.offer(child);
}
}
}
//返回影响后的
return rootNodes;
}