Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to display the inorder, preorder, and postorder and also the value of the given expression Now inorder,postorder and preorder display successfully but value of give expression is not display and still give me error.

it give me error on line 95 which is last line and I also mentioned it in below.

print(eval_Tree(root))
NameError: name 'eval_Tree' is not defined


What I have tried:

Python
class BST:
    def __init__(self, key):
        self.key = key
        self.lchild = None
        self.rchild = None

    def insert(self, data):
        if self.key is None:
            self.key = data
            return
        if self.key == data:
            return
        if data < self.key:
            if self.lchild:
                self.lchild.insert(data)
            else:
                self.lchild = BST(data)
        else:
            if self.rchild:
                self.rchild.insert(data)
            else:
                self.rchild = BST(data)

    def preorder(self):
        print(self.key, end=" ")
        if self.lchild:
            self.lchild.preorder()
        if self.rchild:
            self.rchild.preorder()

    def inorder(self):
        if self.lchild:
            self.lchild.inorder()
        print(self.key, end=" ")
        if self.rchild:
            self.rchild.inorder()

    def postorder(self):
        if self.lchild:
            self.lchild.postorder()
        if self.rchild:
            self.rchild.postorder()
        print(self.key, end=" ")

    def eval_Tree(root):
        if root is None:
            return 0
        if root.lchild is None and root.rchild is None:
            return float(self.key)
        left_sum = eval_Tree(root.lchild)

        # evaluate right tree
        right_sum = eval_Tree(root.rchild)

        # check which operation to apply
        if root.key == '+':
            return left_sum + right_sum

        elif root.key == '-':
            return left_sum - right_sum

        elif root.key == '*':
            return left_sum * right_sum

        else:
            return left_sum / right_sum


root = BST('+')
root.lchild = BST('/')
root.lchild.lchild = BST('*')
root.lchild.lchild.lchild = BST(0)
root.lchild.lchild.rchild = BST(0)
root.lchild.rchild = BST('-')
root.lchild.rchild.lchild = BST(9)
root.lchild.rchild.rchild = BST(0)
root.rchild = BST('*')
root.rchild.lchild = BST('+')
root.rchild.lchild.lchild = BST(1)
root.rchild.lchild.rchild = BST(1)
root.rchild.rchild = BST('/')
root.rchild.rchild.lchild = BST(0)
root.rchild.rchild.rchild = BST(1)

print("Preorder")
root.preorder()
print()
print("inorder")
root.inorder()
print()
print("postorder")
root.postorder()
print()
print("Value of Given expression IS ")
print(eval_Tree(root))
Posted
Updated 12-Dec-21 2:14am
v3
Comments
Richard MacCutchan 12-Dec-21 7:55am    
"value of give expression is not display and still give me error."
What error, and where does it occur?
Richard Deeming 13-Dec-21 4:22am    
Looks like eval_Tree is a member of your BST class. Shouldn't that be:
print(root.eval_Tree())

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900