Question

JAVA Given the sequence of in-order traversal for a general binary tree, stored in an array...

JAVA

Given the sequence of in-order traversal for a general binary tree, stored in an array inOrder[] = {9, 5, 1, 7, 2, 12, 8, 4, 3, 11 }.

And given the sequence of post-order traversal for the same tree, stored in an array postOrder[] = {9, 1, 2, 12, 7, 5, 3, 11, 4, 8}.

Please implement the following method:

static BinaryTree buildTree(Object inOrder[], Object postOrder[])

-The method buildTree() returns a BinaryTree object in memory that is constructed on the basis of the two input sequences –– inOrder and postOrder.

Helper methods are allowed.

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

PLEASE GIVE IT A THUMBS UP

nikhil@nikhil-Vostro-15-3568:-/Desktop/CODE$ javac A.java nikhil@nikhil-Vostro-15-3568:-/Desktop/CODES java A 8 5 9 7 1 12 2

class BinaryTree {
int data;
BinaryTree left, right;

public BinaryTree(int data)
{
this.data = data;
left = right = null;
}
}
class Index {
int index;
}

class A {
static BinaryTree buildUtil(int inOrder[], int postOrder[], int inStrt,
int inEnd, Index pIndex)
{   
if (inStrt > inEnd)
return null;
BinaryTree node = new BinaryTree(postOrder[pIndex.index]);
(pIndex.index)--;
if (inStrt == inEnd)
return node;
int iIndex = search(inOrder, inStrt, inEnd, node.data);
node.right = buildUtil(inOrder, postOrder, iIndex + 1, inEnd, pIndex);
node.left = buildUtil(inOrder, postOrder, inStrt, iIndex - 1, pIndex);

return node;
}
static BinaryTree buildTree(int inOrder[], int postOrder[]){
int n = inOrder.length;
Index pIndex = new Index();
pIndex.index = n - 1;
return buildUtil(inOrder, postOrder, 0, n - 1, pIndex);
}

static int search(int arr[], int strt, int end, int value)
{
int i;
for (i = strt; i <= end; i++) {
if (arr[i] == value)
break;
}
return i;
}
static void print(BinaryTree node)
{
if (node == null)
return;
System.out.print(node.data + " ");
print(node.left);
print(node.right);
}

public static void main(String[] args)
{
int inOrder[] = new int[] { 9,5,1,7,2,12,8,4,3,11 };
int postOrder[] = new int[] { 9,1,2,12,7,5,3,11,4,8 };   
BinaryTree head = buildTree(inOrder, postOrder);   
print(head);
}
}

NM000 13 16 -/Desktop/CODE/A.java (CODE) - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Prefe

-/Desktop/CODE/A.java (CODE) - Sublime Text (UNREGISTERED) Tools Project Preferences Help File Edit Selection Find View Goto

Add a comment
Know the answer?
Add Answer to:
JAVA Given the sequence of in-order traversal for a general binary tree, stored in an array...
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
  • 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

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree,...

    [Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals.You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Pre order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line...

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • Suppose a binary tree data (in tiny written size) is stored in an array (A) as...

    Suppose a binary tree data (in tiny written size) is stored in an array (A) as given below and root is placed at “0”index. Note the array indices are in larger written size (0 to 74). Show the traversal data of the given tree for a)      In-Order Traversal b)     Post Order Traversal A 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3 28 13 36 15 9 22 44 7 10 75 33 19 15...

  • java A University would like to implement its students registery as a binary search tree, calledStudentBST....

    java A University would like to implement its students registery as a binary search tree, calledStudentBST. Write an Student node class, called StudentNode, to hold the following information about an Student: - id (as a int) - gpa (as double) StudentNode should have constructors and methods (getters, setters, and toString()) to manage Write the StudentBST class, which is a binary search tree to hold objects of the class StudentNode. The key in each node is the id. : import.java.ArrayList; public...

  • Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order,...

    Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...

  • Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binar...

    Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...

  • QUESTION 4 The shape of binary tree is unknown (unrelated to the tree defined in problem...

    QUESTION 4 The shape of binary tree is unknown (unrelated to the tree defined in problem 3). However, we do have the following information: performing an inorder and a postorder traversals of the tree (containing nine integer nodes with integer values from 1 through 9) yield the following outputs: a) Inorder (LNR): 123456789 b) Postorder (LRN): 1354 2 8 796 Based on the above traversal information, determine the shape of the binary tree. Note that there is no need to...

  • 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

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