Question

Assume you are given “preorder” and “inorder” traversal result of a Binary Tree. Write an algorithm...

Assume you are given “preorder” and “inorder” traversal result of a Binary Tree. Write an algorithm (pseudocode) that constructs the Binary Tree.

For example, you can start with the Pre-Order and In-Order traversal of the same tree given below.

Pre-Order = 80, 50, 10, 70, 100

In-Order = 10, 50, 70, 80, 100

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pre-Order traversal moves like root left right of the tree.

In-Order traversal moves like left root right of the tree.

To generate the binary tree from the given Pre-Order and In-Order traversal, the steps are as follows:

  1. In the Pre-Order traversal, the first node is the root of the tree.
  2. Find that node in In-Order traversal, nodes on the left side of it is the left sub-tree and the right side of it is the right sub-tree.
  3. Find the first node from Pre-Order in sub-trees, that node will be the root and find that node in In-Order traversal to find the left and right sub tree.
  4. Repeat 3, till there is no more sub-tree to process.

Pseudocode:

construct_tree(in_order, pre_order, start, stop):

  1. pIndex = 0 // variable to traverse the pre_order
  2. if start > stop, return null   // check it is a empty tree or not
  3. new_node = pre_order[pIndex++]   // creating new node and set to value of pre_order of pIndex
  4. if start == stop, return new_node   // Check if there is only one node or not
  5. iIndex = find(in_order, start, stop, new_node data) // finding the index of that value in In_order
  6. new_node left = construct(in_order, pre_order, start, iIndex-1) // left side of that value in in_order will be left sub tree
  7. new_node right = construct(in_order, pre_order, iIndex+1, stop) // right side of that value in in_order will be right sub tree
  8. return new_node   // return that node

find(in_order, start, stop, data):

  1. for i=start, i<=stop, i++
    • if in_order[i] == data return i    // finding the index of value in in_order

Pre_Order = 80, 50, 10, 70, 100

In_Order = 10, 50, 70, 80, 100

Traversing each element of Pre_Order, the first element is 80. 80 will become the root of the tree. And find the position of 80 in In_Order. The position is 4. All elements before 80, from positions 1 to 3 (10, 50, 70) are left subtree and position 5 (100) will be right sub-tree.

The next element in Pre_Order is 50. 50 will be the root. And find the position of 50 from In_Order, which is 2. The left element is 10 and the right element is 70.

The next element in Pre_Order is 10. There are no left and right nodes in In_Order and move to the next element of Pre_Order. The remaining nodes 70, 100 have no left and right nodes in In_Order.

The above tree is the resultant tree.

Add a comment
Know the answer?
Add Answer to:
Assume you are given “preorder” and “inorder” traversal result of a Binary Tree. Write an algorithm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT