T
T
toNtr2022-02-18 11:00:05
Algorithms
toNtr, 2022-02-18 11:00:05

Is symmetric traversal applicable to non-binary trees?

Hello!
I began to study tree traversal algorithms and noticed that in all examples the symmetric traversal is applied only to binary trees, but I could not find where it would directly say that other types of trees cannot be bypassed in this way or examples of their traversal.

In symmetrical binary tree traversal, we check first one child, then the parent, then the other child. But if we assume that there are more than two descendants, then the question arises: after which child is the parent to be checked or is the parent checked after each child, except for the last one?

Is there a way to symmetrically traverse a non-binary tree, and what does it look like?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2022-02-18
@toNtr

Just a thought.
Logic suggests that this approach is used when it is necessary not only to traverse the entire tree somehow, but to traverse in the sort order of the tree (or in reverse order).
Of course, you can apply this to trees with many nodes as well. But this will most likely not be a symmetrical bypass, because purely visually it doesn’t smell symmetrical, but algorithmically everything is the same. In some tree configurations with more than 2 child nodes, the traversal may well remain symmetrical (when there are an even number of child nodes and the parent is checked in the middle).
At what stage to check the parent in this case depends on how the descendants are sorted relative to the parent in this particular case. Those. in a binary tree, the left child is less than the parent, and the right one is greater - this is the rule and creates order when traversed. If there are more than 2 nodes, then you need to define a similar ordering rule for descendants relative to the parent. in accordance with this rule and make a detour.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question