Java 中的 jstree 递归副本

jstree recursive copy in Java

我的树看起来像这样:

  1. 节点

所以我尝试用这种方式复制root与节点:

public void copy() {    
    List<Trees> nodes = findNodes(oldIdRoot);   //find nodes
    Integer newIdRoot = method.add(); //add root

    copyNodes(nodes, newIdRoot, oldIdRoot);  //add node
}

private void copyNodes(List<Trees> nodes, Integer newIdRoot, oldIdRoot) {
    // probably after the second copying list of nodes is clear and that's the problem 
    for (Trees trees : nodes) {  
        Integer nId= method.add(trees, newIdRoot); 
        //here should be recursion ...
    }
  }

    private List<Trees> findNodes(Integer oldIdRoot)  {
                .......
        List<Trees> foundedNodes = new ArrayList<Trees>();
        foundedNodes.add(...)); // here add all nodes from my Tree value
        return foundedNodes;
    }

之后收到:

所以一切都正确完成了。

现在我尝试再次复制整棵树。所以我站在 root - 1 并按下复制按钮,结果:

但我希望这样的结果:

那个问题的解决方案可能是递归复制。我看到很多这样的例子,但我不明白如何在我的解决方案中做到这一点。有人可以帮我解决这个问题并举例说明递归复制的工作原理吗?

我已经为您的案例编写了演示。

顺便说一句,我在最后一张图片中发现了一点错误。

这里是正确的(演示程序输出的片段):

1

    2

    1-copied

        2-copied

    1

        2

        1-copied

             2-copied

来源:

Main.java

package org.recursion.example;

import org.recursion.example.tree.TreeNode;

public class Main
{
    public TreeNode tree;

    public Main( String[] args )
    {
        tree = new TreeNode(); //1
        tree.addChild( new TreeNode() ); //2
    }

    public static void main( String[] args )
    {
        //initial state
        Main app = new Main( args );
        System.out.print( "\nInitial state" );
        app.tree.print( 0 );

        System.out.print( "\nCopying 1" );
        app.tree.addChild( app.copy( 1 ) );
        TreeNode n = app.tree.getChild( 1 );
        n.setName( n.getName() + "-copied" );
        n = n.getChild( 0 );
        n.setName( n.getName() + "-copied" );
        app.tree.print( 0 );

        System.out.print( "\nCopying 2" );
        app.tree.addChild( app.copy( 1 ) );
        app.tree.print( 0 );
    }

    private static TreeNode findNode( TreeNode node, int id )
    {
        if ( node.getId() == id )
        {
            return node;
        }
        if ( node.isLeaf() )
        {
            return null; //no such nodes in this branch
        }
        //else search in depth
        for ( int i = 0; i < node.getChildrenCount(); i++ )
        {
            TreeNode child = node.getChild( i );
            TreeNode node1 = findNode( child, id );
            if ( node1 != null )
            {
                return node1;
            }
        }

        return null;  //no such nodes in the subTree
    }

    public TreeNode copy( int nodeId )
    {
        TreeNode oldNode = findNode( tree, nodeId );
        if ( oldNode == null )
        {
            return null;
        }

        return copy( oldNode );
    }

    private TreeNode copy( TreeNode node )
    {
        TreeNode newNode = new TreeNode( node.getName() );

        for ( int i = 0; i < node.getChildrenCount(); i++ )
        {
            newNode.addChild( copy( node.getChild( i ) ) );
        }

        return newNode;
    }
}

TreeNode.java

package org.recursion.example.tree;

import java.util.ArrayList;
import java.util.List;

public class TreeNode
{
    private static int count;

    private int id;
    private String name;
    private List<TreeNode> children = new ArrayList<TreeNode>();

    public TreeNode( String name )
    {
        id = newId();
        setName( name );
    }

    private static int newId()
    {
        return ++count;
    }

    public TreeNode()
    {
        id = newId();
        setName( id );
    }

    public int getChildrenCount()
    {
        return children.size();
    }

    public TreeNode getChild( int i )
    {
        return children.get( i );
    }

    public void addChild( TreeNode child )
    {
        children.add( child );
    }

    public boolean isLeaf()
    {
        return children.isEmpty();
    }

    public int getId()
    {
        return id;
    }

    public void setName( String name )
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setName( int id )
    {
        setName( String.valueOf( id ) );
    }

    public void print( int ident )
    {
        System.out.println();
        System.out.print( fill( ident ) );
        System.out.print( name );

        ident += 4;
        for ( int i = 0; i < getChildrenCount(); i++ )
        {
            TreeNode child = getChild( i );
            child.print( ident );
        }
    }

    private String fill( int i )
    {
        StringBuilder sb = new StringBuilder();
        for ( int j = 0; j < i; j++ )
        {
            sb.append( ' ' );
        }

        return sb.toString();
    }
}