For this project, you are going to build a converter to transform a numeric expression from...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
For this project, you are going to build a converter to transform a numeric expression from infix notation into a binary expression tree, then build a tree evaluator to evaluate it. For example, if the infix is: 7.8 (2.3 +2.5) The corresponding binary expression tree will be: 7.8 2.3 + 2.5 As you will see, there are quite some similarities between this project and the one we did in HW 6. Although we are using different data structures, as well as algorithms to achieve similar end results. a. Download the attached "Converter.java", "Evaluator.java", "Tokenizer.java", "TreeEvaluator.java", "TreeNode.java". You may also download the sample data file "expressions.txt", or you may use your own input data file. b. First,take a look at the "Tokenizer.java". It is exactly the same as HW 6 and is already written for you (DO NOT MODIFY IT). Its purpose is to read valid tokens one by one from the input string. A token can be either a number, an operator, or parenthesis. Its main interface consists two methods: Boolean hasNext Token() which returns true if there are still tokens remaining, false otherwise. String nextToken () which returns the next token when there are still tokens remaining. In a way, it is very similar to Scanner, which you should be fairly familiar with as of now. You can take a look at its implementation, which uses a queue. c. Now look at the "Converter.java". Its constructor takes in the infix expression. You can assume the input expression is valid. The sample "convert to postfix" implementation, as required in HW 6, is provided as a reference. You can study it a bit - this can be useful when you implement the "build binary expression tree" part, below. The corresponding sample of postfix expression evaluation as required in HW6 is also provided in "Evaluator. java". It is not really needed for this project, so feel free to skip it. d. Take a look at "TreeNode.java". This represents a node in the expression tree. It has a data field (which contains either an operator or a number), a left child, and a right child. A number node must be a leaf node - meaning both children will be null. All the code in this class is already written for you. Please study a bit for its public interface. e. Take a look at "TreeEvaluator.java". Its constructor takes in the root of an expression tree. A large part of it is implemented for you already, especially the print () method - it will print the tree in "vertical format", which will be handy for you to inspect the tree. f. Now go back to "Converter.java", and implement the method: public TreeNode getExpressionTree () It should read the tokens in the infix expression and build a binary expression tree from it, then return the root of the tree. The algorithm is quite similar to the "convert to postfix" one, with some small modifications. You need two stacks, one for the operators (the + */), one for the expressions. Read the tokens one by one from the infix expression; when you see a number, construct a "number" node (i.e. leaf node), and push into the expression stack; when you see an operator (+ -*/), push it into the operator stack; when you see a right parenthesis, you can get the operator by popping the operator stack, and get the two operands by popping from the expression stack, then construct a "operator" node and push it back into the expression stack. In the end, when you run out of tokens, there can be two cases. In one case, the operator stack is empty: now the only one node in the expression stack should be the root of the result expression tree. In the other case, there will be one operator and two expressions left: now you can just construct the final expression node with those and return it. g. Now go back to "TreeEvaluator.java", and implement the method: private double evaluateHelper (TreeNode node) This should be fairly straightforward to implement using recursion. If the node is a number, just return the number as the result. Otherwise, evaluate the left child and right child, then perform the arithmetic operation (+*/) and return the result. h. For this assignment you are limited to language features in Chapter 1-17 of the textbook. Also remember DO NOT change any method signature! For this project, you are going to build a converter to transform a numeric expression from infix notation into a binary expression tree, then build a tree evaluator to evaluate it. For example, if the infix is: 7.8 (2.3 +2.5) The corresponding binary expression tree will be: 7.8 2.3 + 2.5 As you will see, there are quite some similarities between this project and the one we did in HW 6. Although we are using different data structures, as well as algorithms to achieve similar end results. a. Download the attached "Converter.java", "Evaluator.java", "Tokenizer.java", "TreeEvaluator.java", "TreeNode.java". You may also download the sample data file "expressions.txt", or you may use your own input data file. b. First,take a look at the "Tokenizer.java". It is exactly the same as HW 6 and is already written for you (DO NOT MODIFY IT). Its purpose is to read valid tokens one by one from the input string. A token can be either a number, an operator, or parenthesis. Its main interface consists two methods: Boolean hasNext Token() which returns true if there are still tokens remaining, false otherwise. String nextToken () which returns the next token when there are still tokens remaining. In a way, it is very similar to Scanner, which you should be fairly familiar with as of now. You can take a look at its implementation, which uses a queue. c. Now look at the "Converter.java". Its constructor takes in the infix expression. You can assume the input expression is valid. The sample "convert to postfix" implementation, as required in HW 6, is provided as a reference. You can study it a bit - this can be useful when you implement the "build binary expression tree" part, below. The corresponding sample of postfix expression evaluation as required in HW6 is also provided in "Evaluator. java". It is not really needed for this project, so feel free to skip it. d. Take a look at "TreeNode.java". This represents a node in the expression tree. It has a data field (which contains either an operator or a number), a left child, and a right child. A number node must be a leaf node - meaning both children will be null. All the code in this class is already written for you. Please study a bit for its public interface. e. Take a look at "TreeEvaluator.java". Its constructor takes in the root of an expression tree. A large part of it is implemented for you already, especially the print () method - it will print the tree in "vertical format", which will be handy for you to inspect the tree. f. Now go back to "Converter.java", and implement the method: public TreeNode getExpressionTree () It should read the tokens in the infix expression and build a binary expression tree from it, then return the root of the tree. The algorithm is quite similar to the "convert to postfix" one, with some small modifications. You need two stacks, one for the operators (the + */), one for the expressions. Read the tokens one by one from the infix expression; when you see a number, construct a "number" node (i.e. leaf node), and push into the expression stack; when you see an operator (+ -*/), push it into the operator stack; when you see a right parenthesis, you can get the operator by popping the operator stack, and get the two operands by popping from the expression stack, then construct a "operator" node and push it back into the expression stack. In the end, when you run out of tokens, there can be two cases. In one case, the operator stack is empty: now the only one node in the expression stack should be the root of the result expression tree. In the other case, there will be one operator and two expressions left: now you can just construct the final expression node with those and return it. g. Now go back to "TreeEvaluator.java", and implement the method: private double evaluateHelper (TreeNode node) This should be fairly straightforward to implement using recursion. If the node is a number, just return the number as the result. Otherwise, evaluate the left child and right child, then perform the arithmetic operation (+*/) and return the result. h. For this assignment you are limited to language features in Chapter 1-17 of the textbook. Also remember DO NOT change any method signature!
Expert Answer:
Answer rating: 100% (QA)
SOLUTION import javaio import javautil public class Converter public static void mainString args thr... View the full answer
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date:
Students also viewed these programming questions
-
List three specific parts of the Case Guide, Objectives and Strategy Section (See below) that you had the most difficulty understanding. Describe your current understanding of these parts. Provide...
-
Supply chains of the Roman Empire had much in common with modern supply chains. Supply chains then and now require development of good mental models to understand them and keep them working well....
-
First, spend a couple of sentences summarizing the Concepts in the article below ? Then, answer the following. In the Concepts in Article, the speaker mentioned, "A stock is fundamentally more...
-
Compare sand, die, investment, lost foam, and continuous casting techniques.
-
How would you prepare pentanal from the following starting materials? (a) CH 3 CH 2 CH 2 CH 2 CH 2 OH (b) CH 3 CH 2 CH 2 CH 2 CH = CH 2 (c) CH 3 CH 2 CH 2 CH 2 CO 2 CH 3
-
Using the single-letter abbreviations for the amino acids in Table 23.1, write the sequence of amino acids in a polypeptide represented by the first four letters in your first name. Do not use any...
-
Carlos Martinez, who is a physical therapist, is thinking about starting a firm to provide in-home therapy services for people suffering from sports-related injuries. Carlos lives in Columbus, Ohio....
-
Presented below are selected accounts for Salazar Company as reported in the worksheet using a perpetual inventory system at the end of May 2014. Instructions Complete the worksheet by extending...
-
6. Consider two G.P.'s. 2, 22, 23, and 4, 4, 4, of 60 and n terms respectively. If the 225 n geometric mean of all the 60 + n terms is (2), then k(n-k) is equal to: (a) 560 (b) 1540 (c) 1330 (d) 2600
-
Construct a bond graph or Niggli formula to determine if it is possible for all anions to be equivalent in a structure of tetrahedrally coordinated cations and stoichiometry of C 2 A 3 ? Which...
-
Most contracts are made orally. Oral contracts are just as binding as written contracts if you can prove that they were made. However, because the existence of oral contracts may be hard to prove,...
-
In what circumstances can parents be held liable for the tortious acts of their children?
-
What is the difference between trespass to chattels and conversion, and what does a court consider when distinguishing between these two torts?
-
True Or False Under the retained control exception to the nonliability rule for independent contractors, an employer is considered to have retained control if the employer maintains control over the...
-
True Or False Some courts allow words alone to constitute an assault.
-
Under the doctrine of ____________ ____________, an individual is held liable for the tortious acts of another.
-
For the given ER , Diagram, ,please DESIGN A SIMPLE SEARCHENGINE INTERFACE IN HTML, CSS, JAVASCRIPT, PHP . ALLOW THEUSER TO FIND SCHOLARSHIP BY CATEGORY. UNREGISTERED SCHOLARSHIP FINDER- ER DIAGRAM...
-
In Problem 8.43, determine the smallest value of for which the rod will not fall out of the pipe. IA -3 in.-
-
Suppose that w(u, ) 0 for all edges (u, ) E. What is the relationship between the weight functions w and w?
-
Let f (n) an= g(n) be asymptotically positive functions. Prove or disprove each of the following conjectures. a. f (n) = O(g(n)) implies g(n) = O(f (n)). b. f (n) + g(n) = (min(f (n), g(n))). c. f...
-
Suppose that a dynamic set S is represented by a direct-address table T of length m. Describe a procedure that finds the maximum element of S. What is the worst-case performance of your procedure?
-
Example 11.3 introduces Klein's Model I. Use the data file klein to answer the following questions. a. Estimate the investment function in equation (11.18) by OLS. Comment on the signs and...
-
Mike's Veneer Shop owns a vacuum press that requires annual maintenance. Mike has a contract to cover the maintenance expenses for the next 5 years. The contract calls for an annual payment of \(\$...
-
True or False: The Consumer Price Index typically increases faster than the Higher Education Price Index.
Study smarter with the SolutionInn App