Recursive copy of jstree in Java

My tree looks something like this:

image

  • root
  • node

So, I am trying to copy root using node as follows:

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 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 right finished.

Now I'm trying to copy the whole tree. Therefore, I stand on the root - 1and press the copy button, the result:

ss3

but I expect the result:

ss4

The solution to this problem is probably a recursive copy. I see many examples of this, but I don’t understand how to do this in my solution. Can someone help me with this problem and show me an example of how a recursive copy works?

+4
source share
1 answer

I wrote a demo for your case.

Btw, .

( ):

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();
    }
}
+1

All Articles