![]() |
|
XP is just a number | |
PerlMonks |
Tree traversal without recursion: the tree as a state machineby Aristotle (Chancellor) |
on Feb 16, 2007 at 15:22 UTC ( [id://600456]=perlmeditation: print w/replies, xml ) | Need Help?? |
I am currently working my way through Higher-Order Perl, and as you抎 expect, the subject of tree traversal makes a frequent apperance:
There may be even more appearances later in the book that I抳e yet to discover; as I said, I抦 not through with it yet. However, the book changes topic after that, at least momentarily, so I stopped to ponder. It occured to me that this is the entire extent to which discussions of tree traversal typically go. Another obvious option that occured to me many years ago is not discussed anywhere that I抳e seen, though it is occasionally mentioned as a possibility in passing: You can get rid of any stacks whatsoever by keeping a parent pointer in the tree node data structure. Effectively, this turns the tree into a (sort of) state machine. While traversing, you need no memory other than the current and the previous node/state. The traversal algorithm is very simple:
Obviously, if there is no left child to descend to, you try the right one; and if there is no right child to descend to, you ascend to the parent. Traversal is complete when an attempt to ascend to the parent node fails because there is no parent. Pre-, post- and in-order traversal can be implemented simply by changing which of the conditions implies that the current node must be visited: if you visit the node when coming from?/p>
Assuming all tree nodes are instances of a class which has parent, left and right methods and uses undef to signify the absence of a pointer, the following is an implementation of the in-order version of the traversal algorithm in Perl:
This is the most straightforward implementation, which does have a fault: there is some code duplication between the coming-from-parent and coming-from-left-child states. The complication comes about because node visiting must be ensured even when the node does not have the particular pointer to come from; eg. in the case of in-order traversal, you visit the current node when you come from the left child node; but when a node has no left child node, you must still ensure that the node will be visited. The discovery that the left child node is absent will happen when the previous node was the parent, and so that state must ensure to visit the current node before going on to try to descend to the right. The fix is conceptually simple, but not easy to express in code. You need a way to fall through from the body of one branch to another抯 without checking the condition for that branch, much the way C抯 switch statement works, where branches fall through by default and require an explicit break to exit. A switch statement in C is simply a structured expression of a jump table (but note that you couldn抰 actually use a switch statement in C for this because the case conditions in this algorithm wouldn抰 be constant expressions); so the Perl version will need a couple of explicit gotos:
In this rendition of the algorithm, the reformulation required to implement pre- or post-order traversal is trivial: you just move the callback invocation to the appropriate label. (It is in fact quite simple to implement all three variants in a single function: just put a call in every branch and make them conditional on an extra parameter, eg. $visitor_callback->( $curr_node ) if $order == -1; where $order == 0 means in-order traversal and in that case the parameter is optional.) Makeshifts last the longest.
Back to
Meditations
|
|
女生排卵是什么意思hcv8jop8ns3r.cn | 葡萄套袋前打什么药hcv8jop6ns7r.cn | 贪心不足蛇吞象什么意思hcv9jop2ns9r.cn | 闲鱼转卖什么意思imcecn.com | 苍蝇吃什么hcv8jop1ns5r.cn |
呼吸内镜检查什么zhongyiyatai.com | 兼得是什么意思hcv7jop6ns9r.cn | 春风什么什么hcv8jop8ns8r.cn | 血月代表什么hcv9jop6ns4r.cn | 老死不相往来什么意思hcv8jop8ns5r.cn |
菜心又叫什么菜hcv8jop5ns9r.cn | 碱性磷酸酶偏高是什么原因helloaicloud.com | 外阴瘙痒擦什么药hcv8jop9ns0r.cn | who医学上是什么意思weuuu.com | 堤防是什么意思hcv9jop3ns5r.cn |
小孩子为什么老是流鼻血hcv8jop6ns2r.cn | 最贵的烟是什么hcv8jop1ns2r.cn | 白带什么颜色hcv9jop3ns9r.cn | 虚张声势是什么生肖hcv8jop5ns4r.cn | 8月27号是什么星座bfb118.com |