I have tree like following example, where every leaf is object.
[1]
|--[2]
| |--[3]
| |--[4]
| |--[5]
|--[6]
Class structure is:
public class Node {
private Integer id;
private List<Node> children;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public List<Node> getChildren()
{
return children;
}
public void setChildren(List<Node> children)
{
this.children = children;
}
}
If leaf 3 is selected, then breadcrumbs are 1 2 3. How can I create breadcrumbs from this tree? I know how I can recursively create tree, but I don’t know how I can select correct leafs to breadcrumbs.
You need a parent pointer in the children. When you add children, the parent node can set the children’s parent pointer to itself.
You can then easily use this to build your breadcrumbs from a selected leaf node.