Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This assignment you will work on a binary tree ( not binary search tree ) to represent mathematical formulas and printing the tree. ALGEBRAIC EXPRESSIONS
This assignment you will work on a binary tree not binary search tree to represent mathematical formulas and printing the tree. ALGEBRAIC EXPRESSIONS An algebraic expression can be represented using three different notations: Infix: The most conventional way to write an expression is called infix notation. The arithmetic operators appear in between two operands. eg Prefix: In prefix notation, as the name suggests, operators come before the operands. eg Postfix: In postfix notation, different from infix and prefix notations, operators come after the operands. eg One use of the binary trees in computer science is to represent algebraic expressions. In an algebraic expression tree, each leaf node contains an operand and a nonleaf node, including the root node, contains an operator that is applied to the results of its left and right subtrees. For example, the expression below: can be represented : Figure PART I BUILD TREE In this part you will build an algebraic expression tree using the binary tree data structure. The data structure will represent a single math formula given in postfix notation eg the formula in Figure in postfix form is: For simplicity you may assume the following: The postfix expression contains single digit integers and four operations In an expression, no whitespace is allowed between characters. You can use the Node class below to start writing your program: class Node char char c; public Node char c i this. this.c ; this.left this.right null Write a static method "buildTree" that takes a string formula written in postfix notation, then builds a binary tree Figure for that formula and returns the root node of the tree. public static Node buildTree String formulaInPostfix PART II PRINT TREE Write a static method "printTree" which takes the root node of a tree and prints the three as shown in the examples below: public static void printTree Node root HINTS Think about using a stack while reading the formula character by character. eg Stack
This assignment you will work on a binary tree not binary search tree to represent mathematical
formulas and printing the tree.
ALGEBRAIC EXPRESSIONS
An algebraic expression can be represented using three different notations:
Infix: The most conventional way to write an expression is called infix notation. The arithmetic
operators appear in between two operands. eg
Prefix: In prefix notation, as the name suggests, operators come before the operands.
eg
Postfix: In postfix notation, different from infix and prefix notations, operators come after the
operands. eg
One use of the binary trees in computer science is to represent algebraic expressions. In an algebraic
expression tree, each leaf node contains an operand and a nonleaf node, including the root node,
contains an operator that is applied to the results of its left and right subtrees. For example, the
expression below:
can be represented :
Figure
PART I BUILD TREE
In this part you will build an algebraic expression tree using the binary tree data structure. The data
structure will represent a single math formula given in postfix notation eg the formula in Figure in
postfix form is:
For simplicity you may assume the following:
The postfix expression contains single digit integers and four operations
In an expression, no whitespace is allowed between characters.
You can use the Node class below to start writing your program:
class Node char
char c;
public Node char c i
this.
this.c ; this.left this.right null
Write a static method "buildTree" that takes a string formula written in postfix notation, then builds
a binary tree Figure for that formula and returns the root node of the tree.
public static Node buildTree String formulaInPostfix
PART II PRINT TREE
Write a static method "printTree" which takes the root node of a tree and prints the three as shown in
the examples below:
public static void printTree Node root
HINTS
Think about using a stack while reading the formula character by character.
eg Stack
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started