3
\$\begingroup\$

Here is my solution for the Daily Coding Challenge 50

Given an arithmetic expression in the form of a binary tree, write a function to evaluate it

Example

 *
 / \
 + +
 / \ / \
3 2 4 5
Return >> 45

Is this the most efficient way to do this?

"""Build an operation tree and calculate the result"""
class Tree:
 def __init__(self, value, left=None, right=None):
 """Initialise values"""
 self.value = value
 self.left = left
 self.right = right
 def determine_sum(self, total):
 if self.left == None and self.right == None:
 return int(self.value)
 if self.value == "*":
 total += self.left.determine_sum(total) * self.right.determine_sum(total)
 if self.value == "+":
 total += self.left.determine_sum(total) + self.right.determine_sum(total)
 if self.value == "-":
 total += self.left.determine_sum(total) - self.right.determine_sum(total)
 if self.value == "/":
 total += self.left.determine_sum(total) / self.right.determine_sum(total)
 return total
if __name__ == "__main__": #
 n = Tree("*")
 n.left = Tree("+")
 n.right = Tree("+")
 n.left.right = Tree("+")
 n.right.right = Tree("+")
 n.left.left = Tree("3")
 n.left.right.left = Tree("4")
 n.left.right.right = Tree("5")
 n.right.left = Tree("6")
 n.right.right.left = Tree("7")
 n.right.right.right = Tree("4")
 sum = n.determine_sum(0)
 print(sum)
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jun 26, 2019 at 19:28
\$\endgroup\$
2
  • \$\begingroup\$ In what sense is this a binary search tree? \$\endgroup\$ Commented Jun 26, 2019 at 21:14
  • \$\begingroup\$ Sorry. Binary tree. My apologies \$\endgroup\$ Commented Jun 26, 2019 at 21:19

1 Answer 1

3
\$\begingroup\$
  • dict> ifs
  • operator contains all the functions you need.
  • Taking total as an argument is unneeded.
  • I would personally split the 'Tree' which is actually a Node into two types, operators and values. But that may go against the challenge.
  • Use is to compare to None.
import operator
operators = {
 '*': operator.mul,
 '+': operator.add,
 '-': operator.sub,
 '/': operator.truediv,
}
class Tree:
 def __init__(self, value, left=None, right=None):
 """Initialise values"""
 self.value = value
 self.left = left
 self.right = right
 def determine(self):
 if self.left is None and self.right is None:
 return int(self.value)
 return operators[self.value](
 self.left.determine(),
 self.right.determine()
 )
answered Jun 26, 2019 at 20:37
\$\endgroup\$
2
  • \$\begingroup\$ You never fail to amaze with with how succinctly you write code. Thanks \$\endgroup\$ Commented Jun 26, 2019 at 20:41
  • 2
    \$\begingroup\$ @EML With time you'll be able to too :) You'll be able to see common pitfalls that you know how to fix without even thinking. \$\endgroup\$ Commented Jun 26, 2019 at 21:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.