I am constructing an AVL tree program. I got stuck in a rather easy situation, but difficult to understand what is wrong. The reason I think it is the program's fault and not mine is because I have the exact same class function right before it with "left" and "right" swapped and it works just fine...
As you can see, the function returns the temproot
pointer which is equal to temp2
if root==temp
. The funny thing is, that although when I test-print the value of temproot
JUST before it returns it (that in my example is 15), the actual value that is returned is STILL 20 (the previous value of temproot
). I triple checked everything. It doesn't seem to return the newly acquired value... What may be the problem?
To be more specific, the exact code is this:
//structure
struct avlnode
{
int data;
avlnode * left;
avlnode * right;
}* root;
//class function
avlnode * Tree::RL_rotation (avlnode * temp)
{
avlnode * temproot = temp;
avlnode * temp1= new avlnode;
temp1=temp->right;
avlnode * temp2= new avlnode;
temp2=temp1->left;
temp1->left=temp2->right;
temp2->right=temp1;
temp->right=temp2;
temp->right=temp2->left;
temp2->left=temp;
if (root==temp)
{
root=temp2;
temproot=temp2;
}
cout << "temproot= " << temproot->data << endl;
return temproot;
}