title: How to create a binary tree using only base R?
body: <p>What is the optimal way (assuming that, in this case, optimal means &quot;the fastest way to create or access&quot;) of creating a binary tree using only base R capabilities/tools? I'm assuming some form of recursion and/or data structures using environment manipulation would be necessary?</p>
  <p>More so, I'd like the binary tree creation to be parameterized as in what type of tree should be generated (for example: a perfect one, where all nodes have two children, or not?).</p>
  <p>Example:</p>
  <pre><code>my_tree &lt;- grow_tree(perfect = FALSE, max_height = 3)
  print(my_tree)
  
  my_tree[1]
  1
  
  my_tree[1][left]
  2
  
  my_tree[1][right]
  3
  
  my_tree[1][left][left]
  4
  </code></pre>
  <p>Should be a representation of a tree that looks like:</p>
  <pre><code>    1
     / \
    2   3
   /
  4
  </code></pre>
  <p>Note: feel free to use S3 or S4, considering they are provided in base R. However, it would be interesting to see a solution without them.</p>