Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I want to keep a counter inside of a class in order to print

+3 votes
asked Jun 4, 2021 by despoina manoyse (150 points)
class B{

 public: int metritis;

private: node * ptr;

public:

void func() override {
        
        this->ptr=findwhat(this->ptr,metritis);
        }

node * findwhat(node *p, int & metritis){......}

} What I want is to find a way to access all the pointers followed by p in order to print the contents of a duplicate avl tree using my one ( as asked in my assignment) iterator.

class TreeIteratorImpl : public Iterator<T>::Impl {
  private:
    typedef typename Iterator<T>::Impl Impl;

  public:
    Impl *clone() const override { return new TreeIteratorImpl(ptr); }
    T &access() const override { return ptr->data; }
    int metritis;
    //node *metr; metr->data=0;
    void advance() override {
         //int * metritis; cout<<metritis;
        //int metritis=metr->data;
        this->ptr=findwhat(this->ptr,metritis);
        
    }

    node *findwhat(node *p,int &metritis)
    {
        if(p==nullptr){
            metritis=0; return nullptr;
        }
        if(p->counter>1&&(metritis==1))
        {
            metritis=2; p->visited=2; return p;
        }
        else    if (p->right != nullptr)
      {
           p = leftdown(p->right);
           if(p!=nullptr)
           {   //cout<<"B";
               int v=p->counter;
               if(v==2)
               {   //cout<<"C";
                   p->visited=1;metritis++;p->visited++; //cout<<"HIHI";
                   return p;
                }
                return p;

           }

      }

      else
      {
          p = leftup(p);
          if(p!=nullptr)
          {  //cout<<"D";
              int v=p->counter;
               if(v==2)
               {   //cout<<"E";
                   p->visited=1;metritis++;p->visited++; //cout<<this->ptr->visited; //cout<<"popo";
                   return p;
               }
               return p;
          }

      }
        
        
        
        
        
        
    }

    bool equal(const Impl &i) const override {
      return ptr == ((TreeIteratorImpl *)&i)->ptr;
    }

    TreeIteratorImpl(node *p) : ptr(p) {}

  protected:
    node *ptr;
    friend class avltree<T>;
  };

THis prints all the iterators twice expcept from the leftleft bottom child. How can I correct it?

1 Answer

0 votes
answered Jun 8, 2021 by xDELLx (10,500 points)
The code looks incomplete .No implementation of avltree<T>.

Can u share it & also show its usage in main.

:)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...