From a9dcbaacec557b9c6d8b0d83aa4ad46490342b70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=A2=A6=E6=8A=80=E6=9C=AF?= <596392129@qq.com> Date: Thu, 27 Dec 2018 11:29:51 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E4=BC=98=E5=8C=96=20tree=20?= =?UTF-8?q?=E6=B3=9B=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/tool/node/ForestNodeManager.java | 67 ++++++++++--------- .../core/tool/node/ForestNodeMerger.java | 32 ++++----- 2 files changed, 52 insertions(+), 47 deletions(-) diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeManager.java b/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeManager.java index b15e62d..ece5cb5 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeManager.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeManager.java @@ -23,40 +23,45 @@ import java.util.List; * * @author zhuangqian */ -public class ForestNodeManager { +public class ForestNodeManager { - private List list;// 森林的所有节点 + /** + * 森林的所有节点 + */ + private List list; - public ForestNodeManager(List items) { - list = items; - } + public ForestNodeManager(List items) { + list = items; + } - /** - * 根据节点ID获取一个节点 - * - * @param id 节点ID - * @return 对应的节点对象 - */ - public INode getTreeNodeAT(int id) { - for (INode forestNode : list) { - if (forestNode.getId() == id) - return forestNode; - } - return null; - } + /** + * 根据节点ID获取一个节点 + * + * @param id 节点ID + * @return 对应的节点对象 + */ + public INode getTreeNodeAT(int id) { + for (INode forestNode : list) { + if (forestNode.getId() == id) { + return forestNode; + } + } + return null; + } - /** - * 获取树的根节点(一个森林对应多颗树) - * - * @return 树的根节点集合 - */ - public List getRoot() { - List roots = new ArrayList<>(); - for (INode forestNode : list) { - if (forestNode.getParentId() == 0) - roots.add(forestNode); - } - return roots; - } + /** + * 获取树的根节点(一个森林对应多颗树) + * + * @return 树的根节点集合 + */ + public List getRoot() { + List roots = new ArrayList<>(); + for (T forestNode : list) { + if (forestNode.getParentId() == 0) { + roots.add(forestNode); + } + } + return roots; + } } diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeMerger.java b/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeMerger.java index eda6496..61b71c8 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeMerger.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/node/ForestNodeMerger.java @@ -24,22 +24,22 @@ import java.util.List; */ public class ForestNodeMerger { - /** - * 将节点数组归并为一个森林(多棵树)(填充节点的children域) - * 时间复杂度为O(n^2) - * - * @param items 节点域 - * @return 多棵树的根节点集合 - */ - public static List merge(List items) { - ForestNodeManager forestNodeManager = new ForestNodeManager(items); - for (INode forestNode : items) { - if (forestNode.getParentId() != 0) { - INode node = forestNodeManager.getTreeNodeAT(forestNode.getParentId()); + /** + * 将节点数组归并为一个森林(多棵树)(填充节点的children域) + * 时间复杂度为O(n^2) + * + * @param items 节点域 + * @return 多棵树的根节点集合 + */ + public static List merge(List items) { + ForestNodeManager forestNodeManager = new ForestNodeManager<>(items); + for (T forestNode : items) { + if (forestNode.getParentId() != 0) { + INode node = forestNodeManager.getTreeNodeAT(forestNode.getParentId()); node.getChildren().add(forestNode); - } - } - return forestNodeManager.getRoot(); - } + } + } + return forestNodeManager.getRoot(); + } }