3

I have a problem understanding how to traverse a forest post-order. the definition for it is: (source: Data structures using C by Rohit Khurana page 330)

  1. traverse the subtrees of the first tree in tree post-order.

  2. traverse the remaining trees of F in tree post-order.

  3. visit the root node of the first tree of F.

here is the forest: F forest

and the post-order traversal of it mentioned in the book is:

C F E D B Q P Z Y X A

but I think that P is in the wrong place and the correct answer for it is:

C F E D B Q Z Y X P A

I want to know whether my answer is correct or if the book's answer is true, why it is true??

thanks,

Fatemeh Karimi
  • 914
  • 2
  • 16
  • 23

1 Answers1

0
  1. visit the root node of the first tree of F.

According to this statement, the answer in the book is correct. Visiting the root node as a last action is only valid for the first tree of the forest.

What you are suggesting is acting as if this statement is valid for other trees in the forest as well.

In your example, after outputting Q, you do not output P and instead output it right before A. However, P is the root of the second tree of F, not the first. Therefore, you should output P right after Q instead of at the end before A.

dgkr
  • 345
  • 2
  • 8