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?