title: Understanding the recursive build of trees(data.tree) in R
body: <p>I am trying to build trees recursively in R using <code>data.tree</code> package.
  Anyone who is answering please understand recursion below. The code below contains only two levels of recursion. In the second level recursion I printed the tree structure (marked the print statement in my code for reference), it is similar the image below.</p>
  
  <p><a href="https://i.stack.imgur.com/cm92C.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cm92C.jpg" alt="enter image description here"></a></p>
  
  <p>After the entire completion of code the tree structure is.<a href="https://i.stack.imgur.com/TBSdj.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TBSdj.jpg" alt="enter image description here"></a></p>
  
  <p>I do not understand how the tree from level-2 recursion perfectly merged with the tree from level-1.</p>
  
  <pre><code>library(data.tree)
  library(visNetwork)
  
  recurs=function(i,previous_iteration_tree)
  {
    if(i&lt;=6)
    {
      if(i==1)
      {
        tree&lt;-Node$new(i)
        tree$AddChild(i+1)
        tree$AddChild(i+2)
        tree$AddChild(i+3)
        tree$AddChild(i+4)
      }
      if(i==6)
      {
        dummy&lt;-FindNode(previous_iteration_tree,i-4)
        a=dummy$AddChild(i)
        tree=a
  
        print(tree)#&lt;------------------HERE
        #see the tree here
      }
    }
    else
      return(0)
  
    recurs(i+5,tree)
  
    plot(tree)
  }
  
  recurs(1,0)
  </code></pre>
  
  <p>Please help me out in this case!</p>