C++ 操作员++;二叉搜索树的前向迭代器

C++ 操作员++;二叉搜索树的前向迭代器,c++,iterator,binary-search-tree,inorder,const-iterator,C++,Iterator,Binary Search Tree,Inorder,Const Iterator,我试图实现一个增量操作符(操作符++)来迭代BST(为了遍历),但是每当我尝试转到父节点时,我都遇到了一个问题。这就是我定义节点结构的方式[它是一个类模板 struct node{ T value; //Template node *left; //pointer to the left child node *right; /pointer to the right child node() : left(0) , right(0)

我试图实现一个增量操作符(操作符++)来迭代BST(为了遍历),但是每当我尝试转到父节点时,我都遇到了一个问题。这就是我定义节点结构的方式[它是一个类模板

struct node{
    T value;  //Template    
    node *left;     //pointer to the left child 
    node *right;    /pointer to the right child 

    node() : left(0) , right(0) {} 

    node(const T &v, node *l = 0 , node *r = 0)
        : value(v), left(l) , right(r) {}
}; node * _root;
迭代器从左边最小的子项开始

const_iterator begin() const {
    return const_iterator(FindSmallestPrivate(_root));}
当它指向null时结束

const_iterator end() const {
        return const_iterator(0);}
类const_迭代器的一部分

class const_iterator {

     const node *n;

public:
    typedef std::forward_iterator_tag iterator_category;
    typedef T                         value_type;
    typedef ptrdiff_t                 difference_type;
    typedef const T*                  pointer;
    typedef const T&                  reference;


    const_iterator() : n(0)){
    }

    const_iterator(const const_iterator &other) : n(other.n){

    }


    const_iterator& operator=(const const_iterator &other) {
        n = other.n;
        return *this;
    }

~const_iterator() {
        n = 0;
    }
现在我仍然无法解决的主要问题是:

const_iterator operator++(int) {
    const_iterator tmp(*this);

  /**
   starting from the smallest key and base on a given case
   it should points to the next smallest key (successor)
   and so on until it finds there are no more keys in the tree.

    */

    return *this;
    }
我尝试了很多方法在迭代器类中使用helper函数来找到解决方案,但即使调用一个函数来查找在BST类中运行良好的给定节点的后续节点,仍然存在同样的问题。 当我试图将“n”传递给找到其后继函数的函数时,我在编译时得到一个错误:“不能在没有对象的情况下调用成员函数”


如果有人知道解决此问题的方法,请告诉我。

“当我尝试通过“n”时?”?什么是“n”?显示的代码中没有所谓的“n”。“我很乐意发布整个类的源代码”——不,不要这样做。正如stackoverflow.com的文章中所解释的,您需要发布一个。你有没有读过stackoverflow.com的,它解释了如何提问,如果没有,为什么没有?@SamVarshavchik感谢你的回答。我刚刚编辑了这篇文章,添加了更多的细节。如果每个节点中都没有显式的
父节点
指针,你将非常非常艰难。@n.m.哈哈!我已经知道:D.我想如果我修改结构添加父节点会容易得多,但我写这里是为了看看是否有任何可能的方法。迭代器类始终可以维护从根节点到迭代器引用的当前节点的所有节点的内部状态,能够在树中导航,而无需在每个节点中正式存储父节点。