As part of a Java assignment, I have to take an input arithmetic expression and store it in a binary tree.
I have done everything necessary for the assignment except for the part where I read in the string of the expression and store it in the binary tree.
I have created a class called BinaryTree. Its only field is a treenode called root. This treenode is defined as an innerclass in BinaryTree. It has 3 fields, a generic data field, and two children (left and right) that are type BinaryTree.
I'm having a very difficult time defining an algorithm for reading in an expression such as
(5*(2+3)^3)/2
and storing it in a tree like this
/
^ 2
* 3
5 +
2 3
Can anyone help with the algorithm?
1+2
. When you get that, do:1+2*3
. Yet more complex:1*2+3
. Finally:(1+2)*3
– Bandler