jeudi 22 mars 2018

++ operator for a BST Class

I have already created a BST class, with a root node, and left and right node. It is working perfectly. I am trying to create a ++ operator Iterator, which can just go through each node and increment its. This is what I get so far, I am still thinking it gat something to do with my constructor. Please help, below is just the nested Iterator class that I included in the BST class. I just cout to see if it is working but it keeps printing out 0. class Iterator { private: // private iterator state... nodeptr root; public: Iterator(nodeptr roots_) : root(roots_) {}; ~Iterator() {} bool operator!=(const Iterator& rhs) const { return (this-> root != rhs.root); } bool operator==(const Iterator& rhs) const { return (this->root == rhs.root); }

    Iterator operator++(T) {
      nodeptr ptr = root;
        if (root == NULL)
        {
            cout << "The tree is empty" << endl;
        }
        else
        {
            if (ptr->left != NULL)
            {
                ptr = ptr->left;
            }
            cout << ptr->data << " ";
            if (ptr->right != NULL)
            {
                ptr = ptr->right;
            }
            else
            {
                cout << "_";
            }
        }

        return *this;
    }

Aucun commentaire:

Enregistrer un commentaire