Question: C++ programming. The program does not find the EmployeeId, please help me fix the error. In this program you are going to use the binary

C++ programming. The program does not find the EmployeeId, please help me fix the error.

In this program you are going to use the binary tree class you created in Assignment 1 this week. 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. Sample data would be

1021 John Williams

1057 Bill Witherspoon

2487 Jennifer Twain

3769 Sophia Lancaster

1017 Debbie Reece

1275 George McMullen

1899 Ashley Smith

4218 Josh Plemmons

//BinaryTree.h

#ifndef BINARYTREE_H

#define BINARYTREE_H

#include

using namespace std;

template

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 *&, TreeNode *&);

void destroySubTree(TreeNode *);

void deleteNode(T, TreeNode *&);

void makeDeletion(TreeNode *&);

void displayInOrder(TreeNode *) const;

void displayPreOrder(TreeNode *) const;

void displayPostOrder(TreeNode *) const;

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

void BinaryTree::insert(TreeNode *&nodePtr, TreeNode *&newNode)

{

if (nodePtr == NULL)

nodePtr = newNode; // Insert the node

else if (newNode->value < nodePtr->value)

insert(nodePtr->left, newNode); // Search the left branch

else

insert(nodePtr->right, newNode);// Search the right branch

}

//*********************************************************

// insertNode creates anew node to hold num 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 < nodePtr->value)

nodePtr = nodePtr->left;

else

nodePtr = nodePtr->right;

}

return false;

}

//*********************************************************

// remove calls deleteNode to delete the node whode value *

// member is the same as num *

//*********************************************************

template

void BinaryTree::remove(T item)

{

deleteNode(item, root);

}

//*********************************************************

// deleteNode deletes the node whose value member is the *

// same as num *

//*********************************************************

template

void BinaryTree::deleteNode(T item, TreeNode *&nodePtr)

{

if (item < nodePtr->value)

deleteNode(item, nodePtr->left);

else if (item > nodePtr->value)

deleteNode(item, nodePtr->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

void BinaryTree::makeDeletion(TreeNode *&nodePtr)

{

// Define a temporary pointer to use in reattaching

// the left subtree

TreeNode *tempNodePtr;

if (nodePtr == NULL)

cout << "Cannot delete empty node. ";

else if (nodePtr->right == NULL)

{

tempNodePtr = nodePtr;

nodePtr = nodePtr->left; // Reattach the left child

delete tempNodePtr;

}

else if (nodePtr->left == NULL)

{

tempNodePtr = nodePtr;

nodePtr = nodePtr->right; // Reattach the right child

delete tempNodePtr;

}

}

//*********************************************************

// The displayInOrder function displays the values in the *

// subtree pointed to by nodePtr, via inorder traversal *

//*********************************************************

template

void BinaryTree::displayInOrder(TreeNode *nodePtr) const

{

if (nodePtr)

{

displayInOrder(nodePtr->left);

cout << nodePtr->value.getEmpID() << " "

<< nodePtr->value.getEmpName() << endl;

displayInOrder(nodePtr->right);

}

}

//*********************************************************

// The displayPreOrder function displays the values in the*

// subtree pointed to by nodePtr, via Preorder traversal *

//*********************************************************

template

void BinaryTree::displayPreOrder(TreeNode *nodePtr) const

{

if (nodePtr)

{

cout << nodePtr->value << endl;

displayInOrder(nodePtr->left);

displayInOrder(nodePtr->right);

}

}

//*********************************************************

// displayPostOrder function displays the values in the *

// subtree pointed to by nodePtr, via Postorder traversal *

//*********************************************************

template

void BinaryTree::displayPostOrder(TreeNode *nodePtr) const

{

if (nodePtr)

{

displayInOrder(nodePtr->left);

displayInOrder(nodePtr->right);

cout << nodePtr->value << endl;

}

}

#endif

//EmployeeInfo.h

#pragma once

#ifndef EMPLOYEEINFO_H

#define EMPLOYEEINFO_H

#include "BinaryTree.h"

#include

#include

using namespace std;

class EmployeeInfo

{

friend class BinaryTree;

friend ostream &operator<<(ostream &out, const EmployeeInfo &obj);

private:

int empID;

string empName;

public:

EmployeeInfo();

EmployeeInfo(int id, string name);

int getEmpID();

string getEmpName();

bool operator==(int value);

bool operator==(const EmployeeInfo& emp);

bool operator<(const EmployeeInfo& emp);

bool operator>(const EmployeeInfo& emp);

};

EmployeeInfo::EmployeeInfo()

{

empID = 0;

empName = "";

}

EmployeeInfo::EmployeeInfo(int id, string name)

{

empID = id;

empName = name;

}

int EmployeeInfo::getEmpID()

{

return empID;

}

string EmployeeInfo::getEmpName()

{

return empName;

}

bool EmployeeInfo::operator==(int value)

{

return this->getEmpID() == value;

}

bool EmployeeInfo::operator==(const EmployeeInfo& emp)

{

return this->empID == emp.empID;

}

bool EmployeeInfo::operator<(const EmployeeInfo& emp)

{

return this->empID < emp.empID;

}

bool EmployeeInfo::operator>(const EmployeeInfo& emp)

{

return this->empID > emp.empID;

}

ostream &operator<<(ostream &out, const EmployeeInfo &obj)

{

out << "EmpID: " << obj.empID << ", EmpName: " << obj.empName << endl;

return out;

}

#endif

//Main.cpp

#include "EmployeeInfo.h"

#include "BinaryTree.h"

#include

#include

#include

using namespace std;

int main()

{

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");

BinaryTree tree;

tree.insertNode(emp1);

tree.insertNode(emp2);

tree.insertNode(emp3);

tree.insertNode(emp4);

tree.insertNode(emp5);

tree.insertNode(emp6);

tree.insertNode(emp7);

tree.insertNode(emp8);

tree.displayInOrder();

cout << endl;

char again = 'y';

int id;

cout << "would you like to search for an employee? (Y/N): ";

cin >> again;

if (again)

{

do

{

cout << "enter the ID number: ";

cin >> id;

EmployeeInfo info;

info.getEmpID();

if (tree.searchNode(info))

{

cout << info.getEmpID() << endl;

}

else

cout << "Not found" << endl;

cout << "Would you like to continue? (Y/N): ";

cin >> again;

} while (again == tolower('Y'));

}

system("pause");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!