title: Passing a pointer by reference (insert into binary search tree)
body: <p>My program is calling a member function that performs an insertion into a binary search tree with a pointer passed by reference (so that the value of the pointer is changed).  However when I attempt to insert a second node into the tree the program crashes.  I am assuming that this has to do with a bad pointer.  </p>
  
  <p>The only part of this assignment that can be changed is the implementation of the member functions and not there declaration.</p>
  
  <p>Any help would be appreciated.</p>
  
  <pre><code> //Header file
  using namespace std;
  
  struct PersonRec
  {
      char name[20];
      int bribe;
      PersonRec* leftLink;
      PersonRec* rightLink;
  };
  
  
  class CTree
  {
  
  private:
      PersonRec *tree;
      bool IsEmpty();
      void AddItem( PersonRec*&amp;, PersonRec*);
      void DisplayTree(PersonRec*);
  
  public:
      CTree();
      //~CTree();
      void Add();
      void View();
  
  };
  
  //Implementation file
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  #include "ctree.h"
  
  CTree::CTree()
  {
      tree = NULL;
  }
  
  //PersonList::~MyTree()
  //{
  //
  //}
  
  
  bool CTree::IsEmpty()
  {
      if(tree == NULL) 
      {
          return true;
      }
      else
      {
          return false;
      }
  }
  
  void CTree::Add()
  {
      PersonRec* newPerson = new PersonRec();
      cout &lt;&lt; "Enter the person's name: ";
      std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
      cin.getline(newPerson-&gt;name, 20);
      cout &lt;&lt; "Enter the person's contribution: ";
      cin &gt;&gt; newPerson-&gt;bribe;
  
  
      newPerson-&gt;leftLink = NULL;
      newPerson-&gt;rightLink = NULL;
  
      AddItem(tree, newPerson);
  }
  
  void CTree::View()
  {
      if (IsEmpty())
      {
          cout&lt;&lt;"The list is empy";
      }
      else
      {
          DisplayTree(tree);
  
      }
  
  };
  
  void CTree::AddItem( PersonRec*&amp; ptr, PersonRec* newPer )
  {
          if (tree == NULL)
          {
              ptr = newPer;
          }
          else if ( newPer-&gt;bribe &lt; ptr-&gt;bribe)
              AddItem(ptr-&gt;leftLink, newPer); 
          else
              AddItem(ptr-&gt;rightLink, newPer); 
  }
  void CTree::DisplayTree(PersonRec* ptr)
  {
      if (ptr == NULL)
                      return;
      DisplayTree(ptr-&gt;rightLink);
      cout&lt;&lt;ptr-&gt;name&lt;&lt;" "&lt;&lt;"$"&lt;&lt;ptr-&gt;bribe &lt;&lt;endl;
      DisplayTree(ptr-&gt;leftLink); 
  }
  
  
  //Driver file
  #include &lt;iostream&gt;
  
  using namespace std;
  #include &lt;cstdlib&gt;
  #include "ctree.h"
  
  int displayMenu (void);
  void processChoice(int, CTree&amp;);
  
  int main (void)
  {
  int num;
  CTree ct;
  do 
  {
  num = displayMenu();
  if (num != 3)
  processChoice(num, ct);
  } while (num != 3);
  return 0;
  }
  
  int displayMenu (void)
  {
  int choice;
  cout &lt;&lt; "\nMenu\n";
  cout &lt;&lt; "==============================\n\n";
  cout &lt;&lt; "1. Add student to waiting list\n";
  cout &lt;&lt; "2. View waiting list\n";
  cout &lt;&lt; "3. Exit program\n\n";
  cout &lt;&lt; "Please enter choice: ";
  cin &gt;&gt; choice;
  return choice;
  }
  
  void processChoice(int choice, CTree&amp; myTree)
  {
     switch (choice)
     {
        case 1: myTree.Add (); break;
        case 2: myTree.View (); break;
     } 
  }
  </code></pre>