标签:
这里,我用文件存储了一些数据,如果在真实开发环境下,可以用数据库存储。
他们的字段分别是:selfId(自己本身ID),praentId(父类ID),childId(子类ID),depth(深度,默认为0),expand(扩展,请看代码).如果是数据库存储的话,我们可以记录评论时间,评论人,评论人头像等等信息...
1,0,3,0,true 2,0,-1,0,true 3,1,4,0,true 4,3,-1,0,true 5,3,-1,0,true
1.实例,这里我取名为TreeNode,封装了单个评论的实例。
public class TreeNode implements Comparable<TreeNode> { private int selfId; private int parentId; private int childId; private int depth = 0; // 默认深度为0 private boolean expand = true; public TreeNode() { super(); } public TreeNode(int selfId, int parentId, int childId, int depth, boolean expand) { this.selfId = selfId; this.parentId = parentId; this.childId = childId; this.depth = depth; if (expand) { ArrayList<TreeNode> nodeS = null; nodeS = NodeTreeUtil.getTreeNodesById(selfId); // 根据ID获取节点 for (TreeNode treeNode : nodeS) { if (this.childId == -1) { NodeTreeUtil.setTreeNodes(new TreeNode(treeNode.getSelfId(), treeNode.getParentId(), treeNode.getChildId(), depth + 1, false)); } else { NodeTreeUtil.setTreeNodes(new TreeNode(treeNode.getSelfId(), treeNode.getParentId(), treeNode.getChildId(), depth + 1, true)); } } } } public int getSelfId() { return selfId; } public void setSelfId(int selfId) { this.selfId = selfId; } public int getParentId() { return parentId; } public void setParentId(int parentId) { this.parentId = parentId; } public int getChildId() { return childId; } public void setChildId(int childId) { this.childId = childId; } public int getDepth() { return depth; } public void setDepth(int depth) { this.depth = depth; } public boolean isExpand() { return expand; } public void setExpand(boolean expand) { this.expand = expand; } @Override public int compareTo(TreeNode o) { if (this.depth > o.getDepth()) { return (this.depth - o.getDepth()); } if (this.depth < o.getDepth()) { return (this.depth - o.getDepth()); } return 0; } }
2. NodeTreeUtil类封装了一些简单的方法操作:
public class NodeTreeUtil { public static ArrayList<TreeNode> treeNodes = new ArrayList<TreeNode>(); public static ArrayList<TreeNode> getTreeNodes() { if(treeNodes == null){ return new ArrayList<TreeNode>(); } return treeNodes; } public static void setTreeNodes(TreeNode treeNode) { NodeTreeUtil.getTreeNodes().add(treeNode); } /** * 读取文件内容,封装nodeList * @param fileName * @author:Tian_dd * @blog: tian-dd.top */ public static ArrayList<TreeNode> readFileByLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String tempString = null; ArrayList<TreeNode> treeNodes = new ArrayList<TreeNode>(); while ((tempString = reader.readLine()) != null) { String[] strings = tempString.split(","); TreeNode treeNode = new TreeNode(); treeNode.setSelfId(Integer.parseInt(strings[0])); treeNode.setParentId(Integer.parseInt(strings[1])); treeNode.setChildId(Integer.parseInt(strings[2])); treeNode.setDepth(Integer.parseInt(strings[3])); treeNode.setExpand(Boolean.parseBoolean(strings[4])); treeNodes.add(treeNode); } reader.close(); return treeNodes; } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return null; } public static ArrayList<TreeNode> getNodeList() { ArrayList<TreeNode> treeNodes = readFileByLines( "/Users/tianduoduo/Documents/workspace/TreeNode/src/com/tian/resource/source.txt"); return treeNodes; } public static ArrayList<TreeNode> initNodeTree(){ return new ArrayList<TreeNode>(); } /** * 获取相同父节点的节点 * @param selfId * @author:Tian_dd * @blog: tian-dd.top */ public static ArrayList<TreeNode> getTreeNodesById(int selfId) { ArrayList<TreeNode> treeNodes = new ArrayList<TreeNode>(); ArrayList<TreeNode> totalNodeS = NodeTreeUtil.getNodeList(); for (TreeNode treeNode : totalNodeS) { if (treeNode.getParentId() == selfId) { treeNodes.add(treeNode); } } return treeNodes; } /** * 打印结构 * @param treeNodes * @author:Tian_dd * @blog: tian-dd.top */ public static void printListFormat(ArrayList<TreeNode> treeNodes){ Collections.sort(treeNodes); for (TreeNode treeNode : treeNodes) { System.out.print(treeNode.getSelfId() + "=="); System.out.print(treeNode.getParentId() + "=="); System.out.print(treeNode.getChildId() + "=="); System.out.print(treeNode.getDepth() + "=="); System.out.println(treeNode.isExpand()); } } }
3.让我们测试一下看看...
public class TreeNodeTest { public static void main(String[] args) throws Exception{ new TreeNode(0, 0, 0, 0,true); System.out.println(NodeTreeUtil.getTreeNodes().size()) ; NodeTreeUtil.printListFormat(NodeTreeUtil.getTreeNodes()); 结果: 1==0==3==1==true 2==0==-1==1==true 3==1==4==2==true 4==3==-1==3==true 5==3==-1==3==true } }
public static void main(String[] args) throws Exception{ new TreeNode(1, 0, 3, 0,true); System.out.println(NodeTreeUtil.getTreeNodes().size()) ; NodeTreeUtil.printListFormat(NodeTreeUtil.getTreeNodes()); 结果: 3==1==4==1==true 4==3==-1==2==true 5==3==-1==2==true } }
当我们知道,每个节点的深度,子节点和父节点时,展示这些评论就不是问题了。。。
实际开发,字段扩展
2. 文件换成数据库
3. 这里可以获取任意一个节点下面的所有节点,实际中,我们可以只获取它下面的2个“深度”的评论,提高性能。
4. 性能问题有待提高
标签:
原文地址:http://my.oschina.net/tdd/blog/525062