Re: Re: Tree traversal without recursion!

Nikhil Gupta <[email protected]>
Newsgroups gmane.comp.programming.cppug
Message-ID <[email protected]>
Thanks a lot for your help.
Yes, it definitely helped me.
Please also help me write a function with breath first traversal without recursion and 
without queues and another with recursion.
 
Thanks,
Nikhil
 

Milind Mehendale <[email protected]> wrote:
hi nikhil

you can traversa a binary tree without recursion, by using stack. 
Following r pseudo codes of it, 

///////////// Pre-Order /////////////////////
Stack S
push root onto S
      repeat until S is empty
            v = pop S
            if v is not NULL
                  visit v
                  push v's right child onto S
                  push v's left child onto S

/////////////////////////////////////////////

///////////// Post-Order /////////////////////
Stack S
push root onto S
      repeat until S is empty
            v = pop S
            if v is not NULL
                  push v's right child onto S
                  push v's left child onto S            
      
                  visit v
                  
/////////////////////////////////////////////

///////////// In-Order /////////////////////
Stack S
push root onto S
      repeat until S is empty
            v = pop S
            if v is not NULL
                  push v's right child onto S
                  visit v
                  push v's left child onto S
/////////////////////////////////////////////

I hope this would help u!

Regards

Milind

--- In [email protected], "gupta_nkl" <gupta_nkl@y...> wrote:
> Please let me know, how to implement inorder, postorder, breath 
first 
> tree traversal without recursion.
> 
> Thanks,
> Nikhil



Yahoo! Groups Sponsor
To unsubscribe from this group, send an email to:
[email protected]



Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service. 


---------------------------------
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.