print(eval_Tree(root)) NameError: name 'eval_Tree' is not defined
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))
eval_Tree
BST
print(root.eval_Tree())
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)