A binary expression tree is a specific kind of a binary tree used to represent expressions. The leaves of the binary expression tree are operands, and the interior nodes contain operators. Assume the possible operators including '+', '-', '*', '/', and '%' and operands are numerical data. The following figures illustrate binary expression trees for the expressions with different notations: Example 1 Example 2 Example 3 Postfix Exp: 14 -5 / BXT: [/] / \ [14] [-5] Infix order: 14 / -5 Prefix order: / 14 -5 Evaluates to -2.8 ----------------------- Postfix Exp: 20 3 -4 + * BXT: ___[*] / \ [20] [+] / \ [3] [-4] Infix order: 20 * 3 + -4 Prefix order: * 20 + 3 -4 Evaluates to -20.0 ----------------------- Postfix Exp: 2 3 + 5 / 4 5 - * BXT: ___[*]_____ / \ [/]__ [-] / \ / \ [+] [5] [4] [5] / \ [2] [3] Infix order: 2 + 3 / 5 * 4 -5 Prefix order: * / + 2 3 5 - 4 5 Evaluates to -1.0 Your Tasks: For this assignment, you will build a binary expression tree, display it, and evaluate it. You will encapsulate the behavior in a BXT class. The driver class, tree node, and the BXT class are provided. Please implement appropriate methods in BXT class to build, display, and evaluate a tree. Requirements for each method: Build a BXT: You need to change the string into a tree. The argument string is in postfix notation. Display Infix and Prefix orders Infix is characterized by the placement of operators between operands; Prefix expression notation requires that all operators precede the two operands that they work on; Postfix requires that its operators come after the corresponding operands. See following examples: Infix, Prefix, and Postfix Orders Infix Expression Prefix Expression Postfix Expression A + B + A B A B + A + B * C + A * B C A B C * + Evaluating the Expression Do this recursively. If the node is an operator, recursively evaluate the left child and the right child, and return the result. Else the node is a number, so it can be converted into a double, and returned. Requirements for your application: Please design an application to meet the following specific methods: buildTree(String str) : The argument string is in postfix notation. Build the tree as specified in the document-refer to examples 1 ,2 and 3; eveluateTree(): Do this recursively. If the node is an operator, recursively evaluate the left child and the right child, and return the result. Else the node is a number, so it can be converted into a double, and returned. infix(): Infix is characterized by the placement of operators between operands; prefix(): Prefix expression notation requires that all operators precede the two operands that they work on; posfix(): Postfix requires that its operators come after the corresponding operands Code must be written in JAVA

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

12.13 NVCC Lab: Binary Expression Tree

 

A binary expression tree is a specific kind of a binary tree used to represent expressions. The leaves of the binary expression tree are operands, and the interior nodes contain operators. Assume the possible operators including '+', '-', '*', '/', and '%' and operands are numerical data. The following figures illustrate binary expression trees for the expressions with different notations:

Example 1 Example 2 Example 3
Postfix Exp: 14 -5 /
BXT: [/] / \ [14] [-5] Infix order: 14 / -5 Prefix order: / 14 -5 Evaluates to -2.8 -----------------------
Postfix Exp: 20 3 -4 + *
BXT: ___[*] / \ [20] [+] / \ [3] [-4] Infix order: 20 * 3 + -4 Prefix order: * 20 + 3 -4 Evaluates to -20.0 -----------------------
Postfix Exp: 2 3 + 5 / 4 5 - *
BXT: ___[*]_____ / \ [/]__ [-] / \ / \ [+] [5] [4] [5] / \ [2] [3] Infix order: 2 + 3 / 5 * 4 -5 Prefix order: * / + 2 3 5 - 4 5 Evaluates to -1.0

Your Tasks:

For this assignment, you will build a binary expression tree, display it, and evaluate it. You will encapsulate the behavior in a BXT class. The driver class, tree node, and the BXT class are provided. Please implement appropriate methods in BXT class to build, display, and evaluate a tree.

Requirements for each method:

Build a BXT: You need to change the string into a tree. The argument string is in postfix notation.

Display Infix and Prefix orders

Infix is characterized by the placement of operators between operands;

Prefix expression notation requires that all operators precede the two operands that they work on;

Postfix requires that its operators come after the corresponding operands. See following examples:

Infix, Prefix, and Postfix Orders
Infix Expression Prefix Expression Postfix Expression
A + B + A B A B +
A + B * C + A * B C A B C * +

Evaluating the Expression

Do this recursively. If the node is an operator, recursively evaluate the left child and the right child, and return the result. Else the node is a number, so it can be converted into a double, and returned.

Requirements for your application:

Please design an application to meet the following specific methods:

  • buildTree(String str) : The argument string is in postfix notation. Build the tree as specified in the document-refer to examples 1 ,2 and 3;
  • eveluateTree(): Do this recursively. If the node is an operator, recursively evaluate the left child and the right child, and return the result. Else the node is a number, so it can be converted into a double, and returned.
  • infix(): Infix is characterized by the placement of operators between operands;
  • prefix(): Prefix expression notation requires that all operators precede the two operands that they work on;
  • posfix(): Postfix requires that its operators come after the corresponding operands
  • Code must be written in JAVA
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Types of trees
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education