Bifalgorithm post · Brisk · Chapter 1

How does in-order traversal position the root between its subtrees?

How does in-order traversal position the root between its subtrees? (Brisk · Chapter 1) — "Finally, to traverse a tree in-order, we: 1. Treat the left child an…

Ideas in this flowchart

  • In-order traversal visits each node between its left and right subtrees.
  • Traverse the left child and all of its descendants as a complete subtree.
  • Visit the root node.
  • Traverse the right subtree as a complete subtree.
  • Unlike pre-order or post-order, in-order traversal visits the root between its children.
  • In the sample tree, D is visited first because it has no left subtree.
  • After D, O and I are visited for the same reason.
  • K is visited before its right subtree, which visits C, M, and E in that order.
  • The complete visitation order is D, O, I, K, C, M, E, G, A, H, F, B, L, N.
  • If nodes are evenly spaced, the order can be read from left to right in the diagram.
  • Spatial position does not determine the traversal order; relationships between nodes do.
  • Moving I farther right would not change the order or make K visited earlier.
  • The node-visitation order is shown in a figure.
Feed/

How does in-order traversal position the root between its subtrees?

Channel

Brisk

1 page

"Finally, to traverse a tree in-order, we: 1. Treat the left child and all its descendants as a subtree, and traverse it in its entirety. 2. Visit the root. 3. Traverse the right subtree in its entirety. So instead of visiting the root first (pre-order) or last (post-order) we treat it in between our left and right children. This might seem to be a strange thing to do, but there’s a method to the madness which will become clear in the next section. For the sample tree, the first visited node is D. This is because it’s the first node encountered that doesn’t have a left subtree, which means step 1 doesn’t need to do anything. This is followed by O and I, for the same reason. We then visit K before its right subtree, which in turn visits C, M, and E, in that order. The final order is: D, O, I, K, C, M, E, G, A, H, F, B, L, N. If your nodes are spaced out evenly, you can read the in-order traversal off the diagram by moving your eyes left to right. Be careful about this, though, because ultimately the spatial position doesn’t matter, but rather the relationships between nodes. For instance, if I had drawn node I further to the right, in order to make the lines between D–O–I less steep, that I node might have been pushed physically to the right of K. But that wouldn’t change the order and have K visited earlier. The order of node visitation in in-order traversal is shown in Figure"

Flowchart preview
Series cover
👍
0
0 comments0 shares

Sign in to view and post comments.