Neither of the employee
constructors initialize either the left or right pointers to NULL. this is particularly worrisome for the copy-constructor, but the parameterized constructor is where the pain will really show through:
When loading from a file you do this:
tree.Insert(new employee(first_name, last_name, ID));
which fires this constructor:
employee(std::string first_name, std::string last_name, unsigned int ID)
{
this->first_name = first_name;
this->last_name = last_name;
this->ID = ID;
}
Nowhere in the above are the left and right member pointers assigned to anything. Thus they are indeterminate and therefore garbage. so when you do this:
bool bstree::Insert(employee *newNode)
{
employee *temp;
employee *back;
temp = root;
back = NULL;
while(temp != NULL) // Loop till temp falls out of the tree
{
back = temp;
if(newNode->ID < temp->ID)
temp = temp->left;
else if (newNode->ID > temp->ID)
temp = temp->right;
else
return false;
}
// Now attach the new node to the node that back points to
if(back == NULL) // Attach as root node in a new tree
root = newNode;
else
{
if(newNode->ID < back->ID)
back->left = newNode;
else if (newNode->ID > back->ID)
back->right = newNode;
else
return false;
}
return true;
}
you're chasing pointers that are not valid, and indeed cannot even be evaluated much less dereferenced without invoking undefined behavior.
This is not going to turn into an online debugging session. You need to properly initialize all members of your object class, ideally in the initializer list:
class employee
{
public:
// note: this shouldn't even be *needed*
employee() : ID(), left(), right() {}
// parameterized constructor
employee(const std::string& first, const std::string& last, unsigned int id)
: first_name(first)
, last_name(last)
, ID(id)
, left(), right()
{
}
// copy-ctor. retains values; child pointers set as null
employee(const employee& obj)
: first_name(obj.first_name)
, last_name(obj.last_name)
, ID(obj.id)
, left(), right()
{
}
// assignment operator. does NOT copy child pointers
employee& operator =(const employee& obj)
{
first_name = obj.first_name;
last_name = obj.last_name;
ID = obj.ID;
}
std::string first_name;
std::string last_name;
unsigned int ID;
employee * left, * right;
};
Normally I would code the assignment operator to use the copy-constructor by implementing the copy/swap idiom. but in this case it would be overkill, since your object has no actual dynamic-managed members ( i.e. members the object itself is actually responsible for creating/destroying).
Anyway, the above is a big issue, and I did NOT take the time to dissect the actual tree management code beyond the insertion logic. I wouldn't be surprised if there were a defect lurking in the Delete operation, which is always tedious for binary trees. But this should be enough to get you further down the road.