Question

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

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)

This is the first of three assignments where you will develop a program that symbolically differentiates and arithmetic expression. You do not need to know how differentiation works to write any of the programs. You will not be doing any computations. You will be given a set of rules to apply.

Input:
For this first part you will be given a set of strings that represents a fully parenthesized infix expression that eventually (next assignment) be differentiated. The expression will be composed of single digit integers ("A"-"E"), the variable "X", parenthesis "(" and ")', and the binary operators +, -, *, /, and ^ (exponentiation). No spaces.

Process:
Generate a binary parse tree from the given input.

Output:
(1) Echo print the input string.
(2) Print out the parse tree using an in-order traversal.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

So Here i have change the whole construct tree function code. Your code works when the input expression is in postfix format. But our given input is not in that format so i have used another login in the following code. The code works fine for the given example. I have tested it for some other examples also. It is working fine till the expression is fully parenthesized. But if you find any problems mention in the comments section. I have used python3.

class Et:
def __init__(self , value):
self.value = value
self.left = None
self.right = None
  
def inorder(t):
if t is not None:
inorder(t.left)
print(t.value, end = "")
inorder(t.right)
  
def constructTree(postfix):
stack = []
for char in postfix :
if char == ")":
t1 = stack.pop()
t2 = stack.pop()
t3 = stack.pop()
t4 = stack.pop()
t2.right = t1
t2.left = t3
stack.append(t2)
else:
t = Et(char)
stack.append(t)
t = stack.pop()
return t
postfix = "((A+B)*((C-D)/(E^F)))"
print(postfix)
r = constructTree(postfix)
inorder(r)  

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))) Expected output...
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
  • 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)...

  • Return a method as an expression tree Hi guys. I need to return a method as...

    Return a method as an expression tree Hi guys. I need to return a method as an expression tree, it's currently returning null. public static ExpressionTree getExpressionTree(String expression) throws Exception {       char[] charArray = expression.toCharArray(); Node root = et.constructTree(charArray); System.out.println("infix expression is"); et.inorder(root); return et; } In the above method, et needs to have a value. -- Take a look at the complete class below. Kindly assist. ; public class ExpressionTree extends BinaryTree { private static final String DELIMITERS...

  • python pls and noticed the output added "" One way to represent a binary tree is using the nested list format Consider the following binary tree: 24 72 78 8 51 25 This binary tree could be...

    python pls and noticed the output added "" One way to represent a binary tree is using the nested list format Consider the following binary tree: 24 72 78 8 51 25 This binary tree could be represented using a nested list as follows [55, [24, [8, None, None], [51, [25, None, None], None]], [72, None, [78, None, None ]]] The nested list format always uses a list of length three to represent a binary tree. The first item in...

  • Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None de...

    Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None def search(self, find_data): if self.data == find_data: return self elif find_data < self.data and self.left != None: return self.left.search(find_data) elif find_data > self.data and self.right != None: return self.right.search(find_data) else: return None    def get_left(self): return self.left def get_right(self): return self.right def set_left(self, tree): self.left = tree def set_right(self, tree): self.right = tree def set_data(self, data): self.data = data def get_data(self): return self.data def traverse(root,order):...

  • The INORDER traversal output of a binary tree is U,N,I,V,E,R,S,I,T,Y and the POSTORDER traversal output of the same tree is N,U,V,R,E,T,I,S,I,Y

     a. The INORDER traversal output of a binary tree is U,N,I,V,E,R,S,I,T,Y and the POSTORDER traversal output of the same tree is N,U,V,R,E,T,I,S,I,Y. Construct the tree and determine the output of the PREORDER traversal output.   b. One main difference between a binary search tree (BST) and an AVL (Adelson-Velski and Landis) tree is that an AVL tree has a balance condition, that is, for every node in the AVL tree, the height of the left and right subtrees differ by at most 1....

  • Code to be written in C++: Initially, you will be given symbols printed in a preorder...

    Code to be written in C++: Initially, you will be given symbols printed in a preorder traversal of a boolean expression tree. The internal nodes of the tree will contain one of the following operators: & (and), | (or), ^ (exclusive-or), or ! (not). The nodes containing the first three operators will have two children, while the nodes containing the ! operator will contain only a left child. The leaves of the tree will contain an operand - either f...

  • Given a binary tree, it is useful to be able to display all of its data values. For this task, define a function called basic_print() which prints out all of the data values in a binary tree. The nat...

    Given a binary tree, it is useful to be able to display all of its data values. For this task, define a function called basic_print() which prints out all of the data values in a binary tree. The natural way to solve this problem is to use recursion. The diagram below illustrates a recursive solution to the problem, which consists of three simple steps: Step1: Print the root node Step 3: Print the right sub-tree 10 Step 2: Print the...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • a+b 4) (14 pts) Convert the following infix expression to postfix notation: +b)/(c-d) + e) *f-g...

    a+b 4) (14 pts) Convert the following infix expression to postfix notation: +b)/(c-d) + e) *f-g (A - B + C ) *D + EIF

  • JUST GIVE ME THE C CODE FOR THIS QUESTION. I GOT THE ASSEMBLY CODE.

    JUST GIVE ME THE C CODE FOR THIS QUESTION. I GOT THE ASSEMBLY CODE. In this assignment, you will create a C program that iteratively populates a fixed-size integer array of 3 elements, array α.withhexadecimal integer valuesprovided by scanf. The user will enter 3 positive hexadecimal integer values, one per line, and the program will store the 3 values in arYay _afOJ,arvay aflJ, and crr ay_af2J. Once the 3 integers are entered, your program will print the array ain 32...

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