1

enter image description here

Hi, I need to get four things from the binary tree

  1. infix arithmetic expression GOT IT and tested:

-4*(-5-2)/(-1*x-(-3))

  1. Eval the expression with x = -1 GOT IT and tested:

7

  1. infix arithmetic expression like if instead * is +

I tried to make this but all wrong:

-4+(-5-2)/(-1+x)-(-3)
-4+(-5-2)/((-1+x)-(-3))
-4+(-5-2)/(-1+x-(-3))
-4+(-5-2)/(-1+x)-(-3)
-4+(-5-2)/-1+x-(-3)
-4+(-5-2)/((-3)-(-1+x))
-4+(-5-2)/(-3)-(-1+x)

(-5-2)+(-4)/((-1+x)-(-3))
(-5-2)+(-4)/(-1+x-(-3))
(-5-2)+(-4)/(-1+x)-(-3)
(-5-2)+(-4)/-1+x-(-3)
(-5-2)+(-4)/((-3)-(-1+x))
(-5-2)+(-4)/(-3)-(-1+x)
  1. Get the postfix arithmetic expression, I think should be this but no, is wrong too:

    45-2-1x-3--/-

    -4-52--1-3/

I hope you can help me. Thanks

7
  • 1
    This site is for programming questions, not CS theory/homework
    – Marc B
    Commented Oct 27, 2016 at 16:53
  • "I tried to make it but all wrong". What did you try to do exactly?
    – kraskevich
    Commented Oct 27, 2016 at 17:00
  • @kraskevich you see the long list after the I tried to make this but all wrong I made that, tried to replace the * with +, but not working, I don't know that i'm doing wrong. Commented Oct 27, 2016 at 17:02
  • What is exactly not working? Did you tried to do it manually? Do you have an algorithm? If yes, you should probably post it.
    – kraskevich
    Commented Oct 27, 2016 at 17:05
  • @kraskevich but that is manually, I posted every conbination that I think is the answer. Commented Oct 27, 2016 at 17:11

1 Answer 1

0

Number 3 is wrong on the first line (remember division and multiplication comes before addition and subtraction):

(-4 + (-5 - 2)) / ((-1 + x) - -3)
(-4 + -5 - 2) / (-1 + x - -3)

And Number 4 postfix notation (also known as reverse polish notation) you can get by depth first traversal, adding the parent node after both children were visited:

-4 -5 2 - * -1 x * -3 - /
2
  • 1
    Thanks @maraca, the 4 was correct, but the 3 not, but with your answer I got the correct one and is (-4+(-5-2))/(-1+x-(-3)) Thank you very much ! Commented Oct 27, 2016 at 20:50
  • Ah yes right, I made spaces to distinguish between unary and binary minus, but braces are probably better. Thanks for accepting.
    – maraca
    Commented Oct 27, 2016 at 20:51

Your Answer

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

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