Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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

Replies are listed 'Best First'.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://600549]
help
Chatterbox?
[Discipulus]: ++1nickt but what about: "out immigrants! just redskins!"
[1nickt]: I know what I believe and whether I have integrity. My actions will speak more.
[karlgoethebier]: choroba Nothing unclever in my sig. And remember: Beeing too clever is a bad mistake ;-)
[1nickt]: This is not to suggest that Karl should remove his slogan. Just thoughts of mine... Trust me I believe I am very much more radical than most, and if I had a slogan for every gross injustice in my sig, there would be no more bytes!
Discipulus redskins .. well native american..
[1nickt]: So if I was to offer any editorial advice it would be to (1) make it endure time passing (2) make it positive (3) make it about you and your beliefs
[1nickt]: (Disicpulus The new best choice is maybe "First People"
[karlgoethebier]: Discipulus: This is called "Volksverhetzung" in German which means
[karlgoethebier]: incitement of the people and it is prohibited by law
[1nickt]: The best sign I have sign in a protest march was carried by a kid of about 10 years "If you build a wall my generation will tear it down."

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (12)
As of 2017-06-22 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (519 votes). Check out past polls.