This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Recursive / Non-Recursive Function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Recursive / Non-Recursive Function" Watch "Recursive / Non-Recursive Function" New topic
Author

Recursive / Non-Recursive Function

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
What is the best way to write a recursive and non-recursive solution for a binary tree in order traversal?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Work this with pencil and paper first and see how it goes.

What are the rules to visit in order? Something like:

1 Go left as long as you can
2 Go back to the last place you haven't gone right yet
3 Go right
4 Repeat from 1

First make a "Node" with "left" and "right" variables that refer to other nodes. Set up the 1,2,3 nodes. See if you write "go left as long as you can" in a loop. Then we'll worry about the next steps.

Take a shot at this in code and show us what you make. If you get stuck, then we'll know right where to help.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recursive / Non-Recursive Function
 
Similar Threads
Why recursion?
Towers of Hanoi
Iterative Fibonacci Method Problem
Is it possible to delete all files and folders in a drive non-recursively?
how to clear the stack