title: Should I avoid reference to pointer in modern C++ or it is okay in this special case
body: <p>I'm aware that raw pointers (or more accurately <code>new</code> and <code>delete</code>)  in modern C++ must be avoided unless really needed. I believe when it comes to graph implementation, pointers are pretty useful. I'd be happy to hear comments on this though. </p>
  
  <p>I'm playing around with Binary Search Trees and writing <code>insert</code> functions. I implemented using two different but similar methods, one using pointer and the other one using reference to pointer. Clearly I would like to change the the pointer itself (ref to ptr). Wondering if any of them has advantages or either should not be used and must be written using const reference or smart pointer (modern way).</p>
  
  <pre><code>Node *insert_ptr(Node *root, int data)
  {
      if (root == NULL)
          return create(data);
      else if (data &lt; root-&gt;data)
          root-&gt;left = insert(root-&gt;left, data);
      else
          root-&gt;right = insert(root-&gt;right, data);
      return root;
  }
  
  void insert_ref_to_ptr(Node *&amp;root, int data)
  {
      if (root == NULL)
          root = create(data);
      else if (data &lt; root-&gt;data)
          insert(root-&gt;left, data);
      else
          insert(root-&gt;right, data);
  }
  </code></pre>
  
  <p>I'm providing the rest of code below if you want to get a run. You may want to use <code>1 3 6 8 10 14 13 4 7</code> as input</p>
  
  <pre><code>struct Node
  {
      int data;
      Node *left;
      Node *right;
  };
  
  void display(Node *root)
  {
      if (root != NULL)
      {
          display(root-&gt;left);
          cout &lt;&lt; root-&gt;data &lt;&lt; " ";
          display(root-&gt;right);
      }
  }
  
  Node *create(int data)
  {
      Node *node = new Node();
      node-&gt;data = data;
      node-&gt;left = node-&gt;right = NULL;
      return node;
  }
  
  int main()
  {
      Node *root = NULL;
      vector&lt;int&gt; nodes;
  
      string line;
      int value;
      getline(cin, line);
      stringstream split(line);
      while (split &gt;&gt; value)
          nodes.push_back(value);
      /*
      root = insert_ptr(root, nodes[0]);
      for (int i = 1; i &lt; nodes.size(); i++)
          insert_ptr(root, nodes[i]);
      */
  
      for (int i = 0; i &lt; nodes.size(); i++)
          insert_ref_to_ptr(root, nodes[i]);
      display(root);
      return 0;
  }
  
  </code></pre>