Re: Tree traversal without recursion!

"Milind Mehendale" <[email protected]>
Newsgroups gmane.comp.programming.cppug
Message-ID <[email protected]>
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 ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/EbFolB/TM
---------------------------------------------------------------------~->

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/
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.