Question: import java.io.*; import java.util.*; import TreePackage.*; /** * PreToPost will take an expression in prefix notation and convert it into postfix using * a binary
import java.io.*;
import java.util.*;
import TreePackage.*;
/**
* PreToPost will take an expression in prefix notation and convert it
into postfix using
* a binary tree.
*
* @author Charles Hoot
* @version 4.0
*/
public class PreToPost
{
public static void main(String args[])
{
BinaryTree
String toParse = getExpressionString();
//ADD CODE HERE TO GET THE EXPRESSION TREE AND THEN ITERATE OVER
//IT TO PRODUCE THE POST FIX EXPRESSION
}
/**
* Get the tree from the pre order expression tokens recursively
*
* @param expressionParser a Scanner that holds the tokens in pre
order
* @return a Binary tree hold the expression
*/
private static BinaryTree
{
BinaryTree
// ADD CODE TO RECURSIVELY CONSTRUCT THE BINARY TREE FROM THE
PREFIX
// EXPRESSION HERE
return result;
}
/**
* getExpressionString - Get the string with the expression.
*
* @return A String from the keyboard.
*/
private static String getExpressionString()
{
Scanner input;
String inString = "";
try
{
input = new Scanner(System.in);
System.out.println("Please enter a prefix expression");
inString = input.nextLine().trim();
}
catch(Exception e)
{
System.out.println("There was an error with System.in");
System.out.println(e.getMessage());
System.out.println("Will use the expression + a b / c");
inString = "+ a b / c";
}
return inString;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
