Tree Creation Via Traversal Output

Click For Summary
SUMMARY

This discussion focuses on constructing a binary tree from given pre-order and post-order traversal outputs. The pre-order traversal provided is "ADFGHKLPQRWZ" and the post-order traversal is "GFHKDLAWRQPZ". Key insights include identifying the root node as 'A' from the pre-order sequence and the leftmost node as 'G' from the post-order sequence. The discussion emphasizes a hands-on approach to visualize the tree structure by drawing the nodes based on these traversal outputs.

PREREQUISITES
  • Understanding of binary tree traversal algorithms (pre-order and post-order)
  • Familiarity with tree data structures
  • Basic skills in visualizing data relationships
  • Ability to interpret traversal sequences
NEXT STEPS
  • Research algorithms for constructing binary trees from traversal outputs
  • Learn about tree traversal techniques in depth
  • Explore visual tools for tree structure representation
  • Study examples of binary tree construction with different traversal combinations
USEFUL FOR

Computer science students, software developers, and anyone interested in algorithms related to tree data structures and their traversal methods.

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...
 

Similar threads

Replies
14
Views
3K
Replies
3
Views
2K
  • · Replies 20 ·
Replies
20
Views
4K
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K