Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by Aristotle (Chancellor)
on Feb 16, 2007 at 23:41 UTC ( [id://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.

Replies are listed 'Best First'.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-29 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found