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;...

  • Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException;...

    Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; class BSTTreeNode{    BSTTreeNode left, right;    String data; public BSTTreeNode(){    left = null;    right = null;    data = null; } public BSTTreeNode(String n){    left = null;    right = null;    data = n; } public void setLeft(BSTTreeNode n){    left = n; } public void setRight(BSTTreeNode n){   ...

  • Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from...

    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. Don’t worry if the tree is unbalanced. Note that...

  • 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...

  • 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;...

  • Please help me with me. I did the first part to write the operations but in...

    Please help me with me. I did the first part to write the operations but in the driver its shows me errors, can't fix it. Help me. Need help with this to run the application. /** * BinaryTreeNode represents a node in a binary tree with a left and * right child. * * @author Java Foundations * @version 4.0 */ public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right; /** * Creates a new tree node with...

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