title: BinaryTree Employee Class
body: <p>First create a class called EmployeeInfo that holds two private data members. One data member is an integer called empID which holds the id number of the employee. The second data member is a string called empName which holds the full name of the employee. The program will create an instance of the binary tree class with a data type of EmployeeInfo (BinaryTree). The binary tree will be sorted by the Employee ID number found in the EmployeeInfo class. The program should then allow the user to search for Employee by the Employee ID. If the employee is found in the tree, its name and ID should be displayed. If not, a message should be displayed indicating that it was not found.</p>
  
  <p>I'm having a problem displaying the employee number and name. I get a long list of error codes that I can't figure out. Please point me in the right direction. Above is what I'm trying to accomplish.</p>
  
  <pre><code>#ifndef BINARYTREE_H
  #define BINARYTREE_H
  #include &lt;iostream&gt;
  using namespace std;
  
  // This class is a template class that creates a binary
  // tree that can hold values of any data type. It has 
  // functions to insert a node, delete a node, display the
  // tree In Order, Pre Order and Post Order, search for a 
  // value, count the number of total nodes, left nodes, 
  // and a function to determine the height of the tree.
  
  template &lt;class T&gt;
  class BinaryTree
  {
  private:
      struct TreeNode
      {
          T value;            // The value in the node
          TreeNode *left;     // Pointer to left child node
          TreeNode *right;    // Pointer to right child node
      };
  
      TreeNode *root;         // Pointer to the root node
  
      // Private member functions
      void insert(TreeNode *&amp;, TreeNode *&amp;);
      void destroySubTree(TreeNode *);
      void deleteNode(T, TreeNode *&amp;);
      void makeDeletion(TreeNode *&amp;);
      void displayInOrder(TreeNode *) const;
      void displayPreOrder(TreeNode *) const;
      void displayPostOrder(TreeNode *) const;
      int counter(TreeNode *);
      int leafCounter(TreeNode *);
      int height(TreeNode *);
  
  public:
      // Constructor
      BinaryTree()
      { root = NULL; }
  
      // Destructor
      ~BinaryTree()
      { destroySubTree(root); }
  
      // Binary tree operations
      void insertNode(T);
      bool searchNode(T);
      void remove(T);
  
      void displayPreOrder() const
      { displayPreOrder(root); }
  
      void displayInOrder() const
      { displayInOrder(root); }
  
      void displayPostOrder() const
      { displayPostOrder(root); }
  
      // Node counter
      int counter()
      { 
          int n = counter(root);
          return n;
      }
  
      // Leaf counter
      int leafCounter()
      {
          int leaf = leafCounter(root);
          return leaf;
      }
  
      // Height of the tree
      int height()
      {
          int h = height(root);
          return h;
      }
  };
  
  //*********************************************************
  // insert function accepts a TreeNode pointer and a       *
  // pointer to a node. The function inserts the node into  *
  // the tree pointer to by the TreeNode pointer. This      *
  // function is call recursively.                          *
  //*********************************************************
  template &lt;class T&gt;
  void BinaryTree&lt;T&gt;::insert(TreeNode *&amp;nodePtr, TreeNode *&amp;newNode)
  {
      if (nodePtr == NULL)
          nodePtr = newNode;              // Insert the node
      else if (newNode-&gt;value &lt; nodePtr-&gt;value)
          insert(nodePtr-&gt;left, newNode); // Search the left branch
      else
          insert(nodePtr-&gt;right, newNode);// Search the right branch
  }
  
  //*********************************************************
  // insertNode creates anew node to hold num as its value  *
  // and passes it to the insert function.                  *
  //*********************************************************
  template &lt;class T&gt;
   void BinaryTree&lt;T&gt;::insertNode(T item)
   {
       TreeNode *newNode;     // Pointer to a new node
  
       // Create anew node and store num in it
       newNode = new TreeNode;
       newNode-&gt;value = item;
       newNode-&gt;left = newNode-&gt;right = NULL;
  
       // Insert the node
       insert(root, newNode);
   }
  
  //**********************************************************
  // destroySubTree is called by the destructor. It deletes  *
  // all nodes in the tree.                                  *
  //**********************************************************
  template &lt;class T&gt;
  void BinaryTree&lt;T&gt;::destroySubTree(TreeNode *nodePtr)
  {
       if (nodePtr)
       {
           if (nodePtr-&gt;left)
               destroySubTree(nodePtr-&gt;left);
           if (nodePtr-&gt;right)
               destroySubTree(nodePtr-&gt;right);
           delete nodePtr;
       }
  }
  
  //**********************************************************
  // searchNode determines if a value is present in the tree.*
  // If so, the function returns true. Otherwise it returns  *
  // false.
  //**********************************************************
  template &lt;class T&gt;
  bool BinaryTree&lt;T&gt;::searchNode(T item)
  {
      TreeNode *nodePtr = root;
  
      while (nodePtr)
      {
          if (nodePtr-&gt;value == item)
              return true;
          else if (item &lt; nodePtr-&gt;value)
              nodePtr = nodePtr-&gt;left;
          else
              nodePtr = nodePtr-&gt;right;
      }
      return false;
  }
  
  //*********************************************************
  // remove calls deleteNode to delete the node whode value *
  // member is the same as num                              *
  //*********************************************************
  template &lt;class T&gt;
  void BinaryTree&lt;T&gt;::remove(T item)
  {
      deleteNode(item, root);
  }
  
  //*********************************************************
  // deleteNode deletes the node whose value member is the  *
  // same as num                                            *
  //*********************************************************
  template &lt;class T&gt;
  void BinaryTree&lt;T&gt;::deleteNode(T item, TreeNode *&amp;nodePtr)
  {
      if (item &lt; nodePtr-&gt;value)
          deleteNode(item, nodePtr-&gt;left);
      else if (item &gt; nodePtr-&gt;value)
          deleteNode(item, nodePtr-&gt;right);
      else
          makeDeletion(nodePtr);
  }
  
  //*********************************************************
  // makeDeletion takes a reference to apointer to the node *
  // that is to be deleted. The node is removed and the     *
  // branches of the tree below the node are reattached     *
  //*********************************************************
  template &lt;class T&gt;
  void BinaryTree&lt;T&gt;::makeDeletion(TreeNode *&amp;nodePtr)
  {
      // Define a temporary pointer to use in reattaching
      // the left subtree
      TreeNode *tempNodePtr;
  
      if (nodePtr == NULL)
          cout &lt;&lt; "Cannot delete empty node.\n";
      else if (nodePtr-&gt;right == NULL)
      {
          tempNodePtr = nodePtr;
          nodePtr = nodePtr-&gt;left;    // Reattach the left child
          delete tempNodePtr;
      }
      else if (nodePtr-&gt;left == NULL)
      {
          tempNodePtr = nodePtr;
          nodePtr = nodePtr-&gt;right;   // Reattach the right child
          delete tempNodePtr;
      } 
  
  }
  //*********************************************************
  // The displayInOrder function displays the values in the *
  // subtree pointed to by nodePtr, via inorder traversal   *
  //*********************************************************
  template &lt;class T&gt;
  void BinaryTree&lt;T&gt;::displayInOrder(TreeNode *nodePtr) const
  {
      if (nodePtr)
      {
          displayInOrder(nodePtr-&gt;left);
          cout &lt;&lt; nodePtr-&gt;value.getEmpID() &lt;&lt; " "
                       &lt;&lt; getEmpName() &lt;&lt; endl;
          displayInOrder(nodePtr-&gt;right);
      }
  }
  //*********************************************************
  // The displayPreOrder function displays the values in the*
  // subtree pointed to by nodePtr, via Preorder traversal  *
  //*********************************************************
  template &lt;class T&gt;
  void BinaryTree&lt;T&gt;::displayPreOrder(TreeNode *nodePtr) const
  {
      if (nodePtr)
      {
          cout &lt;&lt; nodePtr-&gt;value &lt;&lt; endl;
          displayInOrder(nodePtr-&gt;left);
          displayInOrder(nodePtr-&gt;right);
      }
  }
  //*********************************************************
  // displayPostOrder function displays the values in the   *
  // subtree pointed to by nodePtr, via Postorder traversal *
  //*********************************************************
  template &lt;class T&gt;
  void BinaryTree&lt;T&gt;::displayPostOrder(TreeNode *nodePtr) const
  {
      if (nodePtr)
      {
          displayInOrder(nodePtr-&gt;left);
          displayInOrder(nodePtr-&gt;right);
          cout &lt;&lt; nodePtr-&gt;value &lt;&lt; endl;
      }
  }
  
  //*********************************************************
  // counter counts the number of nodes the tree has        *
  //*********************************************************
  template &lt;class T&gt;
  int BinaryTree&lt;T&gt;::counter(TreeNode *nodePtr)
  {
      if (nodePtr == NULL)
          return 0;
      else
          return counter(nodePtr-&gt;left) +1+ counter(nodePtr-&gt;right);
  }
  
  //*********************************************************
  // leafCounter counts the number of leaf nodes in the tree*
  //*********************************************************
  template &lt;class T&gt;
  int BinaryTree&lt;T&gt;::leafCounter(TreeNode *nodePtr)
  {
      if (nodePtr == NULL)
          return 0;
      else if (nodePtr-&gt;left == NULL &amp;&amp; nodePtr-&gt;right == NULL)
          return 1;
      else 
          return leafCounter(nodePtr-&gt;left) + leafCounter(nodePtr-&gt;right);
  }
  
  //*********************************************************
  // height returns the height of the tree                  *
  //*********************************************************
  template &lt;class T&gt;
  int BinaryTree&lt;T&gt;::height(TreeNode *nodePtr)
  {
  
      if(nodePtr == NULL)
          return -1;
      if (height(nodePtr-&gt;left) &lt;= height(nodePtr-&gt;right))
          return (height(nodePtr-&gt;right) +1);
      else
          return (height(nodePtr-&gt;left) +1);
  
  }
  #endif
  </code></pre>
  
  <p>My EMPLOYEE CLASS </p>
  
  <pre><code>#ifndef EMPLOYEEINFO_H
  #define EMPLOYEEINFO_H
  #include &lt;string&gt;
  using namespace std;
  
  // This class has two data members to hold the employee ID
  // and the name of the employee.
  
  class EmployeeInfo
  {
  private:
      int empID;      // To hold employee ID number
      string empName; // To hold employee name
  
  public:
      // Default Constructor
      EmployeeInfo();
  
      // Constructor
      EmployeeInfo(int, string);
  
      // Mutators
      void setEmpID(int);
      void setEmpName(string);
  
      // Accessors
      int getEmpID();
      string getEmpName();
  
      bool operator &lt; (const EmployeeInfo &amp;e)
          {
          if (empID &lt; e.empID)
              return true;
          if (empName &lt; e.empName)
              return true;
          return false;
          }
  };
  #endif
  </code></pre>
  
  <p><strong>EDIT</strong> I overloaded the <code>&lt;</code> operator and all the errors went away</p>
  
  <pre><code>#include "EmployeeInfo.h"
  
  //*********************************************************
  // Default constructor intializes the data members        *
  //*********************************************************
  EmployeeInfo::EmployeeInfo()
  {
      empID = 0;
      empName = "";
  }
  
  //*********************************************************
  // Constructor sets the data members                      *
  //*********************************************************
  EmployeeInfo::EmployeeInfo(int ID, string name)
  {
      empID = ID;
      empName = name;
  }
  
  //*********************************************************
  // setEmpID stores the employee ID number                 *
  //*********************************************************
  void EmployeeInfo::setEmpID(int ID)
  {
      empID = ID;
  }
  
  //*********************************************************
  // setEmpName stores the full name of the employee        *
  //*********************************************************
  void EmployeeInfo::setEmpName(string name)
  {
      empName = name;
  }
  
  //*********************************************************
  // getEmpID returns the employee ID number                *
  //*********************************************************
  int EmployeeInfo::getEmpID()
  {
      return empID;
  }
  
  //*********************************************************
  // getEmpName returns the full name of the employee       *
  //*********************************************************
  string EmployeeInfo::getEmpName()
  {
      return empName;
  }
  </code></pre>
  
  <p>MAIN</p>
  
  <pre><code>#include "EmployeeInfo.h"
  #include "BinaryTree.h"
  #include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
  
      // Create an instance of BinaryTree
      BinaryTree&lt;EmployeeInfo&gt; tree;
  
      // Create an EmployeeInfo object
      EmployeeInfo info;
      EmployeeInfo emp1(1021, "John Williams");
      EmployeeInfo emp2(1057, "Bill Witherspoon");
  
      // Store the information
      info.setEmpID(1021);
      info.setEmpID(1057);
  
      info.setEmpName("John Williams");
      info.setEmpName("Bill Witherspoon");
  
      tree.insertNode(emp1);
      tree.insertNode(emp2);
  
      // Display in order
      tree.displayInOrder();
  }
  </code></pre>