The specified algorithm is really correct. What happens in this case is that the Wikipedia article contains one piece of code that handles the general case, which handles pre-order, order and post-processing in one.
You may think that preorder, inorder and postorder bypass everything as special cases of a more general algorithm. In particular, suppose you want to traverse a tree and perform some operation at a specific time during a search (for both preliminary and order). One way to think about this is that you perform some preliminary operation before visiting the current node, some inorder between visiting the child of the node and the node itself and some post-operation after visiting the node. Any of these operations may be βdo nothingβ. For example, a simple pre-order bypass will be listed as
- Pre-order step: complete the operation that you want to perform pre-order.
- Order Pitch: No-op
- Post-Step Step: No-op
Similarly, post-order workaround will be
- Pre-order Step: No-op
- Order Pitch: No-op
- Step in order: perform the operation you want to perform after <
The advantage of Wikipedia code is that it allows you to perform operations that require both a preliminary and a post-order step. For example, suppose you want to perform a tree search, but at each point in time you are tracking which nodes have been visited but not yet completed. You can do it as follows:
- Pre-order step: add the current node to the "active" list.
- Order Pitch: No-op
- Post-installation step: remove the current node from the "active" list.
Some algorithms, such as the Tarjan SCC algorithm , actually do such things (although, admittedly, the graph algorithm and the question relate to trees). Thus, the Wikipedia code provides a general case where more common cases, as well as this more complex case, are special cases.
Hope this helps!
source share