title: C++ tree data structure Access violation with String
body: <p>I am trying to make a tree data structure with c++. Originally I tested with int data, it was going fine. However, when I try to use a string data type, it keeps showing an unhandled exception. It said read access violation, this was 0x5D. I don't know what is causing the error. The complete same code only changing int to string causes the violation.</p>
  <p>Main:</p>
  <pre><code>#include &lt;bits/stdc++.h&gt;
  #include &quot;Tree.h&quot;
  using namespace std;
  
  int main()
  {
      Tree A;
      int choice; bool isCre=0;
  
      cout &lt;&lt; &quot;Input choice : &quot;;
      while (cin &gt;&gt; choice) {
          switch (choice)
          {
          case 1:
              if (!isCre) {
                  A.create_tree(); isCre = 1;
                  break;
              }
              else {
                  A.add_tree();
                  break;
              }
          case 2:
              A.delete_tree(); break;
          case 3:
              A.modify_tree(); break;
          case 4:
              A.display_tree(); break;
          }
          cout &lt;&lt; &quot;Input choice : &quot;;
      }
  
  }
  
  </code></pre>
  <p>Header file:</p>
  <pre><code>#ifndef TREE_H
  #define TREE_H
  
  #include &lt;bits/stdc++.h&gt;
  using namespace std;
  
  class Tree
  {
  private:
      struct Tre
      {
          Tre* parent; Tre* right; Tre* left;
          string data;
          Tre(Tre* parent, string data)
          {
              this-&gt;parent = parent;
              this-&gt;data = data;
              left = right = NULL;
          }
      };
  
      Tre* root;
  
      Tre* create(Tre*&amp; root, string data)
      {
          Tre* p;
          p = new Tre(NULL, data);
          root = p;
          return root;
      }
  
      void dis(Tre* node)
      {
          if (node == NULL)
              return;
  
          cout &lt;&lt; node-&gt;data &lt;&lt; endl;
  
          dis(node-&gt;left);
  
          dis(node-&gt;right);
      }
  
      Tre* search(Tre* node, string data)
      {
          if (node == NULL)
              return NULL;
  
          if (node != NULL &amp;&amp; node-&gt;data == data)
              return node;
  
          /* then recur on left subtree */
          Tre* p = search(node-&gt;left, data);
          // node found, no need to look further
          if (p != NULL &amp;&amp; p-&gt;data == data) return p;
  
          /* node is not found in left,
          so recur on right subtree */
          Tre* q = search(node-&gt;right, data);
          if (q != NULL &amp;&amp; q-&gt;data == data) return q;
      }
  
      Tre* add(Tre* root, string find, string input)
      {
          Tre* p = root;
          p = search(root, find);
  
          if (p-&gt;left != NULL)
          {
              p-&gt;right = new Tre(p, input);
              //p-&gt;left = new Tre(p, input);
          }
          else if (p-&gt;left == NULL)
          {
              p-&gt;left = new Tre(p, input);
              //p-&gt;right = new Tre(p, input);
          }
  
          return root;
      }
  
      Tre* del(Tre* root, string data)
      {
          Tre* p = root, * q;
          p = search(root, data);
          q = p-&gt;parent;
          if (q-&gt;right != NULL &amp;&amp; q-&gt;right-&gt;data == data)
          {
              q-&gt;right = NULL;
              delete p;
          }
          else if (q-&gt;left-&gt;data == data)
          {
              q-&gt;left = NULL;
              delete p;
          }
  
          return root;
      }
  
      Tre* modify(Tre* root, string find, string input)
      {
          Tre* p = root;
          p = search(p, find);
  
          p-&gt;data = input;
  
          return root;
      }
  
  public:
      void create_tree();
      void display_tree();
      void add_tree();
      void delete_tree();
      void modify_tree();
  };
  
  #endif
  
  #endif
  </code></pre>
  <p>Implementation file:</p>
  <pre><code>#include &quot;Tree.h&quot;
  
  void Tree::create_tree()
  {
      string data;
      cout &lt;&lt; &quot;Input data : &quot;; cin &gt;&gt; data;
      create(root, data);
  }
  
  void Tree::display_tree()
  {
      dis(root);
  }
  
  void Tree::add_tree()
  {
      string find, input;
      cout &lt;&lt; &quot;Parent data : &quot;; cin &gt;&gt; find;
      cout &lt;&lt; &quot;New data : &quot;; cin &gt;&gt; input;
      add(root, find, input);
  }
  
  void Tree::delete_tree()
  {
      string data;
      cout &lt;&lt; &quot;Delete data : &quot;; cin &gt;&gt; data;
      del(root, data);
  }
  
  void Tree::modify_tree()
  {
      string find, input;
      cout &lt;&lt; &quot;Find data : &quot;; cin &gt;&gt; find;
      cout &lt;&lt; &quot;New data : &quot;; cin &gt;&gt; input;
      modify(root, find, input);
  }
  
  </code></pre>