I implemented my binary tree with an iterator. The first 6 steps of the iterator are executed correctly, but then the iterator steps in place (the 6th element is displayed all the time). This is due to the fact that at a certain point the FindParent function returns not the ancestor of the element, but the element itself. Prior to this function worked out correctly. I can not understand why I ask for help. Here is my Next ():

bool Next() { if (currentptr == nullptr) // при первом вызове в сurrentptr записываю минимальный элемент { Node<T>* temp = root; while (temp->l != nullptr) temp = temp->l; currentptr = temp; return true; } else { Node<T>* temp = nullptr; Node<T>* parent = nullptr; while (true) { if (currentptr->r != nullptr) //если у элемента есть правый потомок { parent = currentptr; //предок = текущий элемент temp = parent; do { if (temp->r == nullptr) break; temp = temp->r; while (temp->l != nullptr) temp = temp->l; } while (true); if (temp->r == nullptr) { currentptr = temp; return true; } } else //если нет правого потомка { parent = FindParent(currentptr, root); //ищу родителя функцией FindParent if (parent->info < currentptr->info) while (parent->info < currentptr->info) parent = FindParent(parent, root); temp = parent; do { if (temp->r == nullptr) break; temp = temp->r; while (temp->l != nullptr) temp = temp->l; } while (true); if (parent->info > temp->info) { currentptr = temp; return true; } else { currentptr = parent; return true; } } } } } }; 

The function itself FindParent ():

 Node<T>* FindParent(Node<T>* currentptr, Node<T>* root) { if (root->info == currentptr->info && root->r != nullptr) return root->r; if (root->l != nullptr) if (root->l == currentptr) return root; if (root->r != nullptr) if (root->r == currentptr) return root; if (currentptr->info > root->info) FindParent(currentptr, root->r); if (currentptr->info < root->info) FindParent(currentptr, root->l); } 
  • And what are these comments "exactly the opposite"? "if (currentptr-> r! = nullptr) // if the element has no right descendant" ... - AnT
  • A typo. All night I just break my head :) - youke
  • Was the problem a typo? - αλεχολυτ
  • No, this is just a comment. The problem is relevant. - youke

0