Question

In: Computer Science

Hi there, I am making a program using trees, and recursion to compute prefix, infix, and...

Hi there, I am making a program using trees, and recursion to compute prefix, infix, and postfix expressions. I am struggling with placing parentheses to separate the infix numbers, and with how I should write my evaluation. (In java) if someone could please help me out. I included my evaluation class, and my current output.

public class Evaluation {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a prefix expression:");
        Node root = buildTree(sc);
        System.out.println("Infix expression:");
        infix(root);
        System.out.println("\nprefix expression:");
        prefix(root);
        System.out.println("\npostfix expression:");
        postfix(root);
        evaluate(root);
    }

    static Node buildTree(Scanner sc) {
        String input = sc.next();
        if (!input.contains("+") && !input.contains("-") && !input.contains("*") && !input.contains("/")) {
            return new Node(input);
        } else {
            Node right = buildTree(sc);
            Node left = buildTree(sc);
            return new Node(input, left, right);
        }
    }

    static void infix(Node node) {
        if (node != null) {
            infix(node.right);
            System.out.print(node.value + " ");
            infix(node.left);
        }
    }

    static void prefix(Node node) {
        if (node != null) {
            System.out.print(node.value + " ");
            prefix(node.right);
            prefix(node.left);

        }
    }

    static void postfix(Node node) {
        if (node != null) {
            postfix(node.right);
            postfix(node.left);
            System.out.print(node.value + " ");
        }

        static int evaluate (Node node){


                }
            }
        }
    }

Enter a prefix expression:
- + 10 * 2 8 3
Infix expression:
10 + 2 * 8 - 3 ------> should be ( ( 10 + ( 2 * 8 ) ) - 3 )
prefix expression:
- + 10 * 2 8 3
postfix expression:
10 2 8 * + 3 -
Process finished with exit code 0

Solutions

Expert Solution

// java


public class Evaluation {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a prefix expression:");
        Node root = buildTree(sc);
        System.out.println("Infix expression:");
        infix(root);
        System.out.println("\nprefix expression:");
        prefix(root);
        System.out.println("\npostfix expression:");
        postfix(root);
       int result= evaluate(root);
       System.out.println("\nThe value of the above expression is: "+result);
    }

    static Node buildTree(Scanner sc) {
        String input = sc.next();
        if (!input.contains("+") && !input.contains("-") && !input.contains("*") && !input.contains("/")) {
            return new Node(input);
        } else {
            Node right = buildTree(sc);
            Node left = buildTree(sc);
            return new Node(input, left, right);
        }
    }

    static void infix(Node node) {
        if(node!=null&&node.left==null&&node.right==null) // if the node is leaf
            {
                System.out.print(node.value);
                return ;
            }
        if (node != null) {
            System.out.print("(");
            infix(node.right);
            System.out.print(node.value + " ");
            infix(node.left);
             System.out.print(")");
        }
    }

    static void prefix(Node node) {
        if (node != null) {
            System.out.print(node.value + " ");
            prefix(node.right);
            prefix(node.left);

        }
    }

    static void postfix(Node node) {
        if (node != null) {
            postfix(node.right);
            postfix(node.left);
            System.out.print(node.value + " ");
        }

}
        static int evaluate (Node node){
            if(node==null)
            return 0;
            if(node.left==null&&node.right==null)  // if node is leaf then return the integer value of that string 
            {
                return Integer.parseInt(node.value);
            }
            int right=evaluate(node.right);
            int left=evaluate(node.left);
            int result=0;
            if(node.value.equals("+"))
            {
                result= left+right;
            }
            else if(node.value.equals("-"))
            {
                result= right-left;
            }
            else if(node.value.equals("*"))
            {
                result= left*right;
            }
            else if(node.value.equals("/"))
            {
                result= right/left;
            }
           

return result;
                }
                
    
}
        

Sample output:-


Related Solutions

C++ program that can take in EITHER "infix" or "prefix" (NOT POSTFIX) and transforms it into...
C++ program that can take in EITHER "infix" or "prefix" (NOT POSTFIX) and transforms it into the other one. Should use stack and have infixToPrefix and prefixToInfix functions with a simple main function for execution.
Hi I am getting error in implement some test case using Java. I am adding my...
Hi I am getting error in implement some test case using Java. I am adding my code and relevant files here, and the failed test. Please let me know where my problem is occuring and fix my code. Thanks! I will upvote. Implement a class to perform windowing of a Hounsfield value Implement the class described by this API. A partial implementation is provided for you in the eclipse project; however, unlike the previous class, very little work has been...
Hi, I would like to test a java program. I am learning linked list and going...
Hi, I would like to test a java program. I am learning linked list and going to make a linked lists for integer nodes. For instance, I am going to add the numbers 12, 13, and 16 to the list and then display the list contents and add 15 to the list again and display the list contents and delete 13 from the list and display the list contents and lastly delete 12 from the list and display the list...
Hi, (C programming) I am looking to write a program that will encrypt a text file...
Hi, (C programming) I am looking to write a program that will encrypt a text file automatically once program has opened, and have the option to decrypt it in a menu in my program. I have multiple text files that I need to encrypt, and would like if the program could encrypt all of them at once. I would also only like to decrypt the a text file once the name has been entered into a scanf function.
This is a Java program that I am having trouble making. 1- Search for the max...
This is a Java program that I am having trouble making. 1- Search for the max value in the linked list. 2- Search for the min value in the linked list. 3- Swap the node that has the min data value with the max one. (Note: Move the nodes to the new positions). 4- Add the min value and the max value and insert the new node with the calculated value before the last node. I already made a generic...
I am so lost on this. I am making a B in this class but I...
I am so lost on this. I am making a B in this class but I can not seem to get this project completed. Please help. ACC-120 Project – Financial Accounting This assignment supports the following outcomes: ·          Prepare journal and ledger entries for a service or merchandising business. ·          Prepare year-end statements for a service or merchandising business. ·          Report cost decisions using accounting principles and financial statement analysis. ·          Evaluate how knowledge, skills, and attitudes learned in this...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
Hi i am doing an assignment on the financial crisis. I am up to the conclusion...
Hi i am doing an assignment on the financial crisis. I am up to the conclusion and i am having trouble writing it can you please write up a summary of the financial crisis. Thanks
Hi! I am in an intro level Finance course and I am stuck on this problem....
Hi! I am in an intro level Finance course and I am stuck on this problem. Any help would be greatly appreciated. I am deciding on opening a restaurant. I was able to scrape together some capital from friends and family, but I must pay them back in 4 years at 12% per annum. I figure that it will cost me $165,000 to start up with rent, deposits, equipment, salaries, chicken, basil, rice, etc. for the first year, but I...
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT