Hi profissionals!
Happy new year : )
....
I have a binary tree project and I need to convert a fully parenthesized arithmetic expression to a binary tree.
I was thinking of this algorithm:
1. input a string of expression.
2. breakdown the string and creat new node for each char.
...
The problem is that I don't know how to build the tree and put the correct operation to be the root.
my tree class is:
public void insertNode(String value)
{
if (root == null)
root = new TreeNode(value, null);
else
insert(value, root);
}
public void insert(String value, TreeNode n)
{
// if (value < n.getData())
// {
if (!n.hasLeft())
n.setLeft(new TreeNode(value, n));
else
insert(value, n.getLeft());
// }
// else if (value > n.getData())
// {
if (!n.hasRight())
n.setRight(new TreeNode(value, n));
else
insert(value, n.getRight());
// }
}
How can I convert it to a binary tree?
Thank you for helping..