Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:26:43+00:00 2026-05-29T17:26:43+00:00

I was brushing up on different tree traversal methods and ended up reading the

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:26:44+00:00Added an answer on May 29, 2026 at 5:26 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been doing some brushing up on my B-Tree and 2-3-4 tree (B tree
So I've been brushing up on my Java skills as of late and have
Currently I'm brushing up on my Fortran95 knowledge (don't ask why)... I'm running in
Possible Duplicate: When should you use 'friend' in C++? I was brushing up on
I am brushing up again and I am getting an error: Cannot call member
I am brushing up on my design patterns at the moment and got a
For brushing up my C, I'm writing some useful library code. When it came
I have been brushing up on my design patterns and came across a thought
I'm currently brushing up on LINQ and am trying to comprehend the difference between
I've been brushing up on my C++ as of late, and I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.