I have a problem with this very simple block of code. please give me your advice .
(My this problem is solved, and in solving this problem the person having id stakx really helped me, the only problem was that i was using stack< treeNode >, when i saw the push method of the stack carefully, there is a copying process when i write head->object=number, so finally i made the stack of pointers, like this stack< treeNode* > and it really solved the problem , i have no problem now , i am very very thankful to the person stakx.)
before the code you need to suppse the following tree
alt text http://img44.imageshack.us/img44/7016/avlimage06.jpg
as you can see in the picture that root is 8 and stack have two nodes i.e 6 and 4. i pass this stack and root node to the following code
void Avltree::attachwithtree(treeNode* tree, Stack<treeNode>&s)
{
if(!s.isempty())
{
treeNode *stacknode;
stacknode=s.pop();
cout<<"\ninside the attachwithtree function, stack node is "<<stacknode->data;
stacknode->right=tree;//attaching the passed node to the right of popped node
root=stacknode;//setting the root to stack node which is the private data member of class
updatebalance(root);//this function is ok, it does not create problem
while(!s.isempty())
{
cout<<"\nstack is still not empty";
stacknode=s.pop();
cout<<"\nright side of "<<root->data<<" is "<<(root->right)->data;
//the below three lines causing the problem i don't know why,
root=stacknode;
treeNode* temp;
temp=root->right;
cout<<"\n\n\nthe right side of "<<temp->data<<" is now "<<(temp->right)->data;
updatebalance(root);
}
the ouptput of this function is given by
here is the code of the pop method of the stack that i am using
template <class t>
t * Stack<t>::pop()
{
if(topelement!=NULL)
{
t* num;
current=topelement;
num=&(current->object);
topelement=topelement->preptr;
current=topelement;
return(num);
}
else
{
head=NULL;
}
}
here is the code of push method of the stack
template <class t>
void Stack<t>::push(t &number)
{
Node<t>* newNode=new Node<t>;
if(head==NULL)
{
head=newNode;
topelement=newNode;
current=newNode;
head->object=number;
head->preptr=NULL;
}
else
{
topelement=newNode;
newNode->preptr=current;
current=topelement;
newNode->object=number;
}
}
Original answer:
Could it be that the node
4on the stack has a different node6to its right (the one with node7to its right) than the node6(with the node8on its right) you’re working on? You could compare their addresses to make sure you haven’t got two different copies of node6around.Elaboration on the above argument:
Let’s look at your method’s signature:
sis defined as a reference to aStack<treeNode>.Could it be that it should be a
Stack<treeNode*>?Depending on your
treeNodeclass, it’s possible that when you pushXon this stack, you actually end up with a copy ofXand notXitself. Similarly, when you pop from the stack, you might not actually get the item you pushed, but an identical-looking copy of it!?This would mean that, at the time when you push node
6on the stack, its right child is node7. But you have pushed a new, identical node on the stack. Even if you pop this element from the stack and change it, you only change a copy and leave the original tree node as it was before.Therefore, you’d operate on different copies of node
6. First, you pop a copy of it from the stack, and append a tree as its right child. Checking this will give the right result.Then, you pop a copy of node
4from the stack. It’s right child is node6, as expected, BUT not the one you just modified but the original! Therefore you get7on the right side of node6.Demonstrating the difference between pass-by-value and pass-by-reference:
OK, here’s something you need to understand when working with pointers or references. It basically shows the difference between passing a parameter by value (a copy will be created) or passing it by reference (no copy will be created).
Study it carefully and then see how it applies to your problem.
Hint: Yes, in your
popmethod, you work with pointers, but most likely with a pointer to a copy of the object originally pushed on the stack.