Tree Creation Via Traversal Output

AI Thread Summary
To construct a binary tree from given pre-order and post-order traversal outputs, one can utilize the key insights provided by these traversals. The pre-order traversal identifies the root of the tree as the first element, while the post-order traversal indicates the leftmost node as the first element. In the example given, 'A' is the root from the pre-order sequence, and 'G' is the leftmost node from the post-order sequence. To build the tree, it is recommended to start by placing these two nodes on paper and then progressively adding the remaining nodes based on their relationships as indicated by the traversal sequences. This method emphasizes understanding the structural relationships between nodes as derived from the traversal outputs.
Ecurb
Messages
1
Reaction score
0
Hi, I'm familiar with tree traversal alogirthms, post,pre and in-order. If you give me a tree I can print out the specified traversal of that tree. However, one thing I can't do is build a tree from a given output. For example, on my last test, my prof gave me this Question:

A pre-order of a binary tree produced: ADFGHKLPQRWZ
A post order traversal of the same tree produced: GFHKDLAWRQPZ
Draw the binary tree.

How do you build a tree using two given traversal outputs?
 
Technology news on Phys.org
Hm, I don't know of a specific algorithm or method for doing this but I don't think it should be that hard.

Just consider what the pre and post order sequences tell you about where nodes are in relation to each other.

I would suggest starting with the two most obvious pieces of information the pre and post order tell you:

- The root is A (first letter of preorder)
- The leftmost node is G (first letter of postorder)

If it were me I would try drawing those two letters on a piece of paper and then try to fit in the rest of the letters one by one...
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top