現在、バイナリ検索ツリーを設定しており、テンプレートを使用して、バイナリ検索ツリー内のデータの種類を簡単に変更できます。現在、ツリーに格納されるデータを含むstudentRecordクラスのオーバーロードに問題があります。BSTがコンテンツの1つ(この場合は学生ID)に基づいて2つのオブジェクトを適切に比較できるように、このクラス内の比較演算子をオーバーロードする必要があります。ただし、studentRecord内の演算子がオーバーロードされているにもかかわらず、適切な比較はまだ行われていません。
以下の詳細:
現在、タイプのbstオブジェクトstudentTreeが作成されています
bst<studentRecord *> studentTree;
studentRecordは次のクラスです。
// 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;
};
新しいアイテムが私のBSTに追加されるときはいつでも、それらを互いに比較する必要があります。したがって、studentRecordクラスをオーバーロードして、この比較プロセスが発生したときに、studentIDが比較されるようにしました(そうでない場合は、無効な比較が行われます)。
ただし、挿入関数がオーバーロードされた比較関数を使用することはありません。代わりに、2つのオブジェクトを別の方法で比較しているようで、BST内で無効な並べ替えが発生します。私の挿入関数の一部を以下に示します。テンプレート処理が行われるため、toInsertとnodePtr->dataの両方がstudentRecord型である必要があることに注意してください。
// 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);
}
また、ここにBSTクラス定義の一部があります
// 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.....
これを引き起こしている可能性があるものについてのアイデアはありますか?間違ったクラスまたは間違った関数をオーバーロードしていますか?どんな助けでもありがたいです-非常に多くの異なることが一度に起こっているので、私は道に迷っているようです。