MCPcopy Index your code
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / evaluate

Function evaluate

ParseTree.py:38–48  ·  view source on GitHub ↗
(parseTree)

Source from the content-addressed store, hash-verified

36
37# 递归实现两个叶结点的运算
38def evaluate(parseTree):
39 opers = {'+':operator.add, '-':operator.sub, '*':operator.mul, '/':operator.truediv}
40
41 leftC = parseTree.getLeftChild()
42 rightC = parseTree.getRightChild()
43
44 if leftC and rightC:
45 fn = opers[parseTree.getRootVal()]
46 return fn(evaluate(leftC), evaluate(rightC))
47 else:
48 return parseTree.getRootVal()
49
50# 递归实现树的后序遍历
51def postorder(tree):

Callers

nothing calls this directly

Calls 3

getLeftChildMethod · 0.80
getRightChildMethod · 0.80
getRootValMethod · 0.80

Tested by

no test coverage detected