there was error in else part and one more while loop was missing :-
here is the correct algo:-
while(!isEmpty(&s1) || !isEmpty(&s2))
{
while(!isEmpty(&s1))
{
val=pop(&s1);
print : val->data;
if(toggle == -1)
{
if(val->right)
push(&s2,val->right);
if(val->left)
push(&s2,val->left);
}
else
{
if(val->left)
push(&s2,val->left);
if( val->right)
push(&s2,val->right);
}
}
swap(s1,s2);
toggle=toggle * -1;
}
On Mon, Dec 12, 2011 at 9:48 AM, atul anand <[email protected]> wrote:
> so algo for zig-zag using two stack would be like this :-
>
> let* two stack s1,s2;*
>
> toggle = -1;
> push(root,&s1);
>
> while ( !isEmpty(&s1) || ! isEmpty(&s2))
> {
> while( ! isEmtpy(&s1))
> {
>
> print : val=pop(&s1);
>
> if(toggle == -1)
> {
> if(val->right)
> push(val->right , &s2);
>
> if( val - >left)
> push(val->left, & s2);
> }
> else
> {
> if(val->left)
> push(val->right , &s2);
>
> if( val - >right)
> push(val->left, & s2);
>
>
> }
>
>
> }
> swap(&s1,&s2);
>
> toggle=toggle * -1;
>
> }
>
> correct me if i am wrong.
>
> please improve above algo if you find any error.
>
> On Mon, Dec 12, 2011 at 12:40 AM, AMAN AGARWAL <[email protected]>wrote:
>
>> Hi,
>>
>> Yes.
>>
>> Regards,
>> Aman.
>>
>>
>> On Sun, Dec 11, 2011 at 12:54 PM, atul anand <[email protected]>wrote:
>>
>>> by zig-zag order means level order traversal ???
>>>
>>> On Sun, Dec 11, 2011 at 6:22 AM, AMAN AGARWAL <[email protected]>wrote:
>>>
>>>> Hi,
>>>>
>>>> Given a tree, in addition to the left and right pointer, it has a third
>>>> pointer, that is set to NULL.
>>>> Set the third pointer to a node, which will be the successor of the
>>>> current node, when the tree is traversed in the zig-zag order. In other
>>>> words, if we traverse the tree using this third pointer alone, then we will
>>>> be traversing the tree in the zig-zag order.
>>>>
>>>> Regards,
>>>> Aman
>>>>
>>>> --
>>>> AMAN AGARWAL
>>>> "Success is not final, Failure is not fatal: It is the courage to
>>>> continue that counts!"
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Algorithm Geeks" group.
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected].
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Algorithm Geeks" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>>
>>
>> --
>> AMAN AGARWAL
>> "Success is not final, Failure is not fatal: It is the courage to
>> continue that counts!"
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.