Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My Java code did not take double digit as input, (can't enter 10 or larger numbers, take only single digit from 0 to 9). The

My Java code did not take double digit as input, (can't enter 10 or larger numbers, take only single digit from 0 to 9). The code did not take parenthesis either.

Please help to modify the code so user can enter: double digit (like 10, 11 or larger numbers)

user can enter: parenthesis ( )

Thank you

**************************

// Java Program to convert Infix into postfix and prefix //

import java.util.Scanner;

// Create Class BinaryTree /

class BinaryTree

{

// Create class TreeNode /

class TreeNode

{

char data;

TreeNode left, right;

// Create constructor for TreeNode /

public TreeNode(char data)

{

this.data = data;

this.left = null;

this.right = null;

}

// insert data to note

public void addData(char data)

{

this.data = data;

}

}

public static TreeNode root;

// Create constructor

public BinaryTree()

{

root = null;

}

// add to left node

public void addLeft(TreeNode current, char data)

{

current.left = new TreeNode(data);

}

// add to right node

public void addRight(TreeNode current, char data)

{

current.right = new TreeNode(data);

}

// creata a binary tree with an infix expression

public void createBT(String expr)

{

StringBuilder rev_expr = new StringBuilder(expr);

expr = rev_expr.reverse().toString();

this.root = new TreeNode(' ');

TreeNode current = this.root;

for ( int i = 0; i <= expr.length()-1; i++ ) {

char d = expr.charAt(i);

if ( isDigit(d) ) {

if ( i < expr.length()-1 ) {

addRight(current, d);

addLeft(current, ' ');

}

else current.addData(d);

} else if ( isOperator(d) ) {

current.addData(d);

current = current.left;

} else {

System.out.println("invalid character"); // can throw exception here

}

}

}

// walk the binary tree post

// Operator precedence is not considered

// Evaluation is left to right

public void walkPost(TreeNode current)

{

if ( current != null ) {

walkPost(current.left);

walkPost(current.right);

System.out.print(current.data);

}

}

// walk the binary tree in

// Operator precedence is not considered

// Evaluation is left to right

public void walkIn(TreeNode current)

{

if ( current != null ) {

walkIn(current.left);

System.out.print(current.data);

walkIn(current.right);

}

}

// walk the binary tree pre

// Operator precedence is not considered

// Evaluation is left to right

public void walkPre(TreeNode current)

{

if ( current != null ) {

System.out.print(current.data);

walkPre(current.left);

walkPre(current.right);

}

}

// Create function to check if digit /

private boolean isDigit(char ch)

{

return ch >= '0' && ch <= '9';

}

// Create function to check if operator /

private boolean isOperator(char ch)

{

return ch == '+' || ch == '-' || ch == '*' || ch == '/';

}

// Create function to convert character to digit /

private int toDigit(char ch)

{

return ch - '0';

}

// Evaluate the expression

// Let's use the infix method

// Operator precedence is not considered

// Evaluation is left to right

public int EvalExp(TreeNode current)

{

if ( current != null ) {

int left = EvalExp(current.left);

if ( isDigit(current.data) ) return toDigit(current.data);

else if (isOperator(current.data) ) {

if ( current.data == '+' ) return left + EvalExp(current.right);

else if ( current.data == '-' ) return left - EvalExp(current.right);

else if ( current.data == '*' ) return left * EvalExp(current.right);

else if ( current.data == '/' ) return left / EvalExp(current.right);

else return 0; // can throw exception here

}

}

return 0;

}

//Create BinaryTreeTest /

public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

System.out.println("Binary Tree Expression Test");

// Create object for Binary Tree /

BinaryTree tre = new BinaryTree();

System.out.println(" Please Enter Infix Expression:");

String expr = scan.next();

tre.createBT(expr);

scan.close();

System.out.print("Prefix : ");

tre.walkPre(tre.root);

System.out.print(" Infix : ");

tre.walkIn(tre.root);

System.out.print(" Postfix : ");

tre.walkPost(tre.root);

System.out.print(" Evaluation: ");

System.out.println(tre.EvalExp(tre.root));

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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