2
$\begingroup$

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.

$\endgroup$

4 Answers 4

-3
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
5
  • 1
    $\begingroup$ Unless you want to get into Tarski arithmetic and Galois theory, you probably don't want to handle the truly general case. $\endgroup$
    – Pseudonym
    Dec 21, 2022 at 21:03
  • $\begingroup$ @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$... $\endgroup$ Dec 21, 2022 at 21:04
  • $\begingroup$ 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. $\endgroup$
    – Pseudonym
    Dec 21, 2022 at 21:12
  • 1
    $\begingroup$ @Pseudonym: the question is explicitly about linear equations. $\endgroup$ Dec 21, 2022 at 21:13
  • $\begingroup$ Ah, I missed that. Fair enough. $\endgroup$
    – Pseudonym
    Dec 21, 2022 at 21:54
-1
$\begingroup$

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

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

$\endgroup$
1
  • 1
    $\begingroup$ 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. $\endgroup$
    – D.W.
    Dec 21, 2022 at 23:29
-2
$\begingroup$

enter image description here

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

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.