Question

1-Write an efficient algorithm to construct a binary tree from given inorder and postorder traversals.(java only)....

1-Write an efficient algorithm to construct a binary tree from given inorder and postorder traversals.(java only).

2- Apply your proposed algorithm in the previous point to construct the binary tree with the following traversals (java code only):

     In order traversal: 9 8 6 1 2 5 4

     Postorder traversal: 9 6 1 8 5 4 2

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

`Hey,

Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

// Data structure to store a Binary Tree node
class Node
{
   int key;
   Node left, right;

   Node(int key) {
       this.key = key;
   }
}

public class Main
{
   // Recursive function to perform inorder traversal of a binary tree
   public static void inorderTraversal(Node root)
   {
       if (root == null) {
           return;
       }

       inorderTraversal(root.left);
       System.out.print(root.key + " ");
       inorderTraversal(root.right);
   }

   // Recursive function to perform postorder traversal of a binary tree
   public static void postorderTraversal(Node root)
   {
       if (root == null) {
           return;
       }

       postorderTraversal(root.left);
       postorderTraversal(root.right);
       System.out.print(root.key + " ");
   }
       // Recursive function to perform postorder traversal of a binary tree
   public static void preorderTraversal(Node root)
   {
       if (root == null) {
           return;
       }
       System.out.print(root.key + " ");

       postorderTraversal(root.left);
       postorderTraversal(root.right);
   }

   // Recursive function to construct a binary tree from
   // inorder and postorder traversals
   public static Node construct(int start, int end,
               int[] postorder, AtomicInteger pIndex,
               Map<Integer,Integer> map)
   {
       // base case
       if (start > end) {
           return null;
       }

       // Consider the next item from the end of given postorder sequence
       // This value would be the root node of subtree formed by inorder[start, end]
       Node root = new Node(postorder[pIndex.getAndDecrement()]);

       // search the index of current node in inorder sequence to determine
       // the boundary of left and right subtree
       int index = map.get(root.key);

       // recursively construct the right subtree
       root.right = construct(index + 1, end, postorder, pIndex, map);

       // recursively construct the left subtree
       root.left = construct(start, index - 1, postorder, pIndex, map);

       // return root node
       return root;
   }

   // Construct a binary tree from inorder and postorder traversals
   // This function assumes that the input is valid
   // i.e. given inorder and postorder sequence forms a binary tree
   public static Node construct(int[] inorder, int[] postorder)
   {
       // get size
       int n = inorder.length;

       // map is used to efficiently find the index of any element in
       // given inorder sequence
       Map<Integer,Integer> map = new HashMap<>();
       for (int i = 0; i < n; i++) {
           map.put(inorder[i], i);
       }

       // pIndex stores the index of next unprocessed node from the end
       // of postorder sequence
       AtomicInteger pIndex = new AtomicInteger(n - 1);
       return construct(0, n - 1, postorder, pIndex, map);
   }

   public static void main(String[] args)
   {

       int[] inorder = { 9,8,6,1,2,5,4 };
       int[] postorder = { 9,6,1,8,5,4,2 };

       Node root = construct(inorder, postorder);

       // traverse the constructed tree
       System.out.print("Inorder   : "); inorderTraversal(root);
       System.out.print("\nPostorder : "); postorderTraversal(root);
       System.out.print("\nPreorder : "); preorderTraversal(root);
   }
}

Chrome File Edit View History Bookmarks People Tab Window Help 23% O Mon 10:38 PM Q M Inbox - gurkaranpree X C 1-Write An Eff

Note: Brother According to Chegg's policy we are only allowed to answer 1 part if it is not a one word answer and there are many. So, I request you to post other part as separate posts

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
1-Write an efficient algorithm to construct a binary tree from given inorder and postorder traversals.(java only)....
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