c++ - Overloading operator++ to find and return successor node of binary search tree -
this relevant portion of code involving overloading operator++ find , return successor node of binary search tree. i've tested tree itself, , forms correctly, , cout's correctly using inorder traversal function.
but can't life of me figure out i'm doing wrong overloading operator++. i've tried many different versions, , latest one, nextinordersuccessor, returns successor when /not/ called within overloaded operator++
this, along fact i've made multiple version of operator++, lead me believe i'm handling actual overloaded operator wrong. can offer insight?
wcbst tree class. curr, being incremented, node. that's why returned node.
void wcbst::write_to_file() { // ofstream outfile; outfile.open("wcbst.txt"); curr = minvalue(root); (int = 0; < count;i++) { cout << '{' << curr->data.first << ',' << curr->data.second << "}\n"; outfile << '{' << curr->data.first << ',' << curr->data.second << "}\n"; ++curr; } node *wcbst::operator++() { return nextinordersuccessor(root, curr); } node* wcbst::nextinordersuccessor(node* root, node* tofind) { node* successor = null; if (tofind->right) { successor = tofind->right; while (successor->left) { successor = successor->left; } } else { while (root) { if (tofind->data < root->data) { successor = root; root = root->left; } else if (tofind->data > root->data) { root = root->right; } else { break; } } } return successor; }
Comments
Post a Comment