0

我目前有一个二叉搜索树设置,利用模板让我可以轻松地更改二叉搜索树中的数据类型。目前,我无法重载包含要存储在树中的数据的 studentRecord 类。我需要重载此类中的比较运算符,以便我的 BST 可以根据其中一个内容(在本例中为学生 ID)正确比较两个对象。但是,尽管重载了 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(否则将进行无效比较)。

但是,我的插入函数从不使用我的重载比较函数。相反,它似乎是以其他方式比较这两个对象,导致 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.....

关于可能导致这种情况的任何想法?我是否重载了错误的类或错误的函数?感谢您提供任何帮助-我似乎迷路了,因为同时发生了许多不同的事情。

4

2 回答 2

2

你像这样实例化你的模板类:

bst<studentRecord *> studentTree;

所以bstType == studentRecord*

插入看起来像这样:

template<studentRecord*>
void bst<studentRecord*>::insert(studentRecord*& toInsert, bst<studentRecord*>::bstNodePtr & nodePtr);

所以你正在做一个指针比较,这就是为什么你的运营商没有像 Asha 指出的那样被称为。

更重要的是,您只重载大于运算符 (>),但在插入时使用小于运算符 (<)。如果您真的要在 insert 中比较两个 studentRecord 类型的对象,则代码甚至不应该编译,并且应该抱怨它无法找到合适的小于运算符。

更重要的是,我可以指出您的代码中的几个问题:

  1. studentRecord.studentID 是字符串类型吗?但是,您尝试在构造函数中为其分配一个整数。这将简单地将整数转换为 char 并将字符分配给字符串 - 所以很可能不是您想要的。
  2. 您缺少小于运算符。

下面的代码和一些演示操作符的代码在比较两个学生记录类型的实例时被调用。您还可以通过注释 studentRecord 类中的运算符定义(-> 编译错误)来查看缺少小于运算符的影响。

class studentRecord
{
public:

    studentRecord(int studentID) : studentID(studentID)
    { 
    }

    bool operator > (studentRecord &record)
    {
        return (studentID > record.studentID);
    }

    /* Uncomment to get rid of the compile error!
    bool operator < (studentRecord &record)
    {
        return studentID < record.studentID;
    }
    */

private:
    // student information
    int studentID;
};

int main()
{
    studentRecord r1(10);
    studentRecord r2(5);

    if ( r1 < r2 )
    {
        cout << "It works! " << "r1 less than r2" << endl;
    }
    else
    {
        cout << "It works! " << "r1 greater than r2" << endl;
    }

    if ( r1 > r2 )
    {
        cout << "It works! " << "r1 greater than r2" << endl;
    }
    else
    {
        cout << "It works! " << "r1 less than r2" << endl;
    }
}

作为结束注释,最好也提供其他比较运算符(>=、<=、== 和 !=。

于 2011-02-23T09:29:17.050 回答
1

你的树是指针树。因此,当您尝试将元素插入树时,会比较指针的值。所以你的重载运算符不会被调用。如果要使用重载运算符,则应创建bst<studentrecord>

于 2011-02-23T06:14:14.827 回答