Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me execute the last part of my Java file. Most of it is written. The program should read expression trees and determine pre-order

Please help me execute the last part of my Java file. Most of it is written. The program should read expression trees and determine pre-order and post-order traversal. I am having issues with user input of expression trees. Please look at the bottom part of my code and help me make it run. Users should input an expression, and the program should print the expression in pre-order and post-order. It must also display the result as a tree, not just a string. Any help is greatly appreciated. here is my code.

package OperatingSystemsHW1;

import OperatingSystemsHW1.Node;

public class Node> implements Comparable> { E data; Node left; Node right; public Node(E data) { this.data = data; this.left = null; this.right = null; } public Node(E data, Node left, Node right) { this.data = data; this.left = left; this.right = right; } public String toString() { return data.toString(); }

@Override public int compareTo(Node o) { // TODO Auto-generated method stub return this.data.compareTo(o.data); } }

package OperatingSystemsHW1;

import java.util.Scanner; import java.util.Stack;

import OperatingSystemsHW1.Node;

public class TreeExercise { public static boolean isOp(String str) { if (str.equals("/") || str.equals("+") || str.equals("-") || str.equals("*")) { return true; } return false; } // Constructs expression tree public static Node conExpTree(String str) { String[] arr = str.split("\\s+"); Stack> stk = new Stack>(); for (String item: arr) { Node newT = new Node(item); if (!isOp(item)) { stk.push(newT); } else { Node right = stk.pop(); Node left = stk.pop(); newT.left = left; newT.right = right; stk.push(newT); } } return stk.pop(); } //Pre-Order Traversal public static String preOrderTraveral(Node root) { if (root == null) return ""; String str = root.data; return str+ " " + preOrderTraveral(root.left) + preOrderTraveral(root.right); } //Post-Order Traversal public static String postOrderTraveral(Node root) { if (root == null) return ""; String str = root.data; return postOrderTraveral(root.left) + postOrderTraveral(root.right) + str + " "; } //In-Order Traversal public static String inOrderTraveral(Node root) { if (root == null) return ""; String str = root.data; return " " + inOrderTraveral(root.left) + str + inOrderTraveral(root.right) + " "; } ///////////////////////////////////// //main method public static void main(String []args) { Scanner scan = new Scanner(System.in); System.out.println("Expression Tree Calculator");

//need to make an object of an expression tree here (Don't know how)

System.out.println("Enter Expression:"); //object.conExpTree(scan.next());

System.out.print("Pre-Order Expression:"); //object.postOrderTraveral();

System.out.print("Post-Order Expression:"); //object.postOrderTraveral();

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+What kinds of problems need to be overcome?

Answered: 1 week ago