【问题标题】:Represent tree hierarchy in java [duplicate]在java中表示树层次结构[重复]
【发布时间】:2012-06-26 17:18:17
【问题描述】:
可能重复:
Java tree data-structure?
我想在java中表示一个层次结构。层次结构可以是以下形式
Key
|
|-Value1
| |-Value11
| |-Value111
|-Value2
| |-Value22
|-Value3
|-Value4
谁能建议我用最好的数据结构来表示这种 java 中的层次结构?
【问题讨论】:
标签:
java
data-structures
tree
treeview
【解决方案1】:
基本上,您需要的只是一个可以容纳几个孩子并为属性建模的结构。你可以用这样的类结构来表示它:
public class TreeNode {
private Collection<TreeNode> children;
private String caption;
public TreeNode(Collection<TreeNode> children, String caption) {
super();
this.children = children;
this.caption = caption;
}
public Collection<TreeNode> getChildren() {
return children;
}
public void setChildren(Collection<TreeNode> children) {
this.children = children;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
}
你可以看看这里,以获取一些想法:Java tree data-structure?