评价体系的树结构

1、数据准备

这里,我用文件存储了一些数据,如果在真实开发环境下,可以用数据库存储。

他们的字段分别是: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

2、计算

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

	}

}

当我们知道,每个节点的深度,子节点和父节点时,展示这些评论就不是问题了。。。

3、扩展

  1. 实际开发,字段扩展

2.  文件换成数据库

3.  这里可以获取任意一个节点下面的所有节点,实际中,我们可以只获取它下面的2个“深度”的评论,提高性能。

4.  性能问题有待提高

时间: 2024-08-06 20:06:12

评价体系的树结构的相关文章

树结构在程序设计中的运用

                                                                                引言 近年来,由于各种竞赛纷纷采用free-pascal,因此对于算法来说,空间效率上的要求降低了,而对时间效率却提出了更高的要求.这使得选手不仅要娴熟地掌握常规算法,而且要大胆创新,构造更高效的算法来解决问题. 在以往的程序设计中,链式结构采用得较多.的确链式结构有编程复杂度低.简单易懂等优点,但有一个致命的弱点:相邻的两个元素间的联系

oracle分层查询中的start with和connect by(树结构查询)

来源:  http://blog.csdn.net/itmyhome1990/article/details/16338637 ORACLE是一个关系数据库管理系统,它用表的形式组织数据,在某些表中的数据还呈现出树型 结构的联系. 例如有如下案例: 数据为节选,字段值含义分别为税务机构代码.税务机构名称.上级税务机构代码,税务机构级别 select * from extern_dm_swjg查询的时候默认顺序就是上面的顺序,可以看出是混乱的并没有特殊结构特征. 而希望的结果如下图: sj_swj

第一章: 在RDB中的树结构数据

第一章: 在RDB中的树结构数据 在本章中,我将写一个基本的知识来理解这个问题 一  模型的作用 RBD处理树模型的作用总结为两点: 1  在RDB表中保存树的数据 2  效率的查询节点的相关节点 1  在RDB表中保存树的数据 我们可以定义的标准,该模型是否具有存储层次数据的功能 可以由保存的所有节点再现原有的层次结构 如果不能通过保存的数据再现原有的树结构,就不能说这个模型实现了树. 2  效率的查询节点的相关节点 通常,我们将数据保存到数据库中进行搜索,数据中保存了分层数据,可能会查询任何

用dfs序维护树结构

给定一棵n个节点的树,m次查询,每次查询需要求出某个节点深度为h的所有子节点. 对于这个问题如果试图去对每个节点保存所有深度的子节点,在数据大的时候内存会吃不消:或者每次查询的时候去遍历一遍,当数据大的时候,时间效率会非常低. 此时如果使用dfs序维护树结构就可以轻松地解决这个问题. 作为预处理,首先将将树的所有节点按深度保存起来,每个深度的所有节点用一个线性结构保存,每个深度的节点相对顺序要和前序遍历一致. 然后从树的根节点进行dfs,对于每个节点记录两个信息,一个是dfs进入该节点的时间戳i

索引深入浅出:非聚集索引的B树结构在聚集表

一个表只能有一个聚集索引,数据行以此聚集索引的顺序进行存储,一个表却能有多个非聚集索引.我们已经讨论了聚集索引的结构,这篇我们会看下非聚集索引结构. 非聚集索引的逻辑呈现 简单来说,非聚集索引是表的子集.当我们定义了一个非聚集索引时,SQL Server把整套非聚集索引键存在不同的页里.我们来看下一个包含BusinessEntityID(PK),PersonType,FirstName,LastName这4列的表,这个表上有一个非聚集索引定义.主体表按BusinessEntityID列(聚集索引

一行python代码实现树结构

树结构是一种抽象数据类型,在计算机科学领域有着非常广泛的应用.一颗树可以简单的表示为根, 左子树, 右子树. 而左子树和右子树又可以有自己的子树.这似乎是一种比较复杂的数据结构,那么真的能像我们在标题中所说的那样,用一行Python代码就可以实现吗? 一行代码实现? 由于树形结构的外层和内层有着相似的结构,所以多可以用递归的方式定义树.再利用Python中提供的defaultdict,我们就可以很轻松地定义树了,而且只有一行代码. from collections import defaultd

asp.net mvc+EF 递归生成树结构返回json

0.数据表结构,主要属性有:Id.parentId(父节Id).Text.Url……等等. 1.新建一个树结构MenuModels 1 public class MenuModels 2 { 3 private int _id; 4 private string _text; 5 private int? _parentid; 6 private string _icon; 7 private string _url; 8 private object _menus; 9 private Dic

react+redux教程(五)异步、单一state树结构、componentWillReceiveProps

教程目录 react+redux教程(一)connect.applyMiddleware.thunk.webpackHotMiddleware react+redux教程(二)redux的单一状态树完全替代了react的状态机? react+redux教程(三)reduce().filter().map().some().every()....展开属性 react+redux教程(四)undo.devtools.router react+redux教程(五)异步.单一state树结构.compo

Android应用源代码ListView实现的文件夹树结构

Android应用源代码ListView实现的文件夹树结构 点击加号能够展开,点击减号能够收起这一个节点 源代码下载地址:http://download.csdn.net/detail/kiduo08/7711711