Question
Using java language. Build an expression tree. • When you see a number: o you create a new node with the number as the data.
class BTNode<E> private E data; private BTNode<E> left; private BTNode<E> right; public BTNode (E e) data=e; left=null; right
class BTree<E> private BTNode<E> root; public BTree () root=null; public void setRoot (BTNode<E> e) root=e; public BTNode<E>
public void postorder (BTNode<E> r) if (r!=null) postorder (r.getLeft()); postorder (r.getRight()); System.out.print (r.getDa
Test Data would be as follows:
16+3105/^* 16# @ - $ 19*3+15* - $ 36+24*7+ $ 176%22 - IS 176%3@# $
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Java program to construct an expression tree

import java.util.Stack;

// Java program for expression tree

class Node {

    char value;

    Node left, right;

    Node(char item) {

        value = item;

        left = right = null;

    }

}

class ExpressionTree {

    // A utility function to check if 'c'

    // is an operator

    boolean isOperator(char c) {

        if (c == '+' || c == '-'

                || c == '*' || c == '/'

                || c == '^') {

            return true;

        }

        return false;

    }

    // Utility function to do inorder traversal

    void inorder(Node t) {

        if (t != null) {

            inorder(t.left);

            System.out.print(t.value + " ");

            inorder(t.right);

        }

    }

    // Returns root of constructed tree for given

    // postfix expression

    Node constructTree(char postfix[]) {

        Stack<Node> st = new Stack();

        Node t, t1, t2;

        // Traverse through every character of

        // input expression

        for (int i = 0; i < postfix.length; i++) {

            // If operand, simply push into stack

            if (!isOperator(postfix[i])) {

                t = new Node(postfix[i]);

                st.push(t);

            } else // operator

            {

                t = new Node(postfix[i]);

                // Pop two top nodes

                // Store top

                t1 = st.pop();      // Remove top

                t2 = st.pop();

                // make them children

                t.right = t1;

                t.left = t2;

                // System.out.println(t1 + "" + t2);

                // Add this subexpression to stack

                st.push(t);

            }

        }

        // only element will be root of expression

        // tree

        t = st.peek();

        st.pop();

        return t;

    }

    public static void main(String args[]) {

        ExpressionTree et = new ExpressionTree();

        String postfix = "76%3@#/$";

        char[] charArray = postfix.toCharArray();

        Node root = et.constructTree(charArray);

        System.out.println("infix expression is");

        et.inorder(root);

    }

}

Add a comment
Know the answer?
Add Answer to:
Test Data would be as follows: Using java language. Build an expression tree. • When you...
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
  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • Question B1 You are given the following Java classes: public class Queue { private static class...

    Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node next; Node () { object = null; next = null; } Node (Object object, Node next) { this.object = object; this.next = next; private Node header; private int size = 0; // size shows the no of elements in queue public Object dequeue () { if (size == 0 ) { return null; else { Object remove_object = header.object;...

  • Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1)...

    Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • Q1) Write a method called inToTree of ExpressionTree class which takes a string of infix expression...

    Q1) Write a method called inToTree of ExpressionTree class which takes a string of infix expression (fully parenthesized), resets the root of the expression tree to such tree which constructed from the expression. The root set to null of the expression contains characters other than value, operator, and parenthesis or if it’s not an in-fix full parenthesized expression. You may assume each value is single digit only . public void inToTree(String expression){ You may use a JCF Deque class as...

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

  • JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective:...

    JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective: javafx GUI program for BST Animation. BSTAnimation.java, AbstractTree.java and Tree.java. Modify the BSTAnimation.java  (1) Add Show Preoreder and Show Postorder button. Insert these two buttons next to the Show Inorder button to display tree in selected order. (2) Currently removing node method is to replace the removed node by the highest node on left-subtree. Modify the method by using lowest node on the right-subtree instead....

  • Return a method as an expression tree Hi guys. I need to return a method as...

    Return a method as an expression tree Hi guys. I need to return a method as an expression tree, it's currently returning null. public static ExpressionTree getExpressionTree(String expression) throws Exception {       char[] charArray = expression.toCharArray(); Node root = et.constructTree(charArray); System.out.println("infix expression is"); et.inorder(root); return et; } In the above method, et needs to have a value. -- Take a look at the complete class below. Kindly assist. ; public class ExpressionTree extends BinaryTree { private static final String DELIMITERS...

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
Active Questions
ADVERTISEMENT