Parse stack into a binary tree? - c

I'm making a program tasked with converting a math expression such as (2+4)*(4/3) into
a binary tree, and then manipulating it.
First, when parsing, i've turned the string into two stacks, operands and operators.
How can I determine what the root should be, given that in my example above the tree should look like this:
*
/ \
+ /
/\ /\
2 4 4 3
Notice that the root is * which is the outermost operand. But on my operand stack it looks like this:
/
*
+
And there could be cases like (2+4+3)*4 or 2*((4+1)/3).
How can I determine which operand should be the root of my binary tree?

Convert your infix expression to either prefix or postfix notation. You can't really have a proper operator stack without doing this.
In postfix notation, the expression (2+4)*(4/3) would look like:
2 4 + 4 3 / *
So, you have the multiplication appearing at the end which could be inserted into the tree as its root. Evaluating a postfix expression is much easier for a computer as grouping is not needed.

You can't just put the operators on your stack in the order that they appear in your expression. Once you've done that, you lose the ability to disambiguate, as you've identified.
See e.g. http://en.wikipedia.org/wiki/Shunting_yard_algorithm for an algorithm to parse infix notation.

You can use a stack to implement an infix to binary expression tree. This link has a C++ implementation:
An infix to binary-expression-tree parser that usings two stacks
one for operators and another for operands, which all derive from a base node class.

Related

What's the best way to read a mathematical function f(x,y) command line argument?

From main(), I want the user to input a mathematical function (I,e: 2xy) through the command line. From there, I initially thought to iterate through the string and parse out different arithmetic operators, x, y, etc. However, this could become fairly complicated for more intricate functions, (e.g: (2x^2)/5 +sqrt(x^4) ). Is there a more general method to be able to parse a mathematical function string like this one?
One of the most helpful ways to deal with parsing issues like that is to switch the input methods from equations like that to an RPN based input where the arguments come first and the operators come last.
Rewriting your complex equation would end up looking like:
2 2 x ^ * 5 / x 4 ^ sqrt +
This is generally easier to implement, as you can do it with a simple stack -- pushing new arguments on, while the operators pull the require pieces off the stack and put the result back on. Greatly simplifies the parsing, but you still need to implement the functions.
What you need is an expression evaluator.
A while ago, I wrote a complete C expression evaluator (i.e. evaluated expressions written using C syntax) for a command line processor and scripting language on an embedded system. I used this description of the algorithm as a starting point. You could use the accompanying code directly, but I did not like the implementation, and wrote my own from the algorithm description.
It needed some work to support all C operators, function calls, and variables, but is a clear explanation and therefore a good starting point, especially if you don't need that level of completeness.
The basic principle is that expression evaluation is easier for a computer using a stack and 'Reverse Polish Notation', so the algorithm converts an in-fix notation expression with associated order of precedence and parentheses to RPN, and then evaluates it by popping operands, performing operations, and pushing results, until there are no operations left and one value left on the stack.
It might get a bit more complicated is you choose to deal with implicit multiply operators (2xy rather then 2 * x * y for example. Not least because you'd need to unambiguously distinguish the variables x and y from a single variable xy. That is probably only feasible if you only allow single character variable names. I suggest you either do that and insert explicit multiply operators on the operator stack as part of the parse, or you disallow implicit multiply.

How to represent a mathematical expression in memory

Me and my friends are trying to implement a computer algebra system.
We have already implemented an algorithm that converts an expression like 1+2*4 to a binary tree
+
/ \
1 *
/ \
2 4
And we implemented an algorithm that evaluates this kind of binary tree.
We now want to implement an algorithm that simplifies expressions with variables.
For example, x+2x will become 3x
I was thinking it would be easier to merge similar operators in the binary tree. For example:
+
/ \
a +
/ \
b c
will become
+
/ | \
a b c
This way it would be easier to find terms that can be simplified.
For example, if I had x+2x+3x in my expression then they would be under the same + operator and thus they can find each other much easier without traversing most of the tree.
My friend thinks that we should implement the tree in a different approach. He suggested that we should implement each node as a polynomial, and then we can make operations such as polynomial addition between nodes. Using this approach we can make operations between polynomials much easier, without going back and forth the tree.
Which approach should we choose? is there a better approach other than these two?

Print a mathematical expression on a bitmap in c

I want my program to read a mathematical expression from standard input and print a bitmap with the expression formatted similarly to how Latex does this. Input is limited to simple expressions, that is, consisting of arithmetic operators, subscripts, superscripts and fraction bars.
For now, the program can interpret an expression and store it as a tree. The only problem I do not know how to solve is how to divide a plane into sections/boxes in order to print the expression on a bitmap properly. The biggest problem is with subscripts, superscripts (downscaling and placing symbols higher or lower), fraction bars and the fact that it has to work well recursively for example abcd
I would be grateful for an answer. I have tried to find a similar problem on the web, but nobody seems to have asked a question like this before.

Infix expression to Binary Tree in C

I have to parse an infix expression into a binary tree.
The expression is:
(((x1 + 5.12) ∗ (x2 − 7.68))/x3)
I don't really have a clue on how to interpret the expression. Does someone has a clue on how to process this?
Your task is not so hard, firstly you should acquaint yourself with notation types and then with expression parsing.
In general, to parse and evaluate an (infix) expression, you need to:
read and tokenize it, i.e. classify each symbol as: operand, operation, etc.
convert from infix to binary expression tree: this is usually done with algorithms such as Shunting yard algorithm.
create a grammar that defines operation precedence and allows strict1 order of expression evaluation.
Expressions written in infix notation are slightly more difficult to parse, that is why usually they are converted to more "machine friendly" versions, like (reverse) Polish notation which provides some advantages among which is the elimination of the need of parenthesses.
So, as you can see this is roughly the big picture and your task is a part of it. Here is a visualisation of binary expression tree for: 2 * 3 / ( 2 – 1 ) + 5 * ( 4 – 1 )
Here is more on the topic and an example implementation in C++.
1. In your case obeying the rules of Algebra.

Order of operations for dereference and bracket-ref in C

If I do *ptr[x], is that equivalent to *(ptr[x]), or (*ptr)[x]?
*(ptr[x])
See the Wikipedia operator precedence table, or, for a more detailed table, this C/C++ specific table.
In C, all postfix operators have higher precedence than prefix operators, and prefix operators have higher precedence than infix operators. So its *(ptr[x])
Using the counter-clockwise movement of analyzing and parsing that simple example
1. starting with ptr, work in counter-clockwise until you hit asterisk operator
2. asterisk, in counter-clockwise until you hit subscript operator
3. we arrive here, at subscript operator [x]
Since [] has higher precedence than the asterisk as per this table , that makes it *(ptr[x])

Resources