Java生成部門樹和設定每個節點高度
阿新 • • 發佈:2020-07-24
* 功能1: 部門列表生成樹
* 功能2: 設定樹的每個節點高度
程式碼 :
public class TreeDemo { //測試 public static void main(String[] args) { long t1 = System.currentTimeMillis(); //模擬初始資料集合 List<TreeNode> list = initData(); //計算樹結構 TreeNodeVo tree = getTree(list);//設定每個節點高度 getHeight(tree); System.out.println("List耗時:" + (System.currentTimeMillis() - t1)); System.out.println(JSONObject.toJSON(tree)); } /** * *****************求樹高,最底層預設是1*************** * * ------- 11-------111 * | * ------1----| * | |-------12 * | * 0---| * |-----2----|--------21 * * 例如 節點 111 的高度是1, 11高度是2 , 1 高度是 3 * * *****************求樹高,最底層預設是1****************/ private static Integer getHeight(TreeNodeVo tree) { List<TreeNodeVo> children = tree.getChildren(); if (CollectionUtils.isEmpty(children)) { return 1; } //最大的兒子高度 Integer maxHightSon = 1; for (TreeNodeVo vo : children) {int heightOfChildren = getHeight(vo); vo.setHeight(heightOfChildren); System.out.println("vo.getName :" + vo.getName()); System.out.println("vo.getHeight :" + vo.getHeight()); maxHightSon = Math.max(heightOfChildren, maxHightSon); } tree.setHeight(maxHightSon + 1); System.out.println("tree.getName :" + tree.getName()); System.out.println("tree.getHeight :" + tree.getHeight()); return maxHightSon + 1; } private static TreeNodeVo getTree(List<TreeNode> list) { int count = 0; // 2. 根節點 TreeNodeVo root = new TreeNodeVo(); root.setId(-1); root.setName("根節點"); // 3. 子節點 待下掛佇列 LinkedList<TreeNodeVo> tempQueue = new LinkedList<>(); tempQueue.add(root); while (!tempQueue.isEmpty() && list.size() > 0) { TreeNodeVo currentNodeVo = tempQueue.poll(); for (int i = list.size() - 1; i >= 0; i--) { count++; TreeNode item = list.get(i); if (item.getParentId() != null && currentNodeVo.getId().equals(item.getParentId())) { TreeNodeVo child = convert(item); currentNodeVo.getChildren().add(child); tempQueue.add(child); list.remove(item); } } } System.out.println("迴圈次數:" + count); return root; } //copy private static TreeNodeVo convert(TreeNode node) { TreeNodeVo nodeVo = new TreeNodeVo(); nodeVo.setId(node.getId()); nodeVo.setName(node.getName()); nodeVo.setParentId(node.getParentId()); return nodeVo; } //初始化資料 private static List<TreeNode> initData() { TreeNode node1 = new TreeNode(); node1.setId(1); node1.setName("水果"); node1.setParentId(-1); TreeNode node2 = new TreeNode(); node2.setId(2); node2.setName("蔬菜"); node2.setParentId(-1); TreeNode node3 = new TreeNode(); node3.setId(3); node3.setName("蘋果"); node3.setParentId(1); TreeNode node4 = new TreeNode(); node4.setId(4); node4.setName("梨子"); node4.setParentId(1); TreeNode node5 = new TreeNode(); node5.setId(5); node5.setName("生菜"); node5.setParentId(2); TreeNode node18 = new TreeNode(); node18.setId(18); node18.setName("生包菜"); node18.setParentId(5); TreeNode node6 = new TreeNode(); node6.setId(6); node6.setName("紅蘋果"); node6.setParentId(3); TreeNode node7 = new TreeNode(); node7.setId(7); node7.setName("青梨子"); node7.setParentId(4); TreeNode node9 = new TreeNode(); node9.setId(9); node9.setName("熟菜"); node9.setParentId(2); TreeNode node19 = new TreeNode(); node19.setId(19); node19.setName("熟包菜"); node19.setParentId(9); TreeNode node13 = new TreeNode(); node13.setId(13); node13.setName("肉類"); node13.setParentId(-1); TreeNode node14 = new TreeNode(); node14.setId(14); node14.setName("牛肉"); node14.setParentId(13); TreeNode node15 = new TreeNode(); node15.setId(15); node15.setName("雞肉"); node15.setParentId(13); TreeNode node16 = new TreeNode(); node16.setId(16); node16.setName("牛腿肉"); node16.setParentId(14); TreeNode node17 = new TreeNode(); node17.setId(17); node17.setName("雞翅"); node17.setParentId(15); List<TreeNode> list = new LinkedList<>(); list.add(node1); list.add(node2); list.add(node3); list.add(node4); list.add(node5); list.add(node6); list.add(node7); list.add(node9); list.add(node13); list.add(node14); list.add(node15); // list.add(node16); list.add(node17); list.add(node18); list.add(node19); //打亂順序 Collections.shuffle(list); return list; } @Data private static class TreeNode { private Integer id; private String name; private Integer parentId; } @Data private static class TreeNodeVo extends TreeNode { private Integer height; private List<TreeNodeVo> children = new ArrayList<>(); } }