Bifalgorithm post · Brisk · Chapter 1

How does post-order traversal visit the nodes of a tree?

How does post-order traversal visit the nodes of a tree? (Brisk · Chapter 1) — "It’s the same as pre-order, except that we visit the root after the children in…

Ideas in this flowchart

  • Post-order traversal of a tree
  • The root is visited after its left and right subtrees, unlike in pre-order traversal.
  • The traversal is difficult because node visits are postponed and their later order must be remembered.
  • A node can be visited only after both of its subtrees have been completed.
  • In the sample tree, I is the first node visited.
  • G waits for its subtrees, K waits for its subtrees, D waits for O’s subtree, and O waits for I.
  • During the unwinding, K still waits because its right subtree has not been visited.
  • The right-subtree visits produce C, E, and M, in that order, before K is visited.
  • The complete visitation order is I, O, D, C, E, M, K, A, F, L, N, B, H, and G.
  • This order is not the reverse of the pre-order visitation; G is last instead of first, while the remaining order is rearranged.
  • The post-order visitation sequence is shown in a figure.
Feed/

How does post-order traversal visit the nodes of a tree?

Channel

Brisk

1 page

"It’s the same as pre-order, except that we visit the root after the children instead of before. Still, despite its similarity, this has always been the trickiest one for me. Everything seems postponed, and you have to remember what order to do it in later. For our sample tree, the first node visited turns out to be I. This is because we have to postpone visiting G until we finish its left (and right) subtree; then we postpone K until we finish its left (and right) subtree; postpone D until we’re done with O’s subtree, and postpone O until we do I. Then finally, the thing begins to unwind...all the way back up to K. But we can’t actually visit K itself yet, because we have to do its right subtree. This results in C, E, and M, in that order. Then we can do K, but we still can’t do G because we have its whole right subtree’s world to contend with. The entire order ends up being: I, O, D, C, E, M, K, A, F, L, N, B, H, and finally G. Note that this is not remotely the reverse of the pre-order visitation, as you might expect. G is last instead of first, but the rest is all jumbled up. The order of node visitation in post-order traversal is shown in Figure"

Flowchart preview
Series cover
👍
0
0 comments0 shares

Sign in to view and post comments.