Possible Duplicate:
Java tree data-structure?
我想用java表示一个层次结构。层次结构可以采用以下形式
Key
|
|-Value1
| |-Value11
| |-Value111
|-Value2
| |-Value22
|-Value3
|-Value4
任何人都可以建议我最好的数据结构来表示 Java 中的这种层次结构吗?
最佳答案
基本上,您需要的只是一个可以容纳一些子级的结构,并且您可以对属性进行建模。您可以用这样的类结构来表示:
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?
关于java - 用java表示树的层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11212656/