Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Binary Tree Template Write your own version of a class template that will cre

ID: 3556000 • Letter: 1

Question

1. Binary Tree Template

Write your own version of a class template that will create a binary tree that can hold

values of any data type. Demonstrate the class with a driver program. C++

Explanation / Answer

#ifndef EMPLOYEEINFO_H #define EMPLOYEEINFO_H #include #include 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(); // Overloaded operator. This works directly with // the insert function of BinaryTree.h more specifically // line 71. *For my knowledge* bool operator < (const EmployeeInfo &e) { if (empID value value) insert(nodePtr->left, newNode); // Search the left branch else insert(nodePtr->right, newNode);// Search the right branch } //********************************************************* // insertNode creates a new node to hold item as its value* // and passes it to the insert function. * //********************************************************* template void BinaryTree::insertNode(T item) { TreeNode *newNode; // Pointer to a new node // Create anew node and store num in it newNode = new TreeNode; newNode->value = item; newNode->left = newNode->right = NULL; // Insert the node insert(root, newNode); } //********************************************************** // destroySubTree is called by the destructor. It deletes * // all nodes in the tree. * //********************************************************** template void BinaryTree::destroySubTree(TreeNode *nodePtr) { if (nodePtr) { if (nodePtr->left) destroySubTree(nodePtr->left); if (nodePtr->right) destroySubTree(nodePtr->right); delete nodePtr; } } //********************************************************** // searchNode determines if a value is present in the tree.* // If so, the function returns true. Otherwise it returns * // false. //********************************************************** template bool BinaryTree::searchNode(T item) { TreeNode *nodePtr = root; while (nodePtr) { if (nodePtr->value == item) return true; else if (item value) nodePtr = nodePtr->left; else nodePtr = nodePtr->right; } return false; } #endif #include "EmployeeInfo.h" #include "BinaryTree.h" #include using namespace std; int main() { // Create an instance of BinaryTree BinaryTree tree; // Create an EmployeeInfo object EmployeeInfo emp1(1021, "John Williams"); EmployeeInfo emp2(1057, "Bill Witherspoon"); EmployeeInfo emp3(2487, "Jennifer Twain"); EmployeeInfo emp4(3769, "Sophia Lancaster"); EmployeeInfo emp5(1017, "Debbie Reece"); EmployeeInfo emp6(1275, "George McMullen"); EmployeeInfo emp7(1899, "Ashley Smith"); EmployeeInfo emp8(4218, "Josh Plemmons"); tree.insertNode(emp1); tree.insertNode(emp2); tree.insertNode(emp3); tree.insertNode(emp4); tree.insertNode(emp5); tree.insertNode(emp6); tree.insertNode(emp7); tree.insertNode(emp8); // Search for an employee char again = 'y'; // To hold yes int id; // To hold ID number from user cout again; if (again) { do { cout > id; // Create an EmployeeInfo object to store the user's // search parameters EmployeeInfo info; info.setEmpID(id); // If search finds what the user entered display the // corresponding employee if (tree.searchNode(info)) { cout