Re: Iterative Traversal of a binary tree..

persephone lenin <[email protected]> Fri, 6 May 2005 17:24:49 +1000 (EST)
Newsgroups gmane.comp.programming.cppug
Message-ID <[email protected]>
Hiya.

First in the function there is an unnecessary pointer
variable, temp ;).
It seems that your algorithm is skipping
head->right->right. It's hard to explain, so I'll
suggest getting a whiteboard and tracing your way
through the tree as you go through the code.
This may help you improve the function.
Is there any special reason why you can't use
recursion? 
   Beege.



--- Srinivas Ponugupaty <[email protected]> wrote:
---------------------------------
Hi All :-

I am trying to implement a routine to traverse a
binary tree without recursion or using a stack.

The code is below.However I am am not able to get all
the nodes in the binary tree..where am i going wrong.
Thanks

 

void display ( struct node * head )
  {
    struct node * ptr , * temp ;
    ptr = head ;
       
    while ( ptr != NULL ) 
    {
            printf("%d\t",ptr->data ) ;
            if ( ptr->left != NULL )
            ptr = ptr->left ;
        else if ( ptr->right != NULL )
            ptr = ptr->right ;
            else 
            {
             ptr = head->right ;
             while ( ptr  != NULL )
                   {
                         printf("%d\t",ptr->data ) ;
                         if ( ptr->left != NULL )
                        ptr = ptr->left ;
                    else if ( ptr->right != NULL )
                        ptr = ptr->right ;
                        else 
                        return ;
                      }
            }
    } 


            
Yahoo! Mail
Stay connected, organized, and protected. Take the
tour:
http://tour.mail.yahoo.com/mailtour.html



---------------------------------
Yahoo! Groups Links

   To visit your group on the web, go to:
http://groups.yahoo.com/group/cppug/
 
   To unsubscribe from this group, send an email to:
[email protected]
 
   Your use of Yahoo! Groups is subject to the Yahoo!
Terms of Service.

p    e    a    c    e
"Rust your bullets with his tears"

Find local movie times and trailers on Yahoo! Movies.
http://au.movies.yahoo.com


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/cppug/

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/