I am looking all over internet to find a logic to convert an Algebraic Expression into a Binary Tree.
I could only find ones where you first convert the algebra expression to postfix or prefix and then convert it to Binary Tree.
I am just curious to know , if its possible.
Any pointers to external links or logical answers to put me in the right direction ?
yes A Syntax Tree
So this expression
A+(B-C)*D+E*F
should be translated into
|-(+)-|
| |
|---(*)---| |---(*)---|
| | | |
|---(+)---| D E F
| |
| |
A |--( - )--|
| |
B C
-
There are dozens of algorithms for that (virtually everything labeled "parsing" that's more powerful than finite automata), and the algorithms for converting to postfix or prefix can most likely be adapted.user7043– user70432013年08月09日 18:03:23 +00:00Commented Aug 9, 2013 at 18:03
-
We don't need you to put "edit" in your questions, every time you edit them; we certainly don't need to see it in large, bold type. All questions posted on an SE site come with a comprehensive edit history that everyone can look at.Robert Harvey– Robert Harvey2013年08月09日 20:27:00 +00:00Commented Aug 9, 2013 at 20:27
1 Answer 1
You are overlooking the forest for the trees. The great majority of parsing algorithms have parse trees as their output, and virtually every treatise on parsing starts with arithmetic expressions as an example. Get any textbook, and chances are you can simply lift an algorithm from their examples, whether it's a recursive-descent parser, an LR parser or something else.