Java中树形结构的实现 [英] Tree Structure Implementation in java

查看:445
本文介绍了Java中树形结构的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1>
我应该如何在基于Java的应用程序中实现树结构".我如何将条目映射到数据库表?此实现需要多少张表.

1>
How should i implement a "Tree Structure" in Java Based application.?How would i map the entries to the database table?how many tables will i require for this implementation.?

List records<String> = new ArrayList<String>(); 
records.add("null"); 
records.add("Product"); 
records.add("Category"); 
records.add("P1"); 
records.add("P2"); 
records.add("P3"); 
records.add("C1"); 
records.add("C2"); 
records.add("C3"); 


所以我应该如何为上述记录集实现树形结构.
//P1,P2,P3属于父级{Product}
//C1,C2,C3属于父级{Category}
提供的示例代码将很有帮助.


so how should i implement a Tree Structure for the above record set.
//P1,P2,P3 belong to Parent {Product}
//C1,C2,C3 belong to Parent {Category}
Sample code provided will be helpful

推荐答案

您将需要2个值.要获得扁平"树,您需要父母和孩子的钥匙.

You would need 2 values for that. To get a "flat" tree you need the key of the parent and the child.

null, Product
Product, P1
Product, P2
Product, P3
P3, S1
P3, S2
null, Category
Category, C1
Category, C2
Category, C3

Above means:
Product
  +---P1
  +---P2
  +---P3
       +---S1
       +---S2
Category
  +---C1
  +---C2
  +---C3



因为在此示例中直接使用了名称",所以可能导致名称冲突.因此,定义一个唯一的id并将其用于将孩子与父母联系起来会更安全.这将变为:



Because the "name" in this example is directly used, this could lead to name collisions. It would therefore be more safe to define an unique id and use this to link the children to parents. This would become something like:

1, null, Product
2, 1, P1
3, 1, P2
4, 1, P3
etc...



祝你好运!



Good luck!


这篇关于Java中树形结构的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆