Saturday, May 14, 2011

Q. Create a tree from following traversing sequence of a tree.


Pre-order – 1 2 3 4 5 6 7
In-order – 4 2 5 1 6 3 7
Post-order – 4 5 2 6 7 3 1

Answer: To create a tree form traversing sequence we compare pre-order and in-order or post-order and in-order.

  1. First take one item form pre-order (1), make it root (in in-order traverse 1st item is always is root)
  2. Choose 2nd item (2), check this item in in-order traverse where it is; left or right side, make child of root as its position in in-order.
  3. Now take next item (3), again check its position in in-order traverse, put in tree as its position.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites