Content deleted Content added
R. S. Shaw (talk | contribs) m rvv |
R. S. Shaw (talk | contribs) m revert to version of 20:46, 10 December 2005 by Mikkalai |
||
Line 9:
Say we have a tree node structure which contains a value <code>value</code> and references <code>left</code> and <code>right</code> to its two children. Then we can write the following function:
'''pre-order traversal'''
<pre>
visit(node)
Line 18 ⟶ 19:
This [[recursive algorithm]] prints the values in the tree in '''pre-order'''. In pre-order, each node is visited before any of its children. Similarly, if the print statement were last, each node would be visited after all of its children, and the values would be printed in '''post-order'''. In both cases, values in the left subtree are printed before values in the right subtree.
'''post-order traversal'''
<pre>
visit(node)
if node.left != null then visit(node.left)
if node.right != null then visit(node.right)
print node.value
</pre>
'''in-order traversal'''
<pre>
visit(node)
|