Open learner-long-life opened 7 months ago
We denote (A,B,C)
to mean the 3-node subtree
graph TD
A--->B
A--->C
(5,9) 13 17 21 24 29
17 (5,9) 13 17 24 21 29
(5,9,13) 17 21 24 29
17 (5,9,13) 17 24 21 29
((5,9,13), 17) 21 24 29
((5,9,13), 17) 24 21 29
((5,9,13), 17) (21,24) 29
((5,9,13), 17) (21,24) 29
((5,9,13), 17) (21,24,29)
((5,9,13), 17) (21,24,29)
((5,9,13), 17, (21,24,29))
((5,9,13), 17, (21,24,29))
I think whenever we encounter the same first element but different second elements in the two traversals, we should come back to the first element (maybe push it onto a stack?) but move on to the 21
and 24
and complete building the right subtree.
There is a much simpler recursive solution that I read, that shares the same in-order traversal and pre-order traversal list among all calls, splits the in-order traversal in each recursive call by slicing or start/end indices, and pops the front of the pre-order traversal, depending on popping for the parent first, then recursively calling the left child before the right child,
A similar solution for post-order traversal + in-order traversal would be identical except recursively calling the left and right children first, then popping for the parent from the post-order traversal afterwards.
This problem was assigned in [Readings 04]() but was much more difficult than I realized.
This is a tree I considered
with
In-order Traversal
Pre-Order Traversal
I'm proposing a recursive solution with a stack, and two pointers, one in the in-order traversal and one in the pre-order traversal.
The pseudocode looks something like this (it's not fully worked out).
5
in the example above.5 9
in the in-order traversal and9 5
in the pre-order traversal. They are in reverse order of each other, which means the lesser one is the left child of the greater one. Build this treeDelete
5
and9
from both traversals, and put the tree in their place (just one slot). 2b. If they do not contain the same two elements (but the same first element), push the same first element onto a stack, and go back to Step 2 with the next two elements.My working theory is that it's always possible to reconstruct the same tree given both
in-order
andpre-order
traversal.By similar algorithm, I believe it's possible to do the same with any two traversals, such as the second part of the problem states.