Question: Java Programming : 1 . Implement a binary tree node class similar to the following: Class BinaryNode { DataType key; BinaryNode left; BinaryNode right; }
Java Programming : Implement a binary tree node class similar to the following:
Class BinaryNode
DataType key;
BinaryNode left;
BinaryNode right;
Implement a program that reads in a postfix expression every operator is preceded by exactly two operands using integers as operands and basic mathematics operators and and constructs an expression tree out of binary nodes for this expression.
The program should utilize a stack to build the tree, using the following logic:
Read one symbol at a time from an input expression
If the symbol is an operand, create a onenode tree and push it to stack
If the symbol is an operator
Create a new node for the symbol
Pop two trees Tpopped first and T from the stack
Form a new tree: rootsymbol left T right T
Push the new tree to stack
Implement the three traversal methods: preorder traversal, inorder traversal and postorder traversal. When visiting a node, each method should print the nodes value.
Void preordertraverse node v Void inordertraverse node v
visit work at v; ifvleft null
for each child u of v inordertraversevleft
preordertraverseu; visit work at v;
ifvright null
inordertraversevright
Void postordertraverse node v
for each child u of v
postordertraverseu;
visit work at v;
Test all of your code using two postfix expressions, each with at least symbols
a First use your code from Task to read the postfix expression and return the tree
b Then run each of these three traversal methods on the tree, to print out the prefix, infix and postfix expression correspondingly.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
