简体   繁体   中英

Inserting objects into binary search tree - C++

I'm pretty new to binary search trees. I've created a template class for a binary search tree so that it can handle class objects. Now I'm trying to test it in main, and I'm reading in dates from a text file into a 'Date' object of class Date, using a while loop until the end of the file. After the first iteration, I can see that my first date has successfully been entered into the tree when I check through the CodeBlocks debugger. Now, when going through the second iteration, the program reads and sets the dates successfully to the Date Object, outputs it with my cout statements, and then pauses for a bit and crashes.

At the instance where it pauses or crashes, in my debugger, I can see that it is endlessly calling my function overload 'bool operator==(const Date& firstDate, const Date& secondDate)' which I use to compare whether the dates are equal and no matter how many times I try to step into the next line it wont let me, it is just stuck on this line. So I imagine something is wrong with my operator overload?

Function overload where it gets stuck:

bool operator==(const Date& firstDate, const Date& secondDate)
{
    if(firstDate==secondDate) // The debugger is pointing to this line
    {
        return true;
    }
    return false;
}

Main:

ifstream infile("date.txt");

        string date1;

        Bst<Date> dateTree;

        while(getline(infile, date1))
        {
            Date dateObj;
            stringstream date(date1);
            string day;
            string month;
            string year;

            getline(date,day,'/');
            getline(date,month,'/');
            getline(date,year, ' ');
            cout << day << endl;
            cout << month << endl;
            cout << year << endl;

            dateObj.SetDay(day);
            dateObj.SetMonth(month);
            dateObj.SetYear(year);

            dateTree.insertNode(dateObj);
        }

        dateTree.inOrderTraversal();

        return 0;
}

This is my node insertion code:

template <class T>
void Bst<T>::insertNode(const T& insertItem)
{
    nodeType<T> *current;
    nodeType<T> *trailCurrent = nullptr;
    nodeType<T> *newNode;

    newNode = new nodeType<T>;
    newNode->info = insertItem;
    newNode->lLink = nullptr;
    newNode->rLink = nullptr;

    if(root == nullptr)
        root = newNode;
    else
    {
        current = root;
        while(current != nullptr)
        {
            trailCurrent = current;
            if(current->info == insertItem)
            {
                cout << "The item to be inserted is already ";
                cout << "in the tree -- duplicates are not "
                << "allowed." << endl;
                return;
            }
            else if(current->info > insertItem)
                current = current->lLink;
            else
                current = current->rLink;
        }
        if(trailCurrent->info > insertItem)
            trailCurrent->lLink = newNode;
        else
            trailCurrent->rLink = newNode;

    }
}

The test firstDate==secondDate compiles to:

operator==(firstDate, secondDate)

which calls your bool operator==(const Date& firstDate, const Date& secondDate) ad infinitum. You need to compare the components of the Date object. I don't know how it is defined, but maybe something like the following:

bool operator==(const Date& firstDate, const Date& secondDate) {
    return std::tie(firstDate.year, firstDate.month, firstDate.day) == 
           std::tie(secondDate.year, secondDate.month, secondDate.day);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM