准备写一系列Java数据结构的博客,先开个头,然后不断补充内容,首先来说下树
- 树确定父节点与子节点的关系
//确定父节点与子节点的关系 for (int i = 0; i < nodes.size(); i++) { Node n = nodes.get(i); for (int j = i + 1; j < nodes.size(); j++) { //点睛之笔在这个j=i+1,这样避免重复“认亲” Node m = nodes.get(j); if (m.getId() == n.getpId()) { m.getChildren().add(n); n.setParent(m); } else if (m.getpId() == n.getId()) { n.getChildren().add(m); m.setParent(n); } } }
- 遍历树让所有的根节点按顺序排列
public static <T> List<Node> getSortedNodes(List<T> datas, int defaultLevel) throws IllegalAccessException { List<Node> result = new ArrayList<Node>(); List<Node> nodes = convertDatas2Nodes(datas); //将XxxBean数据转换成节点数据 //获取树的根节点 List<Node> rootNodes = getRootNodes(nodes); for (Node node : rootNodes) { addNodes(result, node, defaultLevel, 1); //先序递归遍历 } return result; } private static void addNodes(List<Node> result, Node node, int defaultLevel, int currentLevel) { result.add(node); if (defaultLevel >= currentLevel) { //在遍历用设置节点的展开层级 node.setIsExpand(true); } if (node.isLeaf()) { return; } else { for (int i = 0; i < node.getChildren().size(); i++) { addNodes(result, node.getChildren().get(i), defaultLevel, currentLevel + 1); } } }
- 获取根节点
public static List<Node> getRootNodes(List<Node> nodes) { List<Node> result = new ArrayList<Node>(); for (Node n : nodes) { if (n.isRoot()) { //直接通过节点对象的标签来实现查找根节点 result.add(n); } } return result; }
- 节点对象
package com.example.utils; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2015/8/19. */ public class Node { private int id; public Node() { } public Node(int id, int pId, String name) { this.id = id; this.pId = pId; this.name = name; } /** * 根节点的pId为0 */ private int pId = 0; private String name; private int level; private boolean isExpand = false; private int icon; private Node parent; private List<Node> children = new ArrayList<Node>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public int getpId() { return pId; } public void setpId(int pId) { this.pId = pId; } public String getName() { return name; } public void setName(String name) { this.name = name; } /** * 设置当前节点的层级 * * @return */ public int getLevel() { return parent == null ? 0 : parent.getLevel() + 1; } public boolean isExpand() { return isExpand; } /** * 当父节点被设置为不展开时,将其所有的子节点也设置为不展开 * * @param isExpand */ public void setIsExpand(boolean isExpand) { this.isExpand = isExpand; if (!isExpand) { for (Node node : children) { node.setIsExpand(false); } } } public int getIcon() { return icon; } public void setIcon(int icon) { this.icon = icon; } public Node getParent() { return parent; } public void setParent(Node parent) { this.parent = parent; } public List<Node> getChildren() { return children; } public void setChildren(List<Node> children) { this.children = children; } /** * 是否是根节点 * * @return */ public boolean isRoot() { return parent == null; } /** * 判断当前父节点是否展开 */ public boolean isParentExpand() { if (parent == null) { return false; } return parent.isExpand(); } /** * 是否是叶子节点 * * @return */ public boolean isLeaf() { return children.size() == 0; } }
本文深入探讨了使用Java实现树数据结构的过程,包括如何确定父节点与子节点之间的关系,以及如何遍历树以获取所有根节点并按顺序排列。文章详细解释了节点对象的属性与方法,提供了实用的代码示例。
5442

被折叠的 条评论
为什么被折叠?



