Does the term "inorder traversal" have a well-defined meaning for trees wider than binary trees, or are "pre-" and "post-" order the only type of DFS that makes sense? I mean with n
>2 children per-node.
I guess for n
that is even it might mean going to the 'root' after n/2
children, but is this ever used like this? And what about odd n
?

- 20,590
- 28
- 126
- 201
2 Answers
The inorder traversal will continue to be well defined only if you explicitly partition the children set into left children and right children.
To see this, note that the inorder traversal actually enumerates nodes in the order in which they will appear if we flatten the tree (or equivalently, the order in which the nodes will appear if we gaze over the tree starting from left).
Thus, for an n-ary
tree, you will process the left children set first, followed by the parent and the right children set.
For example, consider the following tree :
If we define the set of left children to be the first 2 child nodes from the left, and the set of right children as the single last node, we will get the following inorder traversal :
14, 15, 5, 16, 17, 18, 6, 19, 2, 20, 21, 7, 8, 9, 3, 10, 1, 11, 12, 4, 13
The method of choosing the left and right children set will depend on the problem in hand.

- 8,765
- 3
- 36
- 38
-
The subtree with root 7, wouldn't be 20, 7, 21, and not 20, 21, 7 as you typed? – ASLLOP Sep 27 '21 at 04:26
-
1Note that we are defining left children to be ` first 2 child nodes from the left` (20, 21 are the first two children starting from left). Perhaps this could be made clearer in the answer. – axiom Sep 27 '21 at 20:23
-
You're right, I just thought it only applied to nodes with more than 2 children. – ASLLOP Sep 28 '21 at 08:32
The inorder traversal will continue to be well defined only if you explicitly partition the children set into left children and right children.
To see this, note that the inorder traversal actually enumerates nodes in the order in which they will appear if we flatten the tree (or equivalently, the order in which the nodes will appear if we gaze over the tree starting from left).
Thus, for an n-ary tree, you will process the left children set first, followed by the parent and the right children set.
For example, consider the following tree : enter image description here
If we define the set of left children to be the first 2 child nodes from the left, and the set of right children as the single last node, we will get the following inorder traversal :
14, 15, 5, 16, 17, 18, 6, 19, 2, 20, 21, 7, 8, 9, 3, 10, 1, 11, 12, 4, 13
The method of choosing the left and right children set will depend on the problem in hand.