# 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.

## 2 Answers

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.

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