Java 正在复制下一个兄弟树的第一个子树

Java 正在复制下一个兄弟树的第一个子树,java,recursion,tree,Java,Recursion,Tree,我已经用java实现了“第一个孩子下一个兄弟姐妹”树的类 下面是一个表示这样一棵树的链接 我实施了以下功能: addChild(); getLabel(); setLabel(T v); getParent(); getNextSibling(); getFirstChild(); My addChild函数按以下顺序添加子函数 public void addChild(Tree<T> c) { c.parent = this; if (firstChild

我已经用java实现了“第一个孩子下一个兄弟姐妹”树的类

下面是一个表示这样一棵树的链接

我实施了以下功能:

addChild();
getLabel(); 
setLabel(T v);
getParent();
getNextSibling();
getFirstChild();
My addChild函数按以下顺序添加子函数

public void addChild(Tree<T> c) {
     c.parent = this;
     if (firstChild == null) 
       firstChild = c;
     else {
         c.nextSibling = firstChild;
         firstChild = c;
        }
}

That is, if we have a tree node 1 and we add tree node 2 and then tree node 3 to it then the final tree would be,
1.addChild(2);
1.addChild(3);

 1                                          1
/ \    which is internally stored as       /
3   2                                      3 - 2
The most recent child added would be the first child
public void addChild(树c){
c、 父=此;
if(firstChild==null)
第一个孩子=c;
否则{
c、 nextSibling=第一个孩子;
第一个孩子=c;
}
}
也就是说,如果我们有一个树节点1,我们添加树节点2,然后再添加树节点3,那么最终的树是,
1.addChild(2);
1.儿童(3);
1                                          1
/\在内部存储为/
3   2                                      3 - 2
最近添加的孩子将是第一个孩子
我想实现一个CopyTree函数,当给定任何这样的树作为参数时,它将创建树的副本并返回它。 我有一些初始代码,但无法获得正确的递归

private Tree<String> CopyTree(Tree<String> tr){
if (tr == null)
    return null;
Tree<String> t = new Tree<String>();
t.setLabel(tr.getLabel());
if (tr.getFirstChild() != null) {
    t.addChild(CopyTree(tr.getFirstChild()));
}
Tree<String> temp = tr.left();

if (temp != null) {
while (temp.getNextSibling() != null) {
    t.addChild(CopyTree(temp.getNextSibling()));
    temp = temp.getNextSibling();
}
}
return t;
}
private Tree CopyTree(Tree tr){
if(tr==null)
返回null;
树t=新树();
t、 setLabel(tr.getLabel());
if(tr.getFirstChild()!=null){
t、 addChild(CopyTree(tr.getFirstChild());
}
树温度=tr.left();
如果(温度!=null){
while(temp.getNextSibling()!=null){
t、 addChild(CopyTree(temp.getNextSibling());
temp=temp.getNextSibling();
}
}
返回t;
}
如何使递归工作


提前感谢

首先,我相信您这里有一个错误:

 while (temp.getNextSibling() != null) {
    t.addChild(CopyTree(temp.getNextSibling()));
    temp = temp.getNextSibling();
}
由于getNextSibling()重新运行右边的下一个子项,而addChild()从左边插入一个子项,因此这里的顺序颠倒了。这可能没问题,但要尽量避免

为了回答您的问题,递归函数应该作为新树的每个节点上的方法调用,并接收旧树的相应节点作为参数。然后,它应该将这个节点的子节点从旧树复制到新树中,并在执行此操作时,对每个子节点调用递归函数。

明白了

private Tree<String> CopyTree(Tree<String> tr){
if (tr == null)
    return null;
Tree<String> t = new Tree<String>();
t.setLabel(tr.getLabel());

Tree<String> temp = tr.left();

if (temp != null) {
    ArrayList<Tree<String>> list = new ArrayList<>();
    while (temp.getNextSibling() != null) {
    list.add(temp.getNextSibling());
    //t.addChild(CopyTree(temp.getNextSibling()));
    temp = temp.getNextSibling();
}
for (int i = (list.size()-1); i>=0; i--) {
    t.addChild(CopyTree(list.get(i)));
}

}
if (tr.left() != null) {
    t.addChild(CopyTree(tr.left()));
}

return t;
}
private Tree CopyTree(Tree tr){
if(tr==null)
返回null;
树t=新树();
t、 setLabel(tr.getLabel());
树温度=tr.left();
如果(温度!=null){
ArrayList=新建ArrayList();
while(temp.getNextSibling()!=null){
添加(temp.getNextSibling());
//t、 addChild(CopyTree(temp.getNextSibling());
temp=temp.getNextSibling();
}
对于(int i=(list.size()-1);i>=0;i--){
t、 addChild(CopyTree(list.get(i));
}
}
如果(tr.left()!=null){
t、 addChild(CopyTree(tr.left());
}
返回t;
}