title: PhoneEntry : Binary Search Tree c++ program
body: <p>I want to develop a BST program which load and store contact information which are name, phone number and email from/to text.file.</p>
  
  <p>Currently, I can store,load, insert, delete and print the name along with its phone number too. But after I enhanced the code by add another information which is email, the BST program have getting a mess.  </p>
  
  <p>Before explaining about the problem that I got, please take a look first at my <strong>phonebook.txt:</strong>
  <a href="https://i.stack.imgur.com/gShZY.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gShZY.jpg" alt="enter image description here"></a></p>
  
  <p>While below is the my <strong>output</strong> of BST program.
  <a href="https://i.stack.imgur.com/ufEbE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ufEbE.png" alt="enter image description here"></a></p>
  
  <p>From the output above, the problem is the program read the email as a name. But the program still read phone number as a phone number. And the email is not shown/not being declared at all? And I don't know where is the real name went to?</p>
  
  <p>For getting it clear, let's look at the output of all contacts in phonebook.txt by choose <strong>2. All Contacts Information</strong> from the program
  <a href="https://i.stack.imgur.com/abF0O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/abF0O.png" alt="enter image description here"></a></p>
  
  <p>There is no name(Adam,Faris,TNB....) shown. But email(eg: adams@rockmail.com) was read as name..</p>
  
  <p>I got the original BST program(name+phonenumber) from <a href="https://www.daniweb.com/programming/software-development/threads/327525/binary-search-tree-file-reading" rel="nofollow noreferrer">https://www.daniweb.com/programming/software-development/threads/327525/binary-search-tree-file-reading</a>  </p>
  
  <p>Then I enhanced the code by adding the email part.
  Here's my code:</p>
  
  <pre><code>//Binary Search Tree Program
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;fstream&gt;
  #include &lt;iomanip&gt;
  #include &lt;stdio.h&gt;
  using namespace std;
  //typedef string ItemType;
  class Person{
      private:
      string name;
      int phonenumber;
      string email;
      public:
      Person();
      Person(string name, int phonenumber, string email);
      string getName();
      int getPhonenumber();
      string getEmail();
      void setName(string newname);
      void setPhonenumber(int newphonenumber);
      void setEmail(string newemail);
  };
  class BinarySearchTree
  {
      private:
          struct tree_node
          {
             tree_node* left;
             tree_node* right;
             //ItemType data;
             Person data;
          };
          tree_node* root;
      public:
          BinarySearchTree()
          {
             root = NULL;
          }
          bool isEmpty() const { return root==NULL; }
          void print_inorder();
          void inorder(tree_node*);
          void insert(Person);
          void remove(string);
          void search(string key);
          void changePhonenumber(string key, int newnumber);
  };
  Person::Person()
  {
  }
  Person::Person(string newname, int newphonenumber, string newemail)
  {
      name = newname;
      phonenumber = newphonenumber;
      email = newemail;
  }
  string Person::getName() {
      return name;
  }
  int Person::getPhonenumber() {
      return phonenumber;
  }
  string Person::getEmail() {
      return email;
  }
  void Person::setName(string newname) {
      name = newname;
  }
  void Person::setPhonenumber(int newphonenumber) {
      phonenumber = newphonenumber;
  }
  void Person::setEmail(string newemail) {
      name = newemail;
  }
  // Smaller elements go left
  // larger elements go right
  void BinarySearchTree::insert(Person p)
  {
      tree_node* t = new tree_node;
      tree_node* parent;
      t-&gt;data = p;
      t-&gt;left = NULL;
      t-&gt;right = NULL;
      parent = NULL;
      // is this a new tree?
      if(isEmpty()) root = t;
      else
      {
          //Note: ALL insertions are as leaf nodes
          tree_node* curr;
          curr = root;
          // Find the Node's parent
          while(curr)
          {
              parent = curr;
              if(t-&gt;data.getName() &gt; curr-&gt;data.getName()) curr = curr-&gt;right;
              else curr = curr-&gt;left;
          }
          if(t-&gt;data.getName() &lt; parent-&gt;data.getName())
             parent-&gt;left = t;
          else
             parent-&gt;right = t;
      }
  }
  void BinarySearchTree::remove(string p)
  {
      //Locate the element
      bool found = false;
      if(isEmpty())
      {
          cout&lt;&lt;" This Tree is empty! "&lt;&lt;endl;
          return;
      }
      tree_node* curr;
      tree_node* parent;
      curr = root;
      while(curr != NULL)
      {
           if(curr-&gt;data.getName() == p)
           {
              found = true;
              break;
           }
           else
           {
               parent = curr;
               if(p&gt;curr-&gt;data.getName()) curr = curr-&gt;right;
               else curr = curr-&gt;left;
           }
      }
      if(!found)
           {
          cout&lt;&lt;" Data not found! "&lt;&lt;endl;
          return;
      }
           // 3 cases :
      // 1. We're removing a leaf node
      // 2. We're removing a node with a single child
      // 3. we're removing a node with 2 children
      // Node with single child
      if((curr-&gt;left == NULL &amp;&amp; curr-&gt;right != NULL)|| (curr-&gt;left != NULL
  &amp;&amp; curr-&gt;right == NULL))
      {
         if(curr-&gt;left == NULL &amp;&amp; curr-&gt;right != NULL)
         {
             if(parent-&gt;left == curr)
             {
               parent-&gt;left = curr-&gt;right;
               delete curr;
             }
             else
             {
               parent-&gt;right = curr-&gt;right;
               delete curr;
             }
         }
         else // left child present, no right child
         {
            if(parent-&gt;left == curr)
             {
               parent-&gt;left = curr-&gt;left;
               delete curr;
             }
             else
             {
               parent-&gt;right = curr-&gt;left;
               delete curr;
             }
         }
       return;
      }
           //We're looking at a leaf node
           if( curr-&gt;left == NULL &amp;&amp; curr-&gt;right == NULL)
      {
          if(parent-&gt;left == curr) parent-&gt;left = NULL;
          else parent-&gt;right = NULL;
                   delete curr;
                   return;
      }
      //Node with 2 children
      // replace node with smallest value in right subtree
      if (curr-&gt;left != NULL &amp;&amp; curr-&gt;right != NULL)
      {
          tree_node* chkr;
          chkr = curr-&gt;right;
          if((chkr-&gt;left == NULL) &amp;&amp; (chkr-&gt;right == NULL))
          {
              curr = chkr;
              delete chkr;
              curr-&gt;right = NULL;
          }
          else // right child has children
          {
              //if the node's right child has a left child
              // Move all the way down left to locate smallest element
              if((curr-&gt;right)-&gt;left != NULL)
              {
                  tree_node* lcurr;
                  tree_node* lcurrp;
                  lcurrp = curr-&gt;right;
                  lcurr = (curr-&gt;right)-&gt;left;
                  while(lcurr-&gt;left != NULL)
                  {
                     lcurrp = lcurr;
                     lcurr = lcurr-&gt;left;
                  }
          curr-&gt;data = lcurr-&gt;data;
                  delete lcurr;
                  lcurrp-&gt;left = NULL;
             }
             else
             {
                 tree_node* tmp;
                 tmp = curr-&gt;right;
                 curr-&gt;data = tmp-&gt;data;
             curr-&gt;right = tmp-&gt;right;
                 delete tmp;
             }
          }
           return;
      }
  }
  void BinarySearchTree::print_inorder()
  {
    inorder(root);
  }
  void BinarySearchTree::inorder(tree_node* p)
  {
  
      if(p != NULL)
      {
          if(p-&gt;left) inorder(p-&gt;left);
          cout&lt;&lt;"  "&lt;&lt;p-&gt;data.getName()&lt;&lt;"\t\t"&lt;&lt;p-&gt;data.getPhonenumber()&lt;&lt;"\t\t"&lt;&lt;p-&gt;data.getEmail()&lt;&lt;endl;
          if(p-&gt;right) inorder(p-&gt;right);
      }
      else return;
  }
  
  //////////////////new/////////////////////////
  void BinarySearchTree::search(string key)
  {
       bool found = false;
      if(isEmpty())
      {
          cout&lt;&lt;" This Tree is empty! "&lt;&lt;endl;
          return;
      }
      tree_node* curr;
      tree_node* parent;
      curr = root;
      while(curr != NULL)
      {
           if(curr-&gt;data.getName() == key)
           {
              found = true;
              cout &lt;&lt; "The phone number for " &lt;&lt; key &lt;&lt; " is " &lt;&lt; curr-&gt;data.getPhonenumber() &lt;&lt; endl;
              break;
           }
           else
           {
               parent = curr;
               if(key&gt;curr-&gt;data.getName()) curr = curr-&gt;right;
               else curr = curr-&gt;left;
           }
      }
      if(!found)
           {
          cout&lt;&lt;" Data not found! "&lt;&lt;endl;
          return;
      }
  }
  void BinarySearchTree::changePhonenumber(string p, int newnumber) {
      bool found = false;
      if(isEmpty())
      {
          cout&lt;&lt;" This Tree is empty! "&lt;&lt;endl;
          return;
      }
      tree_node* curr;
      tree_node* parent;
      curr = root;
      while(curr != NULL)
      {
           if(curr-&gt;data.getName() == p)
           {
              found = true;
              break;
           }
           else
           {
               parent = curr;
               if(p&gt;curr-&gt;data.getName()) curr = curr-&gt;right;
               else curr = curr-&gt;left;
           }
      }
      if(!found)
           {
          cout&lt;&lt;" Person not found. "&lt;&lt;endl;
          return;
      }
      //change the phonenumber associated with the node
      curr-&gt;data.setPhonenumber(newnumber);
      cout&lt;&lt; "Number changed successfully. " &lt;&lt; endl;
  }//end changePhonenumber
  /////////////////////////////////////new add into book class//////////////
  //void fillTree( BinarySearchTree b)
  void fillTree( BinarySearchTree *b)//Line 368
  {
      ifstream file;
      file.open("phonebook.txt");
      if(!file) {
          cout&lt;&lt;" Error opening file. " &lt;&lt; endl;
      }
      //variables used to load data into the tree
      string name;
      int phonenumber;
      string email;
      Person p;
      while(file &gt;&gt; name &gt;&gt; phonenumber &gt;&gt; email)
      {
          p.setName(name);
          p.setPhonenumber(phonenumber);
          p.setEmail(email);
          cout &lt;&lt; p.getName() &lt;&lt; "  " &lt;&lt; p.getPhonenumber() &lt;&lt; "  " &lt;&lt;p.getEmail()&lt;&lt; endl;
          //b.insert(p);
          (*b).insert(p); //Line 384
      }
      file.close();
  }
  int main()
  {
      BinarySearchTree b;
      int ch;
      string name;
      string key;
      int phonenumber;
      string email;
      Person tmp;
      Person tmp1;
  
      fillTree(&amp;b); // Line 398//print the contents of phonebook.txt 
      while(1)
      {
         cout&lt;&lt;endl&lt;&lt;endl;
         cout&lt;&lt;" Binary Search Tree Operations "&lt;&lt;endl;
         cout&lt;&lt;" ----------------------------- "&lt;&lt;endl;
         cout&lt;&lt;" 0. Search             "&lt;&lt;endl;
         cout&lt;&lt;" 1. Insertion/Creation "&lt;&lt;endl;
         cout&lt;&lt;" 2. All Contacts Information "&lt;&lt;endl;
         cout&lt;&lt;" 3. Removal "&lt;&lt;endl;
         cout&lt;&lt;" 4. Change a Phonenumber "&lt;&lt;endl;
         cout&lt;&lt;" 5. Exit "&lt;&lt;endl;
         cout&lt;&lt;" Enter your choice : ";
         cin&gt;&gt;ch;
         switch(ch)
         {
             case 0 : cout &lt;&lt;" Enter the name of the person to search for: "&lt;&lt;endl;
                      cin&gt;&gt;key;
                      b.search(key);
                      break;
             case 1 : cout&lt;&lt;" Enter name to be inserted: ";
                      cin&gt;&gt;name;
                      cout &lt;&lt; endl &lt;&lt; " Enter phone number: " &lt;&lt; endl;
                      cin &gt;&gt; phonenumber;
                      cout&lt;&lt;" Enter email: ";
                      cin&gt;&gt;email;
                      tmp.setName(name);
                      tmp.setPhonenumber(phonenumber);
                      tmp.setEmail(email);
                      b.insert(tmp);
                      break;
             case 2 : cout&lt;&lt;endl;
                      cout&lt;&lt;" All Contacts Information "&lt;&lt;endl;
                      cout&lt;&lt;" ------------------------"&lt;&lt;endl;
                      cout&lt;&lt;"  NAME\t\tPHONE\t\tEMAIL"&lt;&lt;endl;
                      b.print_inorder();
                      break;
             case 3 : cout&lt;&lt;" Enter data to be deleted : ";
                      //cout &lt;&lt; "Deletion needs to be implemented." &lt;&lt; endl;
                      cin&gt;&gt;key;
                      b.remove(key);
                      break;
             case 4 : cout&lt;&lt;" Enter the name of the person whose number you wish to change: " &lt;&lt;endl;
                      cin&gt;&gt;name;
                      cout&lt;&lt;endl&lt;&lt;" Enter the new phonenumber: " &lt;&lt;endl;
                      cin&gt;&gt;phonenumber;
                      cout&lt;&lt;endl;
                      b.changePhonenumber(name, phonenumber);
                      break;
             case 5 : return 0;
  
         }
      }
  } 
  </code></pre>