I currently have a binary search tree setup, utilizing templates to allow me to easily change the type of data within the binary search tree. At the moment, I’m having trouble overloading the studentRecord class which contains the data to be stored in the tree. I need to overload the comparison operators within this class, so that my BST can properly compare two objects based on one of their contents (in this case, the student ID). However, despite overloading the operators within studentRecord, proper comparisons are still not occurring.
Details below:
At the moment, the bst object studentTree has been created, of type
bst<studentRecord *> studentTree;
studentRecord is the following class:
// studentRecord class
class studentRecord{
public:
// standard constructors and destructors
studentRecord(int studentID, string lastName, string firstName, string academicYear){ // constructor
this->studentID=studentID;
this->lastName=lastName;
this->firstName=firstName;
this->academicYear=academicYear;
}
friend bool operator > (studentRecord &record1, studentRecord &record2){
if (record1.studentID > record2.studentID)
cout << "Greater!" << endl;
else
cout << "Less then!" << endl;
return (record1.studentID > record2.studentID);
}
private:
// student information
string studentID;
string lastName;
string firstName;
string academicYear;
};
Whenever new items are added to my BST, they must be compared with each other. Hence, I wanted to overload the studentRecord class, so that when this comparison process occurs, the studentIDs are compared (as otherwise, an invalid comparison will be made).
However, my insertion function never uses my overloaded comparison functions. Instead, it seems to be comparing the two objects some other way, resulting in invalid sorting within the BST. Part of my insert function is below — it is important to note that both toInsert and nodePtr->data should be of type studentRecord, due to the templating process occuring.
// insert (private recursive function)
template<typename bstType>
void bst<bstType>::insert(bstType & toInsert, bstNodePtr & nodePtr){
// check to see if the nodePtr is null, if it is, we've found our insertion point (base case)
if (nodePtr == NULL){
nodePtr = new bst<bstType>::bstNode(toInsert);
}
// else, we are going to need to keep searching (recursive case)
// we perform this operation recursively, to allow for rotations (if AVL tree support is enabled)
// check for left
else if (toInsert < (nodePtr->data)){ // go to the left (item is smaller)
// perform recursive insert
insert(toInsert,nodePtr->left);
// AVL tree sorting
if(getNodeHeight(nodePtr->left) - getNodeHeight(nodePtr->right) == 2 && AVLEnabled)
if (toInsert < nodePtr->left->data)
rotateWithLeftChild(nodePtr);
else
doubleRotateWithLeftChild(nodePtr);
}
Also, here is a portion of the BST class defintion
// BST class w/ templates
template <typename bstType>
class bst{
private: // private data members
// BST node structure (inline class)
class bstNode{
public: // public components in bstNode
// data members
bstType data;
bstNode* left;
bstNode* right;
// balancing information
int height;
// constructor
bstNode(bstType item){
left = NULL;
right = NULL;
data = item;
height = 0;
}
// destructor
// no special destructor is required for bstNode
};
// BST node pointer
typedef bstNode* bstNodePtr;
public: // public functions.....
Any ideas on what may be causing this? Am I overloading the wrong class or the wrong function? Any help is appreciated — I seem to be getting lost since so many different things are occurring at once.
Your tree is a tree of pointers. So when you try to insert an element into the tree the values of the pointers is compared. So your overloaded operator is not called. If you want to use the overloaded operator then you should create
bst<studentrecord>