Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Tree traversal without recursion: the tree as a state machine

by Aristotle (Chancellor)
on Feb 16, 2007 at 23:41 UTC ( #600549=note: print w/ replies, xml ) Need Help??


in reply to Re: Tree traversal without recursion: the tree as a state machine
in thread Tree traversal without recursion: the tree as a state machine

Yes, itís just a different form of storage of the same data. Obviously, you have to store enough information to find your way around the tree somewhere. If itís not one place, itís another. However, this algorithm really does not use a stack, at least not if you define a stack in the only way which makes sense Ė that is, as a data structure whose defining property is support for the operations of pushing things onto the top and popping them off the top.

It consumes more space, of course, to keep all those parent pointers around. If you use a stack, they are transient and you only need O(log n) stack space, whereas itís O(n) if you store the pointers with the nodes Ė another way in which the two approaches are truly distinct.

What I like about this approach (which is what compelled me to post it) is the exceptional simplicity of the traversal algorithm. When it first occured to me, it seemed too simple to work Ė but no, the position and direction of the traverser encodes enough information to guide its path along the entire tree in the right order without any ambiguity.

Makeshifts last the longest.


Comment on Re^2: Tree traversal without recursion: the tree as a state machine
Reaped: Re^3: Tree traversal without recursion: the tree as a state machine
by NodeReaper (Curate) on Jul 29, 2008 at 13:04 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://600549]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2014-12-26 16:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (172 votes), past polls