The search in pre-order consists in visiting each node, first visiting the number contained in it and then the subtree on the left and finally the one on the right. Recursively. In case there are more than two children, they should be visited from left to right. The notation given (A,(B),(C))
will already represent the tree in preorder, and therefore the result of the visit of the nodes will be the same sequence in which they appear in that notation, no matter where they are in the tree. So the result is then: 35, 80, 7, 11, 12, 15, 6, 0, 1, 2, 9, 3, 18, 8, 73, 13, 27 and 61. Your answer is almost correct, the only detail is that the last number is 61 instead of 71.
The in-order search consists of visiting each node, first visiting the left child, then the knot itself, and then the right child. Recursively. Consists of replacing (A,(B),(C))
for ((B),A,(C))
.
In the case of a knot (A,(B),(C),(D))
who has more than two children, it is not clear what should be done, since the in-order visit is only defined for cases where there are no more than two children. There are two possibilities here: replace (A,(B),(C),(D))
for ((B),A,(C),(D))
or by ((B),(C),A,(D))
. That means that the (6,(0),(1),(2))
could be visited as 0, 6, 1, 2 or as 0, 1, 6, 2.
The resulting order would be 12, 11, 7, 0, [1 and 6], 2, 15, 9, 80, 18, 3, 8, 35, 13, [73 and 27], 61. Numbers in [A and B] format can be exchanged depending on whether you want to use ((B),A,(C),(D))
or ((B),(C),A,(D))
.
What you seem to be wanting is the view on post-order which is to visit the node only after the children are visited. They consist of exchanging (A,(B),(C))
for ((B),(C),A)
. This ordination produces a result similar to what you proposed, only with the 35 at the end and with 61 at the place of 71. The post-order visit would be 12, 11, 0, 1, 2, 6, 9, 15, 7, 18, 8, 3, 80, 13, 27, 61, 73 and 35.
Thank you so much Victor!!!!!! I was with difficulty for days, you helped me. I hope to be able to reciprocate, grateful!
– Breno Nahuz