简体   繁体   English

Java中的jstree递归副本

[英]jstree recursive copy in Java

My tree looks something like this: 我的树看起来像这样:

图片

  1. root
  2. node 节点

So I try to copy root with node in this way: 所以我尝试用这种方式复制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;
    }

After that receives: 之后收到:

ss2

So everything is correctly finished. 因此,一切均已正确完成。

Now I try to copy again the whole tree. 现在,我尝试再次复制整个树。 So I stand on root - 1 and press copy button, result: 所以我站在root- 1 ,然后按复制按钮,结果是:

ss3

but I expect that result: 但我希望得到这样的结果:

ss4

The solution of that problem is probably recursive copy. 该问题的解决方案可能是递归复制。 I see many examples of that, but I don't understand how to do that in my solution. 我看到了很多例子,但是我不明白如何在解决方案中做到这一点。 Could somebody help me with that problem and show me an example of how recursive copy works? 有人可以帮助我解决这个问题,并举例说明递归副本如何工作吗?

I've written the demo for your case. 我已经为您编写了演示文件。

Btw, I've found a little error in the last picture. 顺便说一句,我在最后一张照片中发现了一个小错误。

Here is correct (the fragment of the demo program output): 这是正确的(演示程序输出的片段):

1

    2

    1-copied

        2-copied

    1

        2

        1-copied

             2-copied

Sources: 资料来源:

Main.java 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 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM