Thread: Problem in updating global pointer in tree.

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    Problem in updating global pointer in tree.

    Hi, I am having trouble updating my global pointer in the following code,
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct RB{
        RB()=default;
        RB(int clr):color(clr) { }
        int color;
        RB *p,*left,*right;
        int key;
        
    };
    RB *Tnil=new RB(0);
    RB *T=Tnil;
    void insert(RB *T,RB *z)
    {
        RB *y=Tnil;
        RB *x=T;
        while(x!=Tnil)
        {
            y=x;
            if(z->key<y->key)
            x=x->left;
            else
            x=x->right;
        }
        z->p=y;
        if(y==Tnil)
            T=z;
        else if(z->key<y->key)
            y->left==z;
        else
            y->right=z;
        z->right=Tnil;
        z->left=Tnil;
        z->color=1;
    }
    void print(RB *T)
    {
        if(T==Tnil)
        return;
        print(T->left);
        cout<<T->key;
        print(T->right);
    }
    
    int main()
    {
        
      for(int i=1;i<10;++i)
      {
        RB *x=new RB;
        x->key=i;
        insert(T,x);
      
      }
       print(T); 
        
        
    }
    The problem is, at line where I compar y==Tnil, It is evaluating to false at the first insert. But It should be true. again, after ending the function, T again becomes equal Tnil, as a result , none of the is being inserted. Any help?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your RB struct is rather lacking. Rather than doing like you have on lines 52&53, I would make a constructor that took the key value at least.
    It would also be tidier to NULL the pointers you aren't setting. Disabling the copy-constructor and assignment-operator would also be good.

    From the looks of it you are incorrect that y==Tnil is false on the first iteration. The problem, you have is that you never change the global T because it is shadowed by the parameter with the same name.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    thanks... now I understand..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Pointer and structures
    By TimJS in forum C Programming
    Replies: 3
    Last Post: 12-10-2012, 03:19 PM
  2. Global variable not updating
    By Flustration in forum C++ Programming
    Replies: 21
    Last Post: 04-19-2012, 11:05 AM
  3. not sure how to set static global pointer
    By itsthemac in forum C Programming
    Replies: 5
    Last Post: 05-10-2011, 01:12 AM
  4. Global pointer
    By misterowakka in forum C++ Programming
    Replies: 1
    Last Post: 01-22-2008, 10:50 AM
  5. global pointer problem?
    By LiLgirL in forum C Programming
    Replies: 7
    Last Post: 11-25-2003, 04:41 PM