# How to solve linear equation with binary tree

I'm working on a school project that takes in a simple linear equation and has to return the value of $$x$$. The code I have transforms $$x + 3 = 3x - 2$$ into a binary tree format like so:

        =
/   \
/     \
+       -
/ \     / \
x   3   *   2
/ \
3   x


With the expression in this format could someone please explain how can I obtain the value of $$x$$.
Any help is appreciated - if you have an alternative method that may make it easier I'd love to hear it.

For solving this question you should assume a tree with '=' root And in the left side of root push factors of x and in the right side push your constant values, now variable x is equal to sum of right side division by left side.

The solution of

$$ax+b=cx+d$$ is $$x=\frac{d-b}{a-c}.$$

Unless you want to handle other types of equations (but you don't explain), this is the general case.

• Unless you want to get into Tarski arithmetic and Galois theory, you probably don't want to handle the truly general case. Dec 21, 2022 at 21:03
• @Pseudonym: there can be more complex equations that are still linear. E.g. $a(x+b+c)=dx+f-gx$ or $(x-1)(x-3)=x^2+2x$... Dec 21, 2022 at 21:04
• Sure, and even if it's a polynomial on both sides, you can find common factors using GCD. But this still isn't the most general case. Dec 21, 2022 at 21:12
• @Pseudonym: the question is explicitly about linear equations. Dec 21, 2022 at 21:13
• Ah, I missed that. Fair enough. Dec 21, 2022 at 21:54

https://www.cs.utexas.edu/users/novak/algebra.pdf

Here is a link to a university article on a similar equation

• We're not looking for answers that consist solely of a link to an external resource. We're not looking to become just a link farm. Rather, we want to generate new content that will be useful to others and add value over time. Also, if that link stops working, then this answer will become useful. Please edit your answer to summarize the main ideas from that link.
– D.W.
Dec 21, 2022 at 23:29 This is nothing but a $$parse$$ $$tree$$ of your expression. Now coming to your question, you cannot actually obtain something from any parse tree. Parse trees are used in an abstract concept in compilers (to be exact they are the output of syntax analysis phase of a compiler). What you can do, or your software can do is that, u can first simplify the equation like-->

$$x+3=3x-2$$

$$=>2x=5$$

$$=>x=5/2$$

Now u can design a parse tree for the same, like 