How to copy a tree where each node has more that t

2019-06-11 09:15发布

I would like to copy a TreeItem in javafx.

Each node of the tree has more than two leaves. I would like to copy the whole tree. I need a deep copy of that tree. Please help me. Thank you

I am working with TreeView and TreeItem

1条回答
放我归山
2楼-- · 2019-06-11 09:29

How about using this pattern:

class Item {
    //....
}

TreeItem<Item> deepcopy(TreeItem<Item> item) {
    TreeItem<Item> copy = new TreeItem<Item>(item.getValue());
    for (TreeItem<Item> child : item.getChildren()) {
        copy.getChildren().add(deepcopy(child));
    }
    return copy;
}
查看更多
登录 后发表回答