I'm writing a binary search tree for school, and part of the assignment is to write a find function. Here's my code:
typedef BSTIterator<Data> iterator;
iterator find(const Data& item) const {
BSTIterator<Data> iter = this->begin();
BSTIterator<Data> end = this->end();
while (iter != end) {
if (*iter == item) {
return iter;
}
iter++;
}
return iter;
}
!= for iterators is overloaded, like so:
bool operator!=(BSTIterator<Data> const & other) {
return (*this).curr != other.curr;
iter
holds a pointer to a node, and !=, *, and ++ are all overloaded. end
's node pointer has a value of 0.
When I run test code through gdb, somewhere in the comparison between iter
and end
, iter
's internal pointer to a node gets set to 0. I have no idea why. On top of that, the find() function works just fine in the first round of testing, where the tree is small (6 items), but when it gets bigger (randomly generated, ~80 items), it fails. Does anyone have any idea why this is happening?