Question

In the following code I give, everytime it will output ")". Input : ((A+B)*((C-D)/(E^F))) Expect...

In the following code I give, everytime it will output ")".

Input :

((A+B)*((C-D)/(E^F)))

Expected output :

((A+B)*((C-D)/(E^F)))
A+B*C-D/E^F

my output :

)

My code :

class Et:
  def __init__(self , value):
        self.value = value
        self.left = None
        self.right = None
  def isOperator(c):
    if (c == '+' or c == '-' or c == '*' or c == '/' or c == '^' ):
        return True
    else:
        return False
  def inorder(t):
    if t is not None:
        inorder(t.left)
        print (t.value)
        inorder(t.right)
  def constructTree(postfix):
    stack = []
    for char in postfix :
        if not isOperator(char):
            t = Et(char)
            stack.append(t)
        else:
            t = Et(char)
            t1 = stack.pop()
            t2 = stack.pop()
            t.right = t1
            t.left = t2
            stack.append(t)
    t = stack.pop()
    return t
postfix = "((A+B)*((C-D)/(E^F)))"
r = constructTree(postfix)
inorder(r)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Whatever code you have written does not take care of parenthesis like '(' or ')'. In order to get the correct output, you would need to take care of the parenthesis separately.

Only after you take care of them, you will get the desired output for the expression tree.

Add a comment
Know the answer?
Add Answer to:
In the following code I give, everytime it will output ")". Input : ((A+B)*((C-D)/(E^F))) Expect...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT