I was brushing up on different tree traversal methods and ended up reading the following Wikipedia article. As expected, there are three methods of depth first traversal for a binary tree:
- Preorder traversal
- Postorder traversal
- Inorder traversal
The article then goes on to deal with depth first traversal of an arbitrary (generic) tree. I have pasted it here for convenience:
// To traverse a non-empty tree in depth-first order,
// perform the following operations recursively at each node:
Perform pre-order operation
for i=1 to n-1 do
Visit child[i], if present
Perform in-order operation
Visit child[n], if present
Perform post-order operation
Here is all the explanation that Wikipedia provides:
where n is the number of child nodes. Depending on the problem at
hand, the pre-order, in-order or post-order operations may be void, or
you may only want to visit a specific child node, so these operations
should be considered optional. Also, in practice more than one of
pre-order, in-order and post-order operations may be required. For
example, when inserting into a ternary tree, a pre-order operation is
performed by comparing items. A post-order operation may be needed
afterwards to rebalance the tree.
The algorithm specified makes no sense to me since it is specified in terms of undefined operations:
- A preorder operation.
- A postorder operation.
- An inorder operation.
To add to the confusion, I can’t come up with a definition for the said operations based on what I know and what is present in the Wikipedia article. I have been puzzling over this for a while with no real breakthroughs. I have the following questions:
- Is the algorithm specified in the Wikipedia article wrong? I suspect it is, but can’t say anything for certain beyond the fact that it is ill-specified.
- Are a postorder, preorder, inorder depth first traversal even defined for a generic tree? Are these practically used? Does it relate to the definition of the three operations? If so, how?
- If the algorithm is indeed correct, can someone define the above operations for me and explain how it works?
The algorithm stated is indeed correct. What’s happening in this case is that the Wikipedia article contains one piece of code that handles a general case that handles preorder, inorder, and postorder traversals all in one.
You can think of preorder, inorder, and postorder traversals all as special cases of a more general algorithm. Specifically, suppose that you want to do a tree traversal and perform some operation at a particular time during the search (either preorder, inorder, or postorder). One way to think about this is that you do some preorder operation before visiting the current node, some inorder operation between visiting the node’s child and the node itself, and some postorder operation after visiting the node. Any of these operations can be “do nothing.” For example, a simple preorder traversal would be specified as
Similarly, a postorder traversal would be
The advantage of the Wikipedia code is that it lets you do operations that would require both a preorder and postorder step. For example, suppose you want to do a tree search, but track at each point in time what nodes have been visited but not finished yet. You could do this as follows:
Some algorithms, like Tarjan’s SCC algorithm, actually do things like this (though admittedly that’s a graph algorithm and the question pertains to trees). The Wikipedia code thus gives the general case of which the more common cases, plus this more advanced case, are special cases.
Hope this helps!