title: Need help implementing Binary Search Tree
body: <p>I am trying to implement a binary search tree, and I came across this person's code and was basically trying to replicate it:</p>
  
  <p><a href="https://gist.github.com/mycodeschool/44e1a9183ab0e931f729" rel="nofollow">https://gist.github.com/mycodeschool/44e1a9183ab0e931f729</a></p>
  
  <p>with the only difference being that for some reason, this person did not make the insert or search functions as member functions of a Binary Search Tree class. I was basically trying to replicate his code, but I wanted those operations to be a part of the class (because it makes sense to me to do it that way...isn't that the way it should be done?)</p>
  
  <p>So this is my code right now:</p>
  
  <pre><code>class BSTnode {
  
  public:
  BSTnode(int data) {
  
      BSTnode* right = nullptr;
      BSTnode* left = nullptr;
      this-&gt;data = data;
  }
  
  
  int data;
  BSTnode* right;
  BSTnode* left;
  
  };
  
  class BinarySearchTree{
  public:
  BinarySearchTree() {
  
      root = nullptr;
  
  }
  
  BSTnode* insert(BSTnode* root, int data) {
      if (root == nullptr) {
          BSTnode* newNode = new BSTnode(data);
          root = newNode;
          return root;
      //  std::cout &lt;&lt; root-&gt;data &lt;&lt; endl;
      }
  
      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;
  
  
  }
  public:
      BSTnode* root;
  
  };
  </code></pre>
  
  <p>In my main I have the following:</p>
  
  <pre><code>BinarySearchTree bst;
  bst.insert(bst.root, 4);
  
  bst.insert(bst.root, 3);
  bst.insert(bst.root, 4);
  bst.insert(bst.root, 3);
  bst.insert(bst.root, 2);
  
  
  std::cout &lt;&lt; bst.root-&gt;data &lt;&lt; std::endl;
  </code></pre>
  
  <p>My first problem..I have the line of code </p>
  
  <pre><code>std::cout &lt;&lt; bst.root-&gt;data &lt;&lt; std::endl;
  </code></pre>
  
  <p>at the end of my main just to test if things are working as should. However, when I try to run this, the program crashes. When I debug that particular line, I get the error :</p>
  
  <p>Unhandled exception at 0x00e6160a in BST_INTERVIEW.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd. </p>
  
  <p>Why is this happening? Secondly, 
  my code basically just ends up making every element that I insert be the new root. When I debug, it says that everytime I insert, the condition root == nullptr is true, and so every new element is basically set as the root. I assume that when I'm calling insert after the first time, the root is not pointing to the actual root node of the BST, and I assume it is because I have to set this->root = newNode, as oppose to just root. But I feel like then I also have to add "this" at other points in the code, and I am just kinda confused. Can somebody help me make my code like the one in the link provided, but with BST being an actual object with member functions insert, search, etc...?</p>
  
  <p>Edit: Per @Semyon Burov's answer, I changed the parameters for the insert function to be BSTnode*&amp;. I also had to fix my constructor, which should have said this->left and this->right instead of BSTnode* right and BSTnode* left</p>