Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a class that transforms a Postfix expression (interpreted as a sequence of method calls) into an expression tree, and provides methods that process the

Write a class that transforms a Postfix expression (interpreted as a sequence of method calls) into an expression tree, and provides methods that process the tree in different ways. The push methods will operate on an internal stack of Tree nodes. A tester class has already been writtten (what I presume will work), but I am not sure how to write the methods exactly. Here is what I have: //POSTFIXTREE CLASS

import java.util.Stack; public class PostfixTree { private class TreeNode { TreeNode left, right; } TreeNode root;

Stack numberStack = new Stack();

//should push a node containing a number to the internal stack public void pushNumber(double d) { TreeNode n = new TreeNode(); //create a new node }

//should push a node representing addition public void pushAdd() { TreeNode n = new TreeNode(); //create a new node }

//should push a node representing multiplication public void pushMultiply() { TreeNode n = new TreeNode(); //create a new node }

//should push a node representing subtraction public void pushSubtract() { TreeNode n = new TreeNode(); //create a new node }

//should push a node representing division public void pushDivide() { TreeNode n = new TreeNode(); //create a new node }

//should return the numeric value associated with the tree. This must operate //recursively on the expression tree. Note: this should interpret the top of the internal //stack as the root of the tree public double evaluate() { double evaluatedResult = 0; return evaluatedResult; }

//should return a String containing a parenthesized inorder traversal of the expression //tree. Note: this should interpret the top of the internal stack as the root of the tree public String inorder() { String inOrderString = ""; return inOrderString; }

//should return the height of the expression tree (distance in edges between the root //and the farthest leaf). Note: this should interpret the top of the internal stack as //the root of the tree. public int height() { int height = 0; return height; } }

//MAIN METHOD (TESTER)

public class Tester

{

public static void main(String[] args)

{

//create a PostfixTree

PostfixTree tn = new PostfixTree();

//call all the methods on the PostfixTree

tn.pushNumber(1);

tn.pushNumber(2);

//how to print input besides hard coding it??

System.out.println("input: 1 2 +");

tn.pushAdd();

System.out.println("inorder: " + tn.inorder());

System.out.println("height: " + tn.height());

System.out.println("");

//how to print input besides hard coding it??

System.out.println("input: 1 2 + 3 4 - *");

tn.pushNumber(3);

tn.pushNumber(4);

tn.pushSubtract();

tn.pushMultiply();

System.out.println("inorder: " + tn.inorder());

System.out.println("height: " + tn.height());

System.out.println("");

}

}

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