4

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:

  1. Preorder traversal
  2. Postorder traversal
  3. 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:

  1. A preorder operation.
  2. A postorder operation.
  3. 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:

  1. 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.
  2. 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?
  3. If the algorithm is indeed correct, can someone define the above operations for me and explain how it works?
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
batbrat
  • 5,155
  • 3
  • 32
  • 38
  • did you mean , you don't know what is inorder/preorder/post order operation? – Poonam Feb 13 '12 at 07:10
  • @Poonam, I don't know what those operations mean in the context of the algorithm. Normally, I'd think of it as a single step of the corresponding traversal (preorder/postorder/inorder) that works on one 'parent' node and all its children. – batbrat Feb 13 '12 at 07:13
  • @Poonam, I have read the wikipedia article and a bunch of other stuff that defines these things. Just to make sure, I read through your PDF as well. I am well aware of how how Postorder/Preorder/Inorder traversal work, how to code them up recursively/iteratively with a stack. My particular hang up is with the definition of these operations in this context and how all three can be done at once. templatetypedef's answer clears most of it up, but I still am hung up on how a single such operation is defined. – batbrat Feb 13 '12 at 07:25
  • @Poonam, I mean no offence by my earlier comment. I'm merely clarifying my question. I would be grateful for a good definition of a single preorder/postorder/inorder operation. For a generic tree, consider a node A with the children a1, a2, ..., an. Is a preorder operation defined as visiting in this order A, a1, a2, ..., an? I want to get really specific and be 100% sure. – batbrat Feb 13 '12 at 07:29
  • In inorder traversal left node visited if left node is null then node data will be printed and then right of printed data again works from left of it ,null founds print data ,then right node and process repeats, In preorder first prints data and then goes to left tree prints data and to left till null founds and then right and process repeats ,for post order first traverse left till null then right of last left node and process repeats and at last data will be printed ,I am assuming you know this differences if not let me know,I will give example of it – Poonam Feb 13 '12 at 07:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7603/discussion-between-batbrat-and-poonam) – batbrat Feb 13 '12 at 07:41

1 Answers1

6

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

  • Preorder step: Do the operation you want to do preorder
  • Inorder step: No-op
  • Postorder step: No-op

Similarly, a postorder traversal would be

  • Preorder step: No-op
  • Inorder step: No-op
  • Postorder step: Do the operation you want to do postorder

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:

  • Preorder step: Add the current node to the "active" list.
  • Inorder step: No-op
  • Postorder step: Remove the current node from the "active" list.

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!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • I'm still chewing on what you said. However, I don't understand exactly what you mean by a Preodder/Postorder/Inorder operation? Suppose I am at node A in my depth first traversal and A has the children a1, a2, ..., an. Then Preorder would be A, a1, a2, ..., an. Is that one operation? How would an Inorder operation work if there are an odd number of children. I would also be very grateful if you specify how the basic search is working. The way I think of it, I end up doing it preorder in my head. Thanks! – batbrat Feb 13 '12 at 07:18
  • 1
    There's a difference between a preorder *ordering* of the nodes and a preorder *operation* on the nodes. The preorder operation is just something you do before you visit each node. If you print out the nodes during the preorder operation, you'll print out all of the nodes in preorder. The point of the Wikipedia code is to allow you to combine elements of preorder, postorder, and inorder traversals together into a single search capable of doing any one of the three. – templatetypedef Feb 13 '12 at 07:55
  • Let me see if I get this right- Preorder operation: Doing something to the node 'before' visiting it. How is this even possible? Postorder operation: Doing something to the node after I visit it. In other words, I visit it's children or even further and come back. Inorder operation: I do something with the data at the node as soon as I visit it. Have I understood right? – batbrat Feb 13 '12 at 08:04
  • 1
    @batbrat- It's a bit more subtle than that. The DFS walks over the nodes in the tree in a specific order, often revisiting nodes multiple times as it goes up and down. Visiting a node usually means looking at its child nodes and moving the search down into them. Doing an operation before visiting a node means that as soon as you arrive at the node in the search, you perform some operation on that node, and then you begin descending into its child nodes. Does that make sense? – templatetypedef Feb 13 '12 at 08:08
  • Ah! Thanks! That really helps. Postorder operations would occur when walking back up and re-visiting the node. The inorder operation though? When would that be? Right after visiting one of the children? That's what you seem to be saying. – batbrat Feb 13 '12 at 08:11
  • When you talk about the search example, you say "not finished yet." Does this mean that the search hasn't finished yet, or that the node has not been checked yet. I'm assuming the latter. – batbrat Feb 13 '12 at 08:13
  • 1
    @batbrat- What I mean is that you have visited some of the node's children, but not all of them. That is, part of the subtree rooted at the node has been explored, but not all of it. – templatetypedef Feb 13 '12 at 08:14
  • 1
    @batbrat- Yes, the inorder operation is applied in-between visiting the child nodes. And no, you wouldn't use it in a tree search. – templatetypedef Feb 13 '12 at 08:25
  • I really see the light! :) Thank you! Just to make sure, visiting some of the child nodes but not the rest allows for an inorder operation, right? Only, you don't use it for the search example! :) I think I get it. Just do let me know if I actually have! – batbrat Feb 13 '12 at 08:26
  • Thank you so very much! I finally get it! – batbrat Feb 13 '12 at 08:27